pub trait SharedStorage {
type Storage<T>;
type ReadGuard<'a, T>: Deref<Target = SharedValue<T>>
where Self: 'a,
T: 'a;
type WriteGuard<'a, T>: DerefMut<Target = SharedValue<T>>
where Self: 'a,
T: 'a;
type ReadError<'a, T>
where Self: 'a,
T: 'a;
type WriteError<'a, T>
where Self: 'a,
T: 'a;
// Required methods
fn new<T>(value: SharedValue<T>) -> Self::Storage<T>;
fn read<T>(
storage: &Self::Storage<T>,
) -> Result<Self::ReadGuard<'_, T>, Self::ReadError<'_, T>>;
fn write<T>(
storage: &Self::Storage<T>,
) -> Result<Self::WriteGuard<'_, T>, Self::WriteError<'_, T>>;
}Expand description
Replaceable storage backend for super::SharedState.
Implement this trait when the built-in RefCellStorage,
MutexStorage, and RwLockStorage do not match the container you want
to use. The backend owns the actual synchronization primitive and returns
guard types that dereference to SharedValue<T>.
The library does not collapse backend errors into a custom borrowed/poisoned
enum. Your ReadError and WriteError associated types are preserved and
returned as SharedStateError::Storage.
A custom backend has this shape:
use magicstatemachines::{SArc, SharedStorage, SharedValue};
use std::sync::{Mutex, MutexGuard, TryLockError};
pub struct MyMutexStorage;
impl SharedStorage for MyMutexStorage {
type Storage<T> = Mutex<SharedValue<T>>;
type ReadGuard<'a, T> = MutexGuard<'a, SharedValue<T>> where T: 'a;
type WriteGuard<'a, T> = MutexGuard<'a, SharedValue<T>> where T: 'a;
type ReadError<'a, T> = TryLockError<MutexGuard<'a, SharedValue<T>>> where T: 'a;
type WriteError<'a, T> = TryLockError<MutexGuard<'a, SharedValue<T>>> where T: 'a;
fn new<T>(value: SharedValue<T>) -> Self::Storage<T> {
Mutex::new(value)
}
fn read<T>(
storage: &Self::Storage<T>,
) -> Result<Self::ReadGuard<'_, T>, Self::ReadError<'_, T>> {
storage.try_lock()
}
fn write<T>(
storage: &Self::Storage<T>,
) -> Result<Self::WriteGuard<'_, T>, Self::WriteError<'_, T>> {
storage.try_lock()
}
}
type SArcMyMutex<T> = SArc<MyMutexStorage, T>;Required Associated Types§
Sourcetype Storage<T>
type Storage<T>
Concrete cell or lock type containing SharedValue<T>.
Sourcetype ReadGuard<'a, T>: Deref<Target = SharedValue<T>>
where
Self: 'a,
T: 'a
type ReadGuard<'a, T>: Deref<Target = SharedValue<T>> where Self: 'a, T: 'a
Guard returned by read access.
Sourcetype WriteGuard<'a, T>: DerefMut<Target = SharedValue<T>>
where
Self: 'a,
T: 'a
type WriteGuard<'a, T>: DerefMut<Target = SharedValue<T>> where Self: 'a, T: 'a
Guard returned by write access.
Sourcetype WriteError<'a, T>
where
Self: 'a,
T: 'a
type WriteError<'a, T> where Self: 'a, T: 'a
Error returned by write access.
Required Methods§
Sourcefn new<T>(value: SharedValue<T>) -> Self::Storage<T>
fn new<T>(value: SharedValue<T>) -> Self::Storage<T>
Creates backend storage containing the authoritative state and runtime data.
Sourcefn read<T>(
storage: &Self::Storage<T>,
) -> Result<Self::ReadGuard<'_, T>, Self::ReadError<'_, T>>
fn read<T>( storage: &Self::Storage<T>, ) -> Result<Self::ReadGuard<'_, T>, Self::ReadError<'_, T>>
Attempts read access to the backend storage.
Sourcefn write<T>(
storage: &Self::Storage<T>,
) -> Result<Self::WriteGuard<'_, T>, Self::WriteError<'_, T>>
fn write<T>( storage: &Self::Storage<T>, ) -> Result<Self::WriteGuard<'_, T>, Self::WriteError<'_, T>>
Attempts write access to the backend storage.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".