| Crates.io | state-flux-mini |
| lib.rs | state-flux-mini |
| version | 0.1.0 |
| created_at | 2025-10-05 08:21:36.548887+00 |
| updated_at | 2025-10-05 08:21:36.548887+00 |
| description | A minimal state management crate for Rust |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1868779 |
| size | 73,816 |
A minimal, lightweight state-management crate for Rust providing lock-free reads, cheap atomic updates and async notifications to subscribers.
T: PartialEq for changed / try_changed).Add the crate to your Cargo.toml (example uses the local crate name state-flux-mini):
[dependencies]
state-flux-mini = "0.1"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
Create and use a State:
use state_flux_mini::State;
use std::sync::Arc;
let state = Arc::new(State::new(0usize));
// read (returns Arc<T>)
let val = state.get();
println!("value = {}", *val);
// write (replaces stored value and notifies subscribers)
state.set(1usize);
Subscribe and await changes (async):
use state_flux_mini::State;
use tokio::task;
#[tokio::main]
async fn main() {
let state = State::new(0usize);
let mut sub = state.subscribe();
// spawn a watcher
let watcher = task::spawn({
let mut sub = sub.clone();
async move {
let new = sub.changed().await; // requires T: PartialEq
println!("got change -> {}", *new);
}
});
// trigger change
state.set(2usize);
watcher.await.unwrap();
}
Try non-blocking check:
let mut sub = state.subscribe();
if let Some(new) = sub.try_changed() {
println!("changed -> {}", *new);
}
Peek current value without affecting the subscriber:
let cur = sub.peek();
println!("current -> {}", *cur);
State
Subscriber
Notes:
PartialEq) to detect meaningful changes.The repository contains Criterion benchmarks (see benches/state.rs) that measure:
Run with:
cargo bench
(benchmarks require the criterion dev-dependency and a tokio runtime for the async bench.)
GNU GENERAL PUBLIC LICENSE V3 (pick whichever fits your repo). Contributions and issues welcome — keep changes small and focused.