dry run publish
statewatcher
A simple, shared state channel where readers are notified of updates, inspired by tokio::watch but for std.
Features
- Zero dependencies - Pure
stdimplementation - Simple API - Easy to understand and use
- Thread-safe - Built on
ArcandRwLock/Mutex - Lightweight - Minimal overhead using atomic flags for notifications
- Optional mutex mode - Use
Mutexinstead ofRwLockfor read-write access
Why statewatcher?
A simple way to share state between threads with change notifications but without need to pull in Tokio or other async runtimes.
- Event-driven applications using
std::thread - Configuration updates across threads
- State synchronization in multi-threaded applications
- Simple pub-sub patterns without async complexity
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Example
use state_channel;
Multiple Readers
use state_channel;
let = ;
let reader2 = reader1.clone;
let reader3 = reader1.clone;
writer.update;
// All readers see the update
assert_eq!;
assert_eq!;
assert_eq!;
Accessing State Without Cloning
use state_channel;
let = ;
writer.update;
// Access state without cloning
let sum = reader.with_state;
println!;
Mutex Mode (Read-Write Handle)
Enable the mutex feature for a single handle that can both read and write:
[]
= { = "0.1.0", = ["mutex"] }
use state_readerwriter;