#state #async #thread-safe #wait #store #manner #enums

this-state

this-state provides a way to store state in a thread-safe manner as well as a way to asynchronously wait for state changes

4 releases (2 breaking)

0.3.0 Dec 17, 2023
0.2.0 Jun 11, 2023
0.1.1 Jun 11, 2023
0.1.0 Jun 11, 2023

#850 in Asynchronous

ISC license

39KB
752 lines

this-state

this-state provides a way to store state in a thread-safe manner as well as a way to asynchronously wait for state changes.

Examples

The example below uses 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);

lib.rs:

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
    # tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    state_clone.set(MyState::B);
    // do some more work
    # tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    state_clone.set(MyState::C);
});

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

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

Dependencies

~0.4–5MB
~11K SLoC