network_flow/io/
mod.rs

1//! 实现对图进行输入和输出的module
2//! 
3
4/// 将数据从原来的形式与字节形式之间进行转换
5/// 
6/// 对基础的数字类型、Vec、String进行了实现
7/// 
8/// 对于其他自定义类型,需要手动实现该trait,如:
9/// 
10/// ```
11/// use network_flow::io::BitIO;
12/// use std::mem::size_of;
13/// struct MyStruct {
14///     a : String,
15///     b : u32,
16///     c : Vec<String>
17/// }
18/// impl BitIO for MyStruct {
19///     fn to_bit(&self) -> Vec<u8> {
20///         let mut temp = self.a.to_bit();
21///         let mut res = temp.len().to_bit();
22///         res.append(&mut temp);
23///         res.append(&mut self.b.to_bit());
24///         res.append(&mut self.c.to_bit());
25///         res
26///     }
27///     fn from_bit(a : &[u8]) -> Self {
28///         let mut temp = size_of::<usize>();
29///         let len = usize::from_bit(a);
30///         let temp1 = String::from_bit(&a[temp..]);
31///         temp = temp + len;
32///         let temp2 = u32::from_bit(&a[temp..]);
33///         temp = temp + size_of::<u32>();
34///         let temp3 = Vec::<String>::from_bit(&a[temp..]);
35///         MyStruct { a: temp1, b: temp2, c: temp3 }
36///     }
37/// }
38/// ```
39/// 
40/// 使用实现了该trait的类型构建的图,可以进行自动的文件输入输出,保存当前图中的状态或者读取原来的图的状态。
41pub trait BitIO {
42    /// 转为字节形式
43    fn to_bit(&self) -> Vec<u8>;
44    /// 从字节形式生成
45    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
157/// 将数据从原来的形式与字符串形式之间进行转换
158/// 
159/// 对基础的数字类型、String进行了实现
160/// 
161/// 对于其他自定义类型,需要手动实现该trait
162pub 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}