pack.rs (590B)
1 use serde::{de::DeserializeOwned, ser::Serialize}; 2 3 pub fn unpack_data<T>(fname: &str) -> Result<T, String> 4 where 5 T: DeserializeOwned, 6 { 7 let r = snap::read::FrameDecoder::new(std::fs::File::open(fname).map_err(|e| e.to_string())?); 8 bincode::deserialize_from(r).map_err(|e| e.to_string()) 9 } 10 11 pub fn pack_data<T>(fname: &str, data: &T) -> Result<(), String> 12 where 13 T: Serialize, 14 { 15 let f = std::fs::File::create(fname).map_err(|e| e.to_string())?; 16 let w = snap::write::FrameEncoder::new(f); 17 bincode::serialize_into(w, data).map_err(|e| e.to_string())?; 18 Ok(()) 19 }