Struct this_state::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 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 copy 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> !RefUnwindSafe for State<S>

§

impl<S> Unpin for State<S>

§

impl<S> !UnwindSafe for State<S>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.