Persistent storage copy in JSON format for serializable in-memory data
Just like RwLock
, jsave is a reader-writer lock, but serializes and saves data to a file in json format on every writing operation finish
Do not use it unless you only want to persist a tiny amount of data
```rust use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap;
// Data to be persisted. Needs to be serializable and deserializable
struct Data {
data: HashMap
let data = Data { data: HashMap::new(), };
use std::fs::File;
// Create the file for storing the data File::create("db_file").unwrap();
use jsave::Jsave;
// Initialize a new Jsave instance with the given data. Note that the file will be truncated let db = Jsave::initwith(data, "dbfile").unwrap();
{ // Read data let dbread = db.read(); println!("{:?}", *dbread); }
{ // Read and write data let mut dbwrite = db.write(); dbwrite.data.insert("foo".toowned(), 114514); println!("{:?}", *dbwrite);
// Automatically saving data to file when the `JsaveWriteGuard` is dropped
}
drop(db);
// Initialize a new Jsave instance from a file let db = Jsave::::init("db_file").unwrap();
let dbread = db.read(); println!("{:?}", *dbread); ```
GNU General Public License v3.0