1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use rusty_store::{manager::StorageManager, Storage, StoreHandle, Storing, StoringType};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct MyStore {
    pub count: u32,
}

impl Storing for MyStore {
    fn store_type() -> StoringType {
        StoringType::Data
    }
}

pub trait MyStoreTrait {
    fn increment_count(&mut self);
}

impl MyStoreTrait for StorageManager<MyStore> {
    fn increment_count(&mut self) {
        self.modify_store_uncommitted(|store| store.count += 1)
    }
}

fn main() {
    // Initialize the Storage with the defaults
    let storage = Storage::new("com.github.mazynoah.storage".to_owned());

    // Create a handle for managing the store data.
    let handle = StoreHandle::<MyStore>::new("manager_uncommitted");

    // Use `StorageManager` to manage the handle's change.
    let mut manager =
        StorageManager::new(&storage, handle).expect("Failed to create StorageManager");

    // Modify the data without saving the changes to disk.
    manager.increment_count();
    manager.increment_count();
    manager.increment_count();

    // Save the data to the storage
    manager.save().expect("Failed to save count to storage");

    let counter = manager.get_store();

    println!("Count: {}", counter.count);
}