Struct rusty_store::manager::StorageManager
source · pub struct StorageManager<T: Storing> { /* private fields */ }Expand description
Used to manage store handles
§Example
use serde::{Deserialize, Serialize};
use storage::{manager::StorageManager, Storage, StoreHandle, Storing, StoringType};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct MyStore {
pub count: u32,
}
impl Storing for MyStore {
fn store_type() -> StoringType {
StoringType::Data
}
}
// 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");
// Use `StorageManager` to manage the handle's change.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// 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> StorageManager<T>
impl<T: Storing> StorageManager<T>
sourcepub fn new(
storage: &Storage,
handle: StoreHandle<T>,
) -> Result<Self, StoreError>
pub fn new( storage: &Storage, handle: StoreHandle<T>, ) -> Result<Self, StoreError>
Creates a new StorageManager by reading the store data from the provided Storage into the StoreHandle.
This function attempts to read the store data from the storage.
§Example
let storage = Storage::new(&app);
let handle = StoreHandle::<MyStore>::new("my_store_id");
let manager = StorageManager::new(&storage, handle).expect("Failed to create StorageManager");Examples found in repository?
examples/manager_trait/main.rs (line 34)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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_trait");
// Use `StorageManager` to manage the store.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// Modify and save the data using `StorageManager`.
manager
.increment_count()
.expect("Failed to increment count");
let counter = manager.get_store();
println!("Count: {}", counter.count);
}More examples
examples/manager/main.rs (line 24)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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");
// Use `StorageManager` to manage the handle's change.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// 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);
}examples/manager_uncommitted/main.rs (line 34)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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);
}sourcepub fn get_store(&self) -> &T
pub fn get_store(&self) -> &T
Returns a reference to the stored data.
Examples found in repository?
examples/manager_trait/main.rs (line 41)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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_trait");
// Use `StorageManager` to manage the store.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// Modify and save the data using `StorageManager`.
manager
.increment_count()
.expect("Failed to increment count");
let counter = manager.get_store();
println!("Count: {}", counter.count);
}More examples
examples/manager/main.rs (line 34)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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");
// Use `StorageManager` to manage the handle's change.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// 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);
}examples/manager_uncommitted/main.rs (line 44)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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);
}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?
examples/manager/main.rs (line 27)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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");
// Use `StorageManager` to manage the handle's change.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// 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);
}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>
This method is used to modify the store.
§Example
let storage = Storage::new(&app);
let handle = StoreHandle::<MyStore>::new("my_store_id");
let manager = StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
manager.modify_store(|store| store.some_field = 25).expect("Failed to write store modifications");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
let storage = Storage::new(&app);
let handle = StoreHandle::<MyStore>::new("my_store_id");
let manager = StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
manager.modify_store_uncommitted(|store| store.some_field = 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?
examples/manager/main.rs (line 32)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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");
// Use `StorageManager` to manage the handle's change.
let mut manager =
StorageManager::new(&storage, handle).expect("Failed to create StorageManager");
// 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);
}More examples
examples/manager_uncommitted/main.rs (line 42)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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);
}Trait Implementations§
source§impl<T: Clone + Storing> Clone for StorageManager<T>
impl<T: Clone + Storing> Clone for StorageManager<T>
source§fn clone(&self) -> StorageManager<T>
fn clone(&self) -> StorageManager<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<T> Freeze for StorageManager<T>where
T: Freeze,
impl<T> RefUnwindSafe for StorageManager<T>where
T: RefUnwindSafe,
impl<T> Send for StorageManager<T>where
T: Send,
impl<T> Sync for StorageManager<T>where
T: Sync,
impl<T> Unpin for StorageManager<T>where
T: Unpin,
impl<T> UnwindSafe for StorageManager<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit)