pub struct StateManager<R> { /* private fields */ }Expand description
The state manager.
Implementations§
Source§impl StateManager<AsyncOnlyContext>
impl StateManager<AsyncOnlyContext>
Sourcepub const fn new() -> Self
Available on crate feature async only.
pub const fn new() -> Self
async only.Creates a new StateManager appropriate for asynchronous-only
contexts.
§Example
use state_department::AsyncState;
static STATE: AsyncState = AsyncState::new();Sourcepub async fn get<T: Send + Sync + 'static>(
&self,
) -> StateRef<'_, T, AsyncOnlyContext>
Available on crate feature async only.
pub async fn get<T: Send + Sync + 'static>( &self, ) -> StateRef<'_, T, AsyncOnlyContext>
async only.Returns a reference to a value stored in the state.
§Panics
- If the state has not yet been initialized.
- If the state has been dropped.
- If the state does not contain a value of the requested type.
§Example
use state_department::AsyncState;
static STATE: AsyncState = AsyncState::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init_async(async |state| {
state.insert(Foo { bar: 42 });
})
.await;
let foo = STATE.get::<Foo>().await;
assert_eq!(foo.bar, 42);Sourcepub async fn try_get<T: Send + Sync + 'static>(
&self,
) -> Option<StateRef<'_, T, AsyncOnlyContext>>
Available on crate feature async only.
pub async fn try_get<T: Send + Sync + 'static>( &self, ) -> Option<StateRef<'_, T, AsyncOnlyContext>>
async only.Attempts to get a reference to a value stored in the state.
This function does not panic.
§Example
use state_department::AsyncState;
static STATE: AsyncState = AsyncState::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init_async(async |state| {
state.insert(Foo { bar: 42 });
})
.await;
let foo = STATE.try_get::<Foo>().await;
assert_eq!(foo.unwrap().bar, 42);
let str = STATE.try_get::<String>().await;
assert!(str.is_none());Source§impl<R> StateManager<R>
impl<R> StateManager<R>
Sourcepub fn init<F>(&self, init: F) -> StateLifetime<R>where
F: FnOnce(&mut StateRegistry<R>),
pub fn init<F>(&self, init: F) -> StateLifetime<R>where
F: FnOnce(&mut StateRegistry<R>),
Initializes the StateManager, giving you an entrypoint for
populating the state with your desired values.
§Panics
- If the state has already been initialized.
§Example
use state_department::State;
static STATE: State = State::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init(|state| {
state.insert(Foo { bar: 42 });
});
assert_eq!(STATE.get::<Foo>().bar, 42);Sourcepub fn try_init<E, F>(&self, init: F) -> Option<Result<StateLifetime<R>, E>>
pub fn try_init<E, F>(&self, init: F) -> Option<Result<StateLifetime<R>, E>>
Initializes the StateManager, giving you a fallible entrypoint
for populating the state with your desired values.
Returns None if the StateManager is already initialized.
§Example
use state_department::State;
static STATE: State = State::new();
let lifetime = STATE.try_init(|state| {
Err("oh no!")
});
assert!(lifetime.unwrap().is_err());Sourcepub async fn init_async<F>(&self, init: F) -> StateLifetime<R>where
F: AsyncFnOnce(&mut StateRegistry<R>),
pub async fn init_async<F>(&self, init: F) -> StateLifetime<R>where
F: AsyncFnOnce(&mut StateRegistry<R>),
Initializes the StateManager asynchronously, giving you an
entrypoint for populating the state with your desired values in an
asynchronous context.
§Panics
- If the state has already been initialized.
§Example
use state_department::State;
static STATE: State = State::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init_async(async |state| {
state.insert(Foo { bar: 42 });
})
.await;
assert_eq!(STATE.get::<Foo>().bar, 42);Sourcepub async fn try_init_async<E, F>(
&self,
init: F,
) -> Option<Result<StateLifetime<R>, E>>
pub async fn try_init_async<E, F>( &self, init: F, ) -> Option<Result<StateLifetime<R>, E>>
Initializes the StateManager asynchronously, giving you a
fallible entrypoint for populating the state with your desired
values in an asynchronous context.
Returns None if the StateManager is already initialized.
§Example
use state_department::State;
static STATE: State = State::new();
let lifetime = STATE.try_init_async(async |state| {
Err("oh no!")
});
assert!(lifetime.await.unwrap().is_err());Source§impl StateManager<AnyContext>
impl StateManager<AnyContext>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new StateManager appropriate for both synchronous and
asynchronous contexts.
§Example
use state_department::State;
static STATE: State = State::new();Sourcepub fn get<T: Send + Sync + 'static>(&self) -> StateRef<'_, T, AnyContext>
pub fn get<T: Send + Sync + 'static>(&self) -> StateRef<'_, T, AnyContext>
Returns a reference to a value stored in the state.
§Panics
- If the state has not yet been initialized.
- If the state has been dropped.
- If the state does not contain a value of the requested type.
§Example
use state_department::State;
static STATE: State = State::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init(|state| {
state.insert(Foo { bar: 42 });
});
let foo = STATE.get::<Foo>();
assert_eq!(foo.bar, 42);Sourcepub fn try_get<T: Send + Sync + 'static>(
&self,
) -> Option<StateRef<'_, T, AnyContext>>
pub fn try_get<T: Send + Sync + 'static>( &self, ) -> Option<StateRef<'_, T, AnyContext>>
Attempts to get a reference to a value stored in the state.
This function does not panic.
§Example
use state_department::State;
static STATE: State = State::new();
struct Foo {
bar: i32
}
let _lifetime = STATE.init(|state| {
state.insert(Foo { bar: 42 });
});
let foo = STATE.try_get::<Foo>();
assert_eq!(foo.unwrap().bar, 42);
let str = STATE.try_get::<String>();
assert!(str.is_none());Trait Implementations§
Source§impl Default for StateManager<AnyContext>
impl Default for StateManager<AnyContext>
Source§impl Default for StateManager<AsyncOnlyContext>
Available on crate feature async only.
impl Default for StateManager<AsyncOnlyContext>
async only.