Skip to main content

Crate rustand

Crate rustand 

Source
Expand description

§rustand

A lightweight, Zustand-inspired state management library for Rust.

rustand provides a simple, single-threaded way to manage shared state with subscription support. It is designed to be lean, dependency-free, and easy to use.

§Core Features

  • Simple API: Minimalist get, set, and subscribe pattern.
  • Infallible: API methods don’t return Result, making usage clean and idiomatic.
  • Zero Dependencies: Core library has no external dependencies.

§Feature Flags

  • single-threaded (default): Optimized for WASM or single-threaded environments. Uses Rc and RefCell to avoid atomic overhead.
  • wasm: Alias for single-threaded, targeting WebAssembly environments.

§Quick Start

use rustand::Store;

let store = Store::new(0);

// Subscribe to changes
let _sub = store.subscribe(|v| println!("New value: {}", v));

// Update state
store.set(|s| *s += 1);

// Get state
assert_eq!(store.get(), 1);

Structs§

Store
A state container optimized for single-threaded usage.
Subscription
A handle to an active subscription.

Type Aliases§

SubscriberCallback
Callback function type for subscribers.
SubscriberId
Unique identifier for a subscriber.

Attribute Macros§

global_store
Generates a thread_local! global store for the decorated struct. The struct must implement Default. It provides a ::store() associated function to easily retrieve the store.
store_actions
Allows writing an impl Store<State> block directly. This macro rewrites it into an Extension Trait to satisfy Rust’s orphan rules, allowing seamless store.method() syntax.