State

Struct State 

Source
pub struct State<S> { /* private fields */ }
Expand description

A thread-safe state, that can be used to share application state globally.

It is similar to a RWLock<S>, but it also allows asynchronous waiting for state changes. This can be useful to coordinate between different parts of an application.

Implementations§

Source§

impl<S> State<S>

Source

pub fn new(state: S) -> Self

Creates a new state.

Source

pub fn new_with_on_change( state: S, on_change: impl Fn(&S, &S) + 'static, ) -> Self

Creates a new state with the given on_change callback.

§Notes

The callback is not called when the state is set for the first time, as well as on the State::update method. You must call the callback manually in these cases.

Source

pub fn set_on_change( &mut self, on_change: impl Fn(&S, &S) + 'static, ) -> Result<(), UpdateOnChangeError>

Tries to set the on_change callback, to the new callback.

§Notes

The callback is not called when the state is set for the first time, as well as on the State::update method. You must call the callback manually in these cases.

Source

pub fn ref_count(&self) -> usize

Returns the number of references to the state. This can be used to check if there are any other references to the state.

Source

pub fn get_ref(&self) -> StateRef<'_, S>

Returns a reference to the current state. This can be used if the state does not implement Clone or if you want to avoid cloning.

Source

pub fn wait_for<C>(&self, wait_for: C) -> StateFuture<S, C>
where C: Fn(&S) -> bool,

Returns a future that completes when the state matches the given predicate.

Source

pub fn set(&self, state: S)

Sets the state to the given value.

Source

pub fn update(&self, f: impl FnOnce(&mut S))

Updates the state using the given function. This avoids having to create a new state value, which can be useful for large state values.

§Notes

This DOES NOT call the on_change callback, as it is not possible to get the old state.

Source§

impl<S> State<S>
where S: Clone,

Source

pub fn get(&self) -> S

Returns a clone of the current state. This is particularly useful for States that implement Copy.

Source§

impl<S> State<S>
where S: PartialEq<S>,

Source

pub fn wait_for_state(&self, wait_for: S) -> StateFuture<S, impl Fn(&S) -> bool>

Returns a future that resolves when the state is equal to the given value.

Trait Implementations§

Source§

impl<S: Clone> Clone for State<S>

Source§

fn clone(&self) -> State<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug> Debug for State<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: Default> Default for State<S>

Source§

fn default() -> Self

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

impl<S, O> PartialEq<O> for State<S>
where S: PartialEq<O>,

Source§

fn eq(&self, other: &O) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S> Send for State<S>

Source§

impl<S> Sync for State<S>

Auto Trait Implementations§

§

impl<S> Freeze for State<S>

§

impl<S> !RefUnwindSafe for State<S>

§

impl<S> Unpin for State<S>

§

impl<S> !UnwindSafe for State<S>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.