anyhow
This commit is contained in:
parent
d90011d921
commit
474841212d
1 changed files with 4 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::{cell::RefCell, collections::HashMap, fs::File, io::{Read, Write}, path::PathBuf, sync::Arc};
|
use std::{cell::RefCell, collections::HashMap, fs::File, io::{Read, Write}, path::PathBuf, sync::Arc};
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
|
|
||||||
|
@ -12,7 +13,7 @@ pub struct StorageManager {
|
||||||
|
|
||||||
impl StorageManager {
|
impl StorageManager {
|
||||||
pub fn new(path: PathBuf) -> Result<Self> {
|
pub fn new(path: PathBuf) -> Result<Self> {
|
||||||
let map = if let Ok(mut file) = File::open(path) {
|
let map = if let Ok(mut file) = File::open(path.clone()) {
|
||||||
let mut vec = Vec::new();
|
let mut vec = Vec::new();
|
||||||
file.read_to_end(&mut vec)?;
|
file.read_to_end(&mut vec)?;
|
||||||
postcard::from_bytes(&vec)?
|
postcard::from_bytes(&vec)?
|
||||||
|
@ -28,7 +29,8 @@ impl StorageManager {
|
||||||
pub fn save(&self) {
|
pub fn save(&self) {
|
||||||
let map = postcard::to_stdvec_cobs(&self.map).unwrap();
|
let map = postcard::to_stdvec_cobs(&self.map).unwrap();
|
||||||
|
|
||||||
let mut backing_file = File::open(self.storage.as_path()).unwrap();
|
let mut backing_file = File::options().write(true).truncate(true).create(true)
|
||||||
|
.open(self.storage.as_path()).unwrap();
|
||||||
backing_file.write_all(&map).unwrap();
|
backing_file.write_all(&map).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue