StoreManager

Struct StoreManager 

Source
pub struct StoreManager<T: Storing> { /* private fields */ }
Expand description

StoreManager manages the lifecycle of a store within a specified Storage backend. It handles reading and writing store data, as well as providing mutable access to the store’s contents.

The StoreManager is designed to work with any type that implements the Storing trait, allowing it to manage different kinds of store data structures. It abstracts away the complexity of directly interacting with the storage backend, providing an easy-to-use API for managing and persisting store data.

§Example

use serde::{Deserialize, Serialize};
use storage::{StoreManager, Storing};

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

let storage = Storage::new("APP_ID");

// Create a StoreManager for managing the store data
let mut manager = StoreManager::<MyStore>::new(&storage, "my_store_id")
       .expect("Failed to create StoreManager");

// Get a mutable reference to the store
let counter = manager.get_store_mut();

counter.count = 75;

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

let counter = manager.get_store();

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

Implementations§

Source§

impl<T: Storing> StoreManager<T>

Source

pub fn from_handle( storage: &Storage, handle: StoreHandle<T>, ) -> Result<Self, StoreError>

Creates a new StoreManager by reading the store data from the provided Storage into the StoreHandle.

This function attempts to read the store data from the storage.

§Example
use serde::{Deserialize, Serialize};
use rusty_store::{StoreManager, Storing, Storage, StoreHandle};

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

let storage = Storage::new("app_id");
let handle = StoreHandle::<MyStore>::new("my_store_id");
let manager = StoreManager::from_handle(&storage, handle).expect("Failed to create StoreManager");
Source

pub fn new(storage: &Storage, store_id: &str) -> Result<Self, StoreError>

Creates a new StoreManager by reading the store data from the provided Storage into the StoreHandle.

This function attempts to read the store data from the storage.

§Example
use serde::{Deserialize, Serialize};
use rusty_store::{StoreManager, Storing, Storage};

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

let storage = Storage::new("APP_ID");
let manager = StoreManager::<MyStore>::new(&storage, "my_store_id").expect("Failed to create StoreManager");
Examples found in repository?
examples/manager_trait/main.rs (line 25)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_trait").expect("Failed to create StoreManager");
26
27    // Modify and save the data using `StoreManager`.
28    manager
29        .increment_count()
30        .expect("Failed to increment count");
31
32    let counter = manager.get_store();
33
34    println!("Count: {}", counter.count);
35}
More examples
Hide additional examples
examples/manager_uncommitted/main.rs (line 25)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_uncommitted").expect("Failed to create StoreManager");
26
27    // Modify the data without saving the changes to disk.
28    manager.increment_count();
29    manager.increment_count();
30    manager.increment_count();
31
32    // Save the data to the storage
33    manager.save().expect("Failed to save count to storage");
34
35    let counter = manager.get_store();
36
37    println!("Count: {}", counter.count);
38}
examples/manager/main.rs (line 26)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/custom_location/main.rs (line 31)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}
Source

pub fn get_store(&self) -> &T

Returns a reference to the stored data.

Examples found in repository?
examples/minimal/main.rs (line 29)
19fn main() {
20    // Initialize the Storage and create a new manager
21    let mut counter: StoreManager<MyStore> = Storage::new("com.github.mazynoah.storage")
22        .new_manager("manager")
23        .expect("Failed to create StoreManager");
24
25    counter
26        .increment_count()
27        .expect("Could not increment count");
28
29    println!("Count: {}", counter.get_store().count);
30}
More examples
Hide additional examples
examples/manager_trait/main.rs (line 32)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_trait").expect("Failed to create StoreManager");
26
27    // Modify and save the data using `StoreManager`.
28    manager
29        .increment_count()
30        .expect("Failed to increment count");
31
32    let counter = manager.get_store();
33
34    println!("Count: {}", counter.count);
35}
examples/manager_uncommitted/main.rs (line 35)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_uncommitted").expect("Failed to create StoreManager");
26
27    // Modify the data without saving the changes to disk.
28    manager.increment_count();
29    manager.increment_count();
30    manager.increment_count();
31
32    // Save the data to the storage
33    manager.save().expect("Failed to save count to storage");
34
35    let counter = manager.get_store();
36
37    println!("Count: {}", counter.count);
38}
examples/manager/main.rs (line 39)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/custom_location/main.rs (line 45)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}
Source

pub fn get_store_mut(&mut self) -> &mut T

Returns a reference to the stored data.

Examples found in repository?
examples/manager/main.rs (line 29)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
More examples
Hide additional examples
examples/custom_location/main.rs (line 35)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}
Source

pub fn get_store_alive(&mut self) -> Result<&T, StoreError>

Reads the stored data from the storage. This allows to get changes external to the application

Source

pub fn modify_store<F>(&mut self, change: F) -> Result<(), StoreError>
where F: FnMut(&mut T),

Modifies the store and commits the changes to the storage

§Example
use serde::{Deserialize, Serialize};
use rusty_store::{StoreManager, Storing, Storage};

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

let storage = Storage::new("APP_ID");
let mut manager = StoreManager::<MyStore>::new(&storage, "my_store_id")
       .expect("Failed to create StoreManager");

manager.modify_store(|store| store.count = 25).expect("Failed to write store modifications");
Examples found in repository?
examples/manager_trait/main.rs (line 15)
14    fn increment_count(&mut self) -> Result<(), rusty_store::StoreError> {
15        self.modify_store(|store| store.count += 1)
16    }
More examples
Hide additional examples
examples/minimal/main.rs (line 15)
14    fn increment_count(&mut self) -> Result<(), rusty_store::StoreError> {
15        self.modify_store(|store| store.count += 1)
16    }
Source

pub fn modify_store_uncommitted<F>(&mut self, change: F)
where F: FnMut(&mut T),

This method is used to modify the store without committing changes to disk.

§Example
use serde::{Deserialize, Serialize};
use rusty_store::{StoreManager, Storing, Storage};

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

let storage = Storage::new("APP_ID");
let mut manager = StoreManager::<MyStore>::new(&storage, "my_store_id")
       .expect("Failed to create StoreManager");

manager.modify_store_uncommitted(|store| store.count = 25);

manager.save().expect("Failed to save modifications");
Examples found in repository?
examples/manager_uncommitted/main.rs (line 15)
14    fn increment_count(&mut self) {
15        self.modify_store_uncommitted(|store| store.count += 1)
16    }
Source

pub fn save(&mut self) -> Result<(), StoreError>

This method writes the current state of the store to the storage.

Examples found in repository?
examples/manager_uncommitted/main.rs (line 33)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_uncommitted").expect("Failed to create StoreManager");
26
27    // Modify the data without saving the changes to disk.
28    manager.increment_count();
29    manager.increment_count();
30    manager.increment_count();
31
32    // Save the data to the storage
33    manager.save().expect("Failed to save count to storage");
34
35    let counter = manager.get_store();
36
37    println!("Count: {}", counter.count);
38}
More examples
Hide additional examples
examples/manager/main.rs (line 36)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/custom_location/main.rs (line 42)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}

Trait Implementations§

Source§

impl<T: Clone + Storing> Clone for StoreManager<T>

Source§

fn clone(&self) -> StoreManager<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Storing> Debug for StoreManager<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StoreManager<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for StoreManager<T>
where T: RefUnwindSafe,

§

impl<T> Send for StoreManager<T>
where T: Send,

§

impl<T> Sync for StoreManager<T>
where T: Sync,

§

impl<T> Unpin for StoreManager<T>
where T: Unpin,

§

impl<T> UnwindSafe for StoreManager<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.