#state #mutable #applications

app-state

Thread-safe, mutable application states for rust

1 unstable release

0.1.0 Sep 15, 2023

#72 in #mutable

Download history 17/week @ 2024-07-19 3/week @ 2024-07-26 2/week @ 2024-08-02 47/week @ 2024-08-09 324/week @ 2024-08-16 178/week @ 2024-08-23 141/week @ 2024-08-30 47/week @ 2024-09-06 35/week @ 2024-09-13 51/week @ 2024-09-20 34/week @ 2024-09-27 55/week @ 2024-10-04 78/week @ 2024-10-11 51/week @ 2024-10-18 18/week @ 2024-10-25 39/week @ 2024-11-01

191 downloads per month

Custom license

28KB
690 lines

Thread-safe, mutable application states for rust.

Examples

Initializing the state

Any app state that may be used must be initialized first. Note: An AppState can not be used as a MutAppState and vice versa. This means that AppState and MutAppState must be initialized separately and use independent values, even if they are of the same type.

use app_state::{AppState, MutAppState, AppStateTrait};

struct MyState {
 counter: u32,
}

fn main() {
  // Initialize the app state
  AppState::init(MyState { counter: 0 });
  // Initialize the mutable app state
  MutAppState::init(MyState { counter: 0 });
}

Using derive

In order to avoid boilerplate code, the InitAppState and InitMutAppState traits can be derived for any struct. These traits provide the init_app_state and init_mut_app_state methods respectively which can be used to initialize the state more easily.

use app_state::{AppState, MutAppState, AppStateTrait, InitAppState, InitMutAppState};

#[derive(Default, InitAppState, InitMutAppState)]
struct MyState {
  counter: u32,
}

fn main() {
  MyState::default().init_app_state();
  MyState::default().init_mut_app_state();
}

Read-only state

App states internally use Arc to allow for thread-safe access.

use app_state::{AppState, AppStateTrait, stateful};

struct MyState {
  counter: u32,
}

#[stateful]
fn func(state: AppState<MyState>) {
  println!("Counter: {}", state.counter);
}

Mutable state

Mutable states internally use a Mutex to ensure thread-safety. This means that when reading from or writing to the state, the mutex must be locked. This can be done either by calling get_mut() or by using the MutAppStateLock type.

use app_state::{MutAppState, AppStateTrait, stateful};

struct MyState {
  counter: u32,
}

#[stateful]
fn func(state: MutAppState<MyState>) {
  let mut state = state.get_mut();
  state.counter += 1;
}

Mutable state (locked)

In order to mutate the state, you must first lock it. This can be done either by calling get_mut() or by using the MutAppStateLock type.

use app_state::{MutAppState, MutAppStateLock, AppStateTrait, stateful};

struct MyState {
  counter: u32,
}

#[stateful]
fn func(mut state: MutAppStateLock<MyState>) {
  state.counter += 1;
}

Get the state manually

You can also get the state manually by calling AppState::get() or MutAppState::get().

use app_state::{AppState, MutAppState, AppStateTrait};

struct MyState {
  counter: u32,
}

fn main() {
  let state = AppState::<MyState>::get();
  let mut_state = MutAppState::<MyState>::get();
}

Dependencies

~0.5–1MB
~22K SLoC