pub struct Storage<K, V> { /* private fields */ }Expand description
Storage.
Fundamentally, storages are solely thin wrappers around implementors of the
Collection trait, which allows to dereference or downcast to a concrete
type, if known, both, immutably and mutably. Collection is a convenience
trait, is dyn-compatible, and combines several of the Store traits.
While zrx-store implements generic key-value stores, zrx-storage
focuses on providing abstractions and utilities for orchestrating immutable
and mutable access across multiple key-value stores with varying semantics,
encapsulating boilerplate, and making it easy to write reusable operators.
§Examples
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::default();
storage.insert("a", 4);
storage.insert("b", 2);
storage.insert("c", 3);
storage.insert("d", 1);
// Create iterator over the storage
for (key, value) in &*storage {
println!("{key}: {value}");
}Implementations§
Source§impl<K, V> Storage<K, V>
impl<K, V> Storage<K, V>
Sourcepub fn new<T>(collection: T) -> Selfwhere
T: Collection<K, V>,
pub fn new<T>(collection: T) -> Selfwhere
T: Collection<K, V>,
Creates a new storage from the given collection.
§Examples
use std::collections::HashMap;
use zrx_storage::Storage;
// Create storage
let mut storage = Storage::new(HashMap::new());
// Insert value
storage.insert("key", 42);Sourcepub fn downcast_ref<T>(&self) -> Result<&T>where
T: Collection<K, V>,
pub fn downcast_ref<T>(&self) -> Result<&T>where
T: Collection<K, V>,
Attempts to downcast to a mutable reference of T.
This method is a wrapper around the method with the same name that is
implemented for Collection trait objects, and is primarily provided
for convenience, so failed downcasts return a Result instead of an
Option, turning them into errors.
§Errors
Returns Error::Downcast if conversion fails.
§Examples
use std::collections::HashMap;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::new(HashMap::new());
storage.insert("key", 42);
// Downcast storage to inner collection
let store = storage.downcast_ref::<HashMap<_, _>>()?;Sourcepub fn downcast_mut<T>(&mut self) -> Result<&mut T>where
T: Collection<K, V>,
pub fn downcast_mut<T>(&mut self) -> Result<&mut T>where
T: Collection<K, V>,
Attempts to downcast to a mutable reference of T.
This method is a wrapper around the method with the same name that is
implemented for Collection trait objects, and is primarily provided
for convenience, so failed downcasts return a Result instead of an
Option, turning them into errors.
§Errors
Returns Error::Downcast if conversion fails.
§Examples
use std::collections::HashMap;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::new(HashMap::new());
storage.insert("key", 42);
// Downcast storage to inner collection
let store = storage.downcast_mut::<HashMap<_, _>>()?;Trait Implementations§
Source§impl<K, V> Default for Storage<K, V>
impl<K, V> Default for Storage<K, V>
Source§fn default() -> Self
fn default() -> Self
Creates a storage with HashMap::default as a collection.
Note that this method does not allow to customize the BuildHasher,
but uses ahash by default, which is the fastest known hasher.
§Examples
use zrx_storage::Storage;
// Create storage
let mut storage = Storage::default();
// Insert value
storage.insert("key", 42);Source§impl<T, K, V> From<T> for Storage<K, V>
impl<T, K, V> From<T> for Storage<K, V>
Source§fn from(iter: T) -> Self
fn from(iter: T) -> Self
Creates a storage from an iterator.
This implementation allows to create storages directly from iterators
of key-value pairs, which is particularly useful for testing, as well
as documentation examples. Note that the inner collection will always
be a HashMap when using this method.
§Examples
use zrx_storage::Storage;
// Create storage from iterator
let mut storage = Storage::from([("key", 42)]);Source§impl<K, V> FromIterator<(K, V)> for Storage<K, V>
impl<K, V> FromIterator<(K, V)> for Storage<K, V>
Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = (K, V)>,
fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = (K, V)>,
Auto Trait Implementations§
impl<K, V> Freeze for Storage<K, V>
impl<K, V> !RefUnwindSafe for Storage<K, V>
impl<K, V> !Send for Storage<K, V>
impl<K, V> !Sync for Storage<K, V>
impl<K, V> Unpin for Storage<K, V>
impl<K, V> UnsafeUnpin for Storage<K, V>
impl<K, V> !UnwindSafe for Storage<K, V>
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<K, V> TryAsStorage<K> for V
impl<K, V> TryAsStorage<K> for V
Source§fn try_as_storage(
item: &(dyn Any + 'static),
) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
fn try_as_storage( item: &(dyn Any + 'static), ) -> Result<<V as TryAsStorage<K>>::Target<'_>, Error>
Attempts to convert into a storage reference.
§Errors
The following errors might be returned:
Error::Downcast: Item cannot be downcast.
§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorage;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);
// Obtain type-erased reference
let item: &dyn Any = &storage;
// Obtain storage reference
let storage = <i32>::try_as_storage(item)?;Source§impl<K, V> TryAsStorageMut<K> for V
impl<K, V> TryAsStorageMut<K> for V
Source§fn try_as_storage_mut(
item: &mut (dyn Any + 'static),
) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
fn try_as_storage_mut( item: &mut (dyn Any + 'static), ) -> Result<<V as TryAsStorageMut<K>>::Target<'_>, Error>
Attempts to convert into a mutable storage reference.
§Errors
The following errors might be returned:
Error::Downcast: Item cannot be downcast.
§Examples
use std::any::Any;
use zrx_storage::convert::TryAsStorageMut;
use zrx_storage::Storage;
// Create storage and initial state
let mut storage = Storage::default();
storage.insert("key", 42);
// Obtain mutable type-erased reference
let item: &mut dyn Any = &mut storage;
// Obtain mutable storage reference
let storage = <i32>::try_as_storage_mut(item)?;