1pub trait BitIO {
42 fn to_bit(&self) -> Vec<u8>;
44 fn from_bit(a : &[u8]) -> Self;
46}
47
48use core::mem::size_of;
49
50macro_rules! BitIOPrim {
51 ($ty : ty) => {
52 impl BitIO for $ty {
53 fn to_bit(&self) -> Vec<u8> {
54 let mut res = vec![];
55 let l = self.to_be_bytes();
56 for i in l {
57 res.push(i);
58 }
59 res
60 }
61 fn from_bit(a : &[u8]) -> Self {
62 let mut arr = [0; size_of::<$ty>()];
63 for i in 0..size_of::<$ty>() {
64 arr[i] = a[i];
65 }
66 <$ty>::from_be_bytes(arr)
67 }
68 }
69 };
70}
71
72BitIOPrim!(u8);
73BitIOPrim!(u16);
74BitIOPrim!(u32);
75BitIOPrim!(u64);
76BitIOPrim!(u128);
77BitIOPrim!(usize);
78BitIOPrim!(i8);
79BitIOPrim!(i16);
80BitIOPrim!(i32);
81BitIOPrim!(i64);
82BitIOPrim!(i128);
83BitIOPrim!(isize);
84BitIOPrim!(f32);
85BitIOPrim!(f64);
86impl<T : BitIO> BitIO for Vec<T> {
87 fn to_bit(&self) -> Vec<u8> {
88 let mut res = vec![];
89 let l = self.len().to_be_bytes();
90 for i in l {
91 res.push(i);
92 }
93 for i in self {
94 let mut temp = i.to_bit();
95 let l = temp.len().to_be_bytes();
96 for i in l {
97 res.push(i);
98 }
99 res.append(&mut temp);
100 }
101 res
102 }
103 fn from_bit(a : &[u8]) -> Self {
104 let mut res = vec![];
105 let mut buf = [0; size_of::<usize>()];
106 let mut temp = 0;
107 for i in 0..size_of::<usize>() {
108 buf[i] = a[temp];
109 temp = temp + 1;
110 }
111 let len = usize::from_be_bytes(buf);
112 for _ in 0..len {
113 for i in 0..size_of::<usize>() {
114 buf[i] = a[temp];
115 temp = temp + 1;
116 }
117 let len = usize::from_be_bytes(buf);
118 let mut buf2 = vec![];
119 for _ in 0..len {
120 buf2.push(a[temp]);
121 temp = temp + 1;
122 }
123 res.push(T::from_bit(&buf2));
124 }
125 res
126 }
127}
128impl BitIO for String {
129 fn to_bit(&self) -> Vec<u8> {
130 let mut res = vec![];
131 let l = self.as_bytes().len().to_be_bytes();
132 for i in l {
133 res.push(i);
134 }
135 for i in self.as_bytes() {
136 res.push(*i);
137 }
138 res
139 }
140 fn from_bit(a : &[u8]) -> Self {
141 let mut res = vec![];
142 let mut buf = [0; size_of::<usize>()];
143 let mut temp = 0;
144 for i in 0..size_of::<usize>() {
145 buf[i] = a[temp];
146 temp = temp + 1;
147 }
148 let len = usize::from_be_bytes(buf);
149 for _ in 0..len {
150 res.push(a[temp]);
151 temp = temp + 1;
152 }
153 String::from_utf8(res).expect("from_bit<String>:invalid utf8")
154 }
155}
156
157pub trait StrIO {
163 fn to_str(&self) -> String;
164 fn from_str(s : &str) -> Self;
165}
166
167macro_rules! StrIOPrim {
168 ($ty : ty) => {
169 impl StrIO for $ty {
170 fn to_str(&self) -> String {
171 self.to_string()
172 }
173 fn from_str(s : &str) -> Self {
174 s.parse::<$ty>().unwrap()
175 }
176 }
177 };
178}
179
180StrIOPrim!(u8);
181StrIOPrim!(u16);
182StrIOPrim!(u32);
183StrIOPrim!(u64);
184StrIOPrim!(u128);
185StrIOPrim!(usize);
186
187StrIOPrim!(i8);
188StrIOPrim!(i16);
189StrIOPrim!(i32);
190StrIOPrim!(i64);
191StrIOPrim!(i128);
192StrIOPrim!(isize);
193
194StrIOPrim!(f32);
195StrIOPrim!(f64);
196
197impl StrIO for String {
198 fn to_str(&self) -> String {
199 self.clone()
200 }
201 fn from_str(s : &str) -> Self {
202 String::from(s)
203 }
204}