1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use std::io::Result; use ::copy_io::CopyIO; pub trait CustomIO: Sized { fn save<T: CopyIO>(&self, f: &mut T) -> Result<()>; fn load<T: CopyIO>(f: &mut T) -> Result<Self>; } impl<T: CustomIO> CustomIO for Vec<T> { fn save<U: CopyIO>(&self, f: &mut U) -> Result<()> { f.write_copy(&self.len())?; for t in self.iter() { t.save(f)?; } Ok(()) } fn load<U: CopyIO>(f: &mut U) -> Result<Self> { let capacity: usize = f.read_copy()?; let mut result: Vec<T> = Vec::with_capacity(capacity); for _ in 0..capacity { result.push(T::load(f)?); } Ok(result) } }