11 releases
Uses new Rust 2024
0.2.1 | Mar 28, 2025 |
---|---|
0.2.0 | Mar 28, 2025 |
0.1.9 | Dec 2, 2023 |
#972 in Parser implementations
247 downloads per month
7KB
82 lines
vedvaring
A wrapper that automatically saves your object to disk whenever it's modified, ensuring no inconsistency between the in-memory representation and what’s saved on disk.
You only need to implement one method: item_id
.
Usage
1. Implement the trait
use vedvaring::{Saved, FsTrait};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct User {
id: u32,
name: String,
}
impl FsTrait for User {
type Key = u32;
fn item_id(&self) -> Self::Key {
self.id
}
}
2. Create and save
let user = Saved::new(User { id: 1, name: "Bar Fooson".into() });
3. Read and write
let user_id = 1;
let user = Saved::load(user_id).unwrap();
println!("Good morning, {}", user.read().name);
*user.write().name = String::from("Foo Barson"); // Automatically saved after this line.
Storage
Data is by default stored to:
~/.local/share/<crate-name>/<type-name>/<id>
For example, if your crate is named my_crate
and you're saving a MyUser
,
the file will be:
~/.local/share/my_crate/myuser/<id>
Dependencies
~0.7–8MB
~67K SLoC