StateManager

Struct StateManager 

Source
pub struct StateManager<R> { /* private fields */ }
Expand description

The state manager.

Implementations§

Source§

impl StateManager<AsyncOnlyContext>

Source

pub const fn new() -> Self

Available on crate feature async only.

Creates a new StateManager appropriate for asynchronous-only contexts.

§Example
use state_department::AsyncState;

static STATE: AsyncState = AsyncState::new();
Source

pub async fn get<T: Send + Sync + 'static>( &self, ) -> StateRef<'_, T, AsyncOnlyContext>

Available on crate feature 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);
Source

pub async fn try_get<T: Send + Sync + 'static>( &self, ) -> Option<StateRef<'_, T, AsyncOnlyContext>>

Available on crate feature 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>

Source

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);
Source

pub fn try_init<E, F>(&self, init: F) -> Option<Result<StateLifetime<R>, E>>
where F: FnOnce(&mut StateRegistry<R>) -> Result<(), 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());
Source

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);
Source

pub async fn try_init_async<E, F>( &self, init: F, ) -> Option<Result<StateLifetime<R>, E>>
where F: AsyncFnOnce(&mut StateRegistry<R>) -> Result<(), 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>

Source

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();
Source

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);
Source

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>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Default for StateManager<AsyncOnlyContext>

Available on crate feature async only.
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<R> Drop for StateManager<R>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<R> Sync for StateManager<R>

Auto Trait Implementations§

§

impl<R> !Freeze for StateManager<R>

§

impl<R> !RefUnwindSafe for StateManager<R>

§

impl<R> Send for StateManager<R>
where R: Send + Sync,

§

impl<R> Unpin for StateManager<R>
where R: Unpin,

§

impl<R> !UnwindSafe for StateManager<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.