work
This commit is contained in:
12
src/rsc.rs
12
src/rsc.rs
@@ -20,13 +20,17 @@ impl Default for DataDir {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait DataRsc: bincode::Encode + bincode::Decode<()> + Default {
|
||||
fn path() -> &'static str;
|
||||
}
|
||||
|
||||
impl DataDir {
|
||||
pub fn get(&self) -> &Path {
|
||||
self.dirs.data_local_dir()
|
||||
}
|
||||
|
||||
pub fn load<T: bincode::Decode<()> + Default>(&self, path: &str) -> T {
|
||||
let path = self.get().join(path);
|
||||
pub fn load<T: DataRsc>(&self) -> T {
|
||||
let path = self.get().join(T::path());
|
||||
match fs::read(path) {
|
||||
Ok(bytes) => match bincode::decode_from_slice(&bytes, BINCODE_CONFIG) {
|
||||
Ok((data, _)) => data,
|
||||
@@ -36,10 +40,10 @@ impl DataDir {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save<T: bincode::Encode>(&self, path: &str, data: &T) {
|
||||
pub fn save<T: DataRsc>(&self, data: &T) {
|
||||
let dir = self.get();
|
||||
fs::create_dir_all(dir).unwrap();
|
||||
let mut file = File::create(dir.join(path)).unwrap();
|
||||
let mut file = File::create(dir.join(T::path())).unwrap();
|
||||
bincode::encode_into_std_write(data, &mut file, BINCODE_CONFIG).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user