Skip to main content

Storage

Struct Storage 

Source
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>

Source

pub fn new<T>(collection: T) -> Self
where 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);
Source

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<_, _>>()?;
Source

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: Debug, V: Debug> Debug for Storage<K, V>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for Storage<K, V>
where K: Key, V: Value,

Source§

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<K, V> Deref for Storage<K, V>

Source§

fn deref(&self) -> &Self::Target

Dereferences to the inner collection.

Source§

type Target = dyn Collection<K, V>

The resulting type after dereferencing.
Source§

impl<K, V> DerefMut for Storage<K, V>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Dereferences to the inner collection mutably.

Source§

impl<T, K, V> From<T> for Storage<K, V>
where T: IntoIterator<Item = (K, V)>, K: Key, V: Value,

Source§

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>
where K: Key, V: Value,

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = (K, V)>,

Creates a storage from an iterator.

When creating storages directly from an iterator, which is primarily intended for testing, the inner collection is a HashMap.

§Examples
use zrx_storage::Storage;

// Create storage from iterator
let mut storage = Storage::from_iter([("key", 42)]);

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> 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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<K, V> TryAsStorage<K> for V
where K: Key, V: Value,

Source§

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:

§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§

type Target<'a> = &'a Storage<K, V>

Target type of conversion.
Source§

impl<K, V> TryAsStorageMut<K> for V
where K: Key, V: Value,

Source§

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:

§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)?;
Source§

type Target<'a> = &'a mut Storage<K, V>

Target type of conversion.
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.
Source§

impl<K, V, T> StoreFromIterator<K, V> for T
where T: FromIterator<(K, V)>,

Source§

impl<T> Value for T
where T: Debug + 'static,