Trait tide_disco::method::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§
Required Methods§
sourcefn 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,
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.
Object Safety§
Implementations on Foreign Types§
source§impl ReadState for ()
impl ReadState for ()
This allows you to do api.get(...)
in a simple API where the state is ()
.