Struct rusty_store::Storage
source · pub struct Storage { /* private fields */ }Expand description
Handles file system paths for reading from and writing to data storage.
The Storage struct provides a way to manage file paths used for storing data in different locations.
It simplifies the process of accessing and modifying data by providing methods for these operations.
§Example
use serde::{Deserialize, Serialize};
use storage::{Storage, StoreHandle, Storing, StoringType};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
pub struct MyStore {
pub count: u32,
}
impl MyStore {
fn increment_count(&mut self) {
self.count += 1;
}
}
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 mut handle = StoreHandle::<MyStore>::new("handle");
// Read existing store from storage
storage
.read(&mut handle)
.expect("Failed to read from storage");
// Modify the store data
let counter = handle.get_store_mut();
counter.increment_count();
counter.increment_count();
counter.increment_count();
// Write changes to disk
storage
.write(&mut handle)
.expect("Failed to write to storage");
let counter = handle.get_store();
println!("Count: {}", counter.count);
Implementations§
source§impl Storage
impl Storage
sourcepub fn new(app_id: String) -> Self
pub fn new(app_id: String) -> Self
Creates a new Storage instance by obtaining the paths for cache, data, and configuration directories.
§Panics
- Panics if the cache directory, data directory, or configuration directory path cannot be determined.
Examples found in repository?
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
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);
}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 48
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 mut handle = StoreHandle::<MyStore>::new("handle");
// Read existing store from storage
storage
.read(&mut handle)
.expect("Failed to read from storage");
// Modify the store data
let counter = handle.get_store_mut();
counter.increment_count();
counter.increment_count();
counter.increment_count();
// Write changes to disk
storage
.write(&mut handle)
.expect("Failed to write to storage");
let counter = handle.get_store();
println!("Count: {}", counter.count);
}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 from(cache_dir: PathBuf, data_dir: PathBuf, config_dir: PathBuf) -> Self
pub fn from(cache_dir: PathBuf, data_dir: PathBuf, config_dir: PathBuf) -> Self
Creates a new Storage instance by obtaining the paths for cache, data, and configuration directories.
sourcepub fn read<T: Storing>(
&self,
handle: &mut StoreHandle<T>,
) -> Result<(), StoreError>
pub fn read<T: Storing>( &self, handle: &mut StoreHandle<T>, ) -> Result<(), StoreError>
Reads the store from a file and updates the provided StoreHandle.
If the file does not exist, it creates a default store if a default is available.
§Example
let storage = Storage::new(app);
let handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");
storage.read(&mut handle).expect("Failed to read store");
Examples found in repository?
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 48
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 mut handle = StoreHandle::<MyStore>::new("handle");
// Read existing store from storage
storage
.read(&mut handle)
.expect("Failed to read from storage");
// Modify the store data
let counter = handle.get_store_mut();
counter.increment_count();
counter.increment_count();
counter.increment_count();
// Write changes to disk
storage
.write(&mut handle)
.expect("Failed to write to storage");
let counter = handle.get_store();
println!("Count: {}", counter.count);
}sourcepub fn write<T: Storing>(
&self,
handle: &mut StoreHandle<T>,
) -> Result<(), StoreError>
pub fn write<T: Storing>( &self, handle: &mut StoreHandle<T>, ) -> Result<(), StoreError>
Writes the current store T from the provided StoreHandle to a file.
If the file does not exist, it creates a default store if a default is available.
§Example
let storage = Storage::new(app);
let handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");
storage.write(&mut handle).expect("Failed to read store");
Examples found in repository?
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 48
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 mut handle = StoreHandle::<MyStore>::new("handle");
// Read existing store from storage
storage
.read(&mut handle)
.expect("Failed to read from storage");
// Modify the store data
let counter = handle.get_store_mut();
counter.increment_count();
counter.increment_count();
counter.increment_count();
// Write changes to disk
storage
.write(&mut handle)
.expect("Failed to write to storage");
let counter = handle.get_store();
println!("Count: {}", counter.count);
}Trait Implementations§
source§impl<'de> Deserialize<'de> for Storage
impl<'de> Deserialize<'de> for Storage
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Storage
impl RefUnwindSafe for Storage
impl Send for Storage
impl Sync for Storage
impl Unpin for Storage
impl UnwindSafe for Storage
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
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)
clone_to_uninit)