Crate this_state

source ·
Expand description

A library for managing state changes.

Examples

The examples below use the following state:

#[derive(Clone, Debug, PartialEq)]
enum MyState {
    A,
    B,
    C
}

Waiting for a state change

let state = State::new(MyState::A);

let state_clone = state.clone();
tokio::spawn(async move {
    // do some work
    state_clone.set(MyState::B);
    // do some more work
    state_clone.set(MyState::C);
});

state.wait_for_state(MyState::C).await;

assert_eq!(state.get(), MyState::C);

Structs

  • A thread-safe state, that can be used to share application state globally.
  • A future that completes when the state matches the given predicate. This is returned by State::wait_for.
  • A reference to the current state, returned by State::get_ref. It wraps a RwLockReadGuard and can be used to avoid cloning the state.
  • Error returned by State::set_on_change if there are multiple references to the state.