fast_io/
lib.rs

1pub mod copy_io;
2pub mod custom_io;
3
4pub use copy_io::*;
5pub use custom_io::*;
6
7pub mod prelude {
8    pub use copy_io::CopyIO;
9    pub use custom_io::CustomIO;
10    pub use std::io::Result;
11}
12
13#[cfg(test)]
14mod tests {
15    use std::fs::{File, remove_file};
16    use super::prelude::*;
17
18    #[test]
19    fn copy_io() {
20        let path = "copy_test.txt";
21        let a: i32 = -4242;
22        let b: f64 = 42.42;
23        let c: u8 = 42;
24        let d: Point = Point {x: -42.42, y: 42.0};
25        let e: &str = "42";
26        let g: &[f64] = &[2.6125, 5.25, 10.5, 21.0, 42.0];
27
28        // Write
29        {
30            let mut f = File::create(path).expect("File creation failed");
31            // Write copy test
32            f.write_copy(&a).expect("A");
33            f.write_copy(&b).expect("B");
34            f.write_copy(&c).expect("C");
35            f.write_copy(&d).expect("D");
36
37            // Write str test
38            f.write_str(e).expect("E");
39
40            // Write slices test
41            f.write_slice(g).expect("G");
42        }
43
44        // Read
45        {
46            let mut f = File::open(path).expect("File opening failed");
47            // Read copy
48            assert_eq!(f.read_copy::<i32>().expect("A"), a);
49            assert_eq!(f.read_copy::<f64>().expect("B"), b);
50            assert_eq!(f.read_copy::<u8>().expect("C"), c);
51            assert_eq!(f.read_copy::<Point>().expect("D"), d);
52
53            // Read str
54            assert_eq!(&f.read_str().expect("E"), e);
55
56            // Read slices
57            assert_eq!(f.read_slice::<f64>().expect("G").as_slice(), g);
58        }
59        remove_file(path).unwrap();
60    }
61
62    #[test]
63    fn custom_io() {
64        let path = "custom_test.txt";
65        let a = Neuron {bias: 0.0, prev_delta: 15.7, output: 19.8};
66        let b = vec![a.clone();10];
67        let c = Some(Neuron { bias: 27.9, prev_delta: 12.87, output: 0.0});
68        let d: Option<Neuron> = None;
69
70        // Write
71        {
72            let mut f = File::create(path).expect("File creation failed");
73            // Write copy test
74            a.save(&mut f).expect("A");
75            b.save(&mut f).expect("B");
76            c.save(&mut f).expect("C");
77            d.save(&mut f).expect("D");
78        }
79        // Read
80        {
81            let mut f = File::open(path).expect("File opening failed");
82
83            assert_eq!(Neuron::load(&mut f).expect("A"), a);
84            assert_eq!(Vec::<Neuron>::load(&mut f).expect("B"), b);
85            assert_eq!(Option::<Neuron>::load(&mut f).expect("C"), c);
86            assert_eq!(Option::<Neuron>::load(&mut f).expect("D"), d);
87        }
88        remove_file(path).unwrap();
89    }
90
91    #[derive(Copy, Clone, Debug, PartialEq)]
92    struct Point {
93        x: f64,
94        y: f64
95    }
96
97    #[derive(Debug, Clone)]
98    struct Neuron {
99        bias: f64,
100        prev_delta: f64,
101        output: f64
102    }
103
104    impl CustomIO for Neuron {
105        fn save<T: CopyIO>(&self, f: &mut T) -> Result<()> {
106            f.write_copy(&self.bias)
107        }
108        fn load<T: CopyIO>(f: &mut T) -> Result<Self> {
109            Ok(Neuron {
110                bias: f.read_copy()?,
111                prev_delta: 0.0,
112                output: 0.0
113            })
114        }
115    }
116
117    impl PartialEq for Neuron {
118        fn eq(&self, rhs: &Self) -> bool {
119            self.bias == rhs.bias
120        }
121    }
122}