sovran-state
A lightweight, Redux-inspired state management library for Rust that encourages composable, granular state management. Unlike traditional Redux which uses a single state tree, sovran-state allows you to maintain multiple independent state containers, making it ideal for modular applications.
Features
- 🚀 Multiple state containers in a single store
- 🔒 Thread-safe state management
- 🎯 Type-safe actions and state updates
- 📦 Zero dependencies
- 🔄 Predictable state updates through actions
- 📢 Subscribe to specific state changes
- ⚡ Efficient action dispatching with FIFO ordering
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use ;
// Define your state
// Define an action
;
// Use the store
Why sovran-state?
While Redux is great for JavaScript applications, its pattern of using a single state tree doesn't always translate well to Rust applications. sovran-state takes the best parts of Redux (predictable state updates, action-based mutations) and adapts them to be more idiomatic in Rust:
- Multiple State Types: Instead of maintaining a giant state object, you can have multiple smaller, focused state containers.
- Type Safety: Leverage Rust's type system to ensure type-safe state updates and action handling.
- Thread Safety: Built with concurrency in mind, making it suitable for multi-threaded applications.
- Modular Design: Each state type is independent, making it easier to maintain and test different parts of your application.
Usage Examples
Managing Multiple States
use ;
// First state type
// Second state type
// Actions
;
;
// Usage
let store = new;
// Provide initial states
store.provide.unwrap;
store.provide.unwrap;
// Subscribe to specific state changes
store.subscribe.unwrap;
store.subscribe.unwrap;
// Dispatch actions
store.dispatch.unwrap;
store.dispatch.unwrap;
Thread-Safe State Management
use thread;
use ;
;
let store = new;
store.provide.unwrap;
let store_clone = store.clone;
let handle = spawn;
handle.join.unwrap;
Error Handling
The library uses a StoreError enum to handle various error cases:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License
Copyright (c) 2024 Sovran.la, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.