tide_disco::method

Trait ReadState

Source
pub trait ReadState {
    type State: 'static;

    // Required method
    fn read<'life0, 'async_trait, T>(
        &'life0 self,
        op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>,
    ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
       where T: 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

A state which allows read access.

Implementations may allow shared read access (for instance, [RwLock]) but some implementations do not (for instance, [Mutex]). Therefore, do not assume that read is reentrant, or you may have a deadlock.

Required Associated Types§

Source

type State: 'static

The type of state which this type allows a caller to read.

Required Methods§

Source

fn read<'life0, 'async_trait, T>( &'life0 self, op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>, ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
where T: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Do an operation with immutable access to the state.

§Limitations

The reason this function takes a visitor function to apply to the state, rather than just returning a reference to the state, is to allow implementations that cannot provide a plain reference directly. For example, [RwLock] can produce a smart reference, a RwLockReadGuard, but not a plain reference.

Note that GATs may allow us to make this interface more ergonomic in the future. With stable GATs, this trait could be written like

trait ReadState {
    type State: 'static;
    type Reference<'a>: 'a + Deref<Target = Self::State>;
    fn read(&self) -> Self::Reference<'_>;
}

Like many function parameters in tide_disco, the function to apply to the state is also required to return a boxed future.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ReadState for ()

This allows you to do api.get(...) in a simple API where the state is ().

Source§

type State = ()

Source§

fn read<'life0, 'async_trait, T>( &'life0 self, op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>, ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
where T: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<R: Send + Sync + ReadState> ReadState for Arc<R>

Source§

type State = <R as ReadState>::State

Source§

fn read<'life0, 'async_trait, T>( &'life0 self, op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>, ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
where T: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<State: 'static + Send + Sync> ReadState for Mutex<State>

Source§

type State = State

Source§

fn read<'life0, 'async_trait, T>( &'life0 self, op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>, ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
where T: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Source§

impl<State: 'static + Send + Sync> ReadState for RwLock<State>

Source§

type State = State

Source§

fn read<'life0, 'async_trait, T>( &'life0 self, op: impl 'async_trait + Send + for<'a> FnOnce(&'a Self::State) -> BoxFuture<'a, T>, ) -> Pin<Box<dyn Future<Output = T> + Send + 'async_trait>>
where T: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Implementors§