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>
impl<T: Storing> StoreManager<T>
Sourcepub fn from_handle(
storage: &Storage,
handle: StoreHandle<T>,
) -> Result<Self, StoreError>
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");Sourcepub fn new(storage: &Storage, store_id: &str) -> Result<Self, StoreError>
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?
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
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}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}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}Sourcepub fn get_store(&self) -> &T
pub fn get_store(&self) -> &T
Returns a reference to the stored data.
Examples found in repository?
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
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}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}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}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}Sourcepub fn get_store_mut(&mut self) -> &mut T
pub fn get_store_mut(&mut self) -> &mut T
Returns a reference to the stored data.
Examples found in repository?
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
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}Sourcepub fn get_store_alive(&mut self) -> Result<&T, StoreError>
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
Sourcepub fn modify_store<F>(&mut self, change: F) -> Result<(), StoreError>
pub fn modify_store<F>(&mut self, change: F) -> Result<(), StoreError>
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?
More examples
Sourcepub fn modify_store_uncommitted<F>(&mut self, change: F)
pub fn modify_store_uncommitted<F>(&mut self, change: F)
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");Sourcepub fn save(&mut self) -> Result<(), StoreError>
pub fn save(&mut self) -> Result<(), StoreError>
This method writes the current state of the store to the storage.
Examples found in repository?
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
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}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>
impl<T: Clone + Storing> Clone for StoreManager<T>
Source§fn clone(&self) -> StoreManager<T>
fn clone(&self) -> StoreManager<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more