Skip to main content

SharedStorage

Trait SharedStorage 

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

Source

type Storage<T>

Concrete cell or lock type containing SharedValue<T>.

Source

type ReadGuard<'a, T>: Deref<Target = SharedValue<T>> where Self: 'a, T: 'a

Guard returned by read access.

Source

type WriteGuard<'a, T>: DerefMut<Target = SharedValue<T>> where Self: 'a, T: 'a

Guard returned by write access.

Source

type ReadError<'a, T> where Self: 'a, T: 'a

Error returned by read access.

Source

type WriteError<'a, T> where Self: 'a, T: 'a

Error returned by write access.

Required Methods§

Source

fn new<T>(value: SharedValue<T>) -> Self::Storage<T>

Creates backend storage containing the authoritative state and runtime data.

Source

fn read<T>( storage: &Self::Storage<T>, ) -> Result<Self::ReadGuard<'_, T>, Self::ReadError<'_, T>>

Attempts read access to the backend storage.

Source

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".

Implementors§

Source§

impl SharedStorage for MutexStorage

Available on crate feature std only.
Source§

type Storage<T> = Mutex<SharedValue<T>>

Source§

type ReadGuard<'a, T> = MutexGuard<'a, SharedValue<T>> where T: 'a

Source§

type WriteGuard<'a, T> = MutexGuard<'a, SharedValue<T>> where T: 'a

Source§

type ReadError<'a, T> = TryLockError<MutexGuard<'a, SharedValue<T>>> where T: 'a

Source§

type WriteError<'a, T> = TryLockError<MutexGuard<'a, SharedValue<T>>> where T: 'a

Source§

impl SharedStorage for RefCellStorage

Source§

type Storage<T> = RefCell<SharedValue<T>>

Source§

type ReadGuard<'a, T> = Ref<'a, SharedValue<T>> where T: 'a

Source§

type WriteGuard<'a, T> = RefMut<'a, SharedValue<T>> where T: 'a

Source§

type ReadError<'a, T> = BorrowError where T: 'a

Source§

type WriteError<'a, T> = BorrowMutError where T: 'a

Source§

impl SharedStorage for RwLockStorage

Available on crate feature std only.
Source§

type Storage<T> = RwLock<SharedValue<T>>

Source§

type ReadGuard<'a, T> = RwLockReadGuard<'a, SharedValue<T>> where T: 'a

Source§

type WriteGuard<'a, T> = RwLockWriteGuard<'a, SharedValue<T>> where T: 'a

Source§

type ReadError<'a, T> = TryLockError<RwLockReadGuard<'a, SharedValue<T>>> where T: 'a

Source§

type WriteError<'a, T> = TryLockError<RwLockWriteGuard<'a, SharedValue<T>>> where T: 'a