Global Cell
A thread-safe, async-friendly global mutable cell implementation for Rust, built on primitives. Note: this crate uses nightly 2024 features to optimize performance.
Features
- Thread-safe: Built to be shared and accessed quickly between threads
- Async-friendly: Designed on async/await patterns with most async platforms
- Global state: Can be used as a static global variable, especially with the featured OnceCell
- Easy mutability: Easily read/write access ensures type + cell safety at compile-time
Installation
Install the binary and utilize. The crate will be shortly published to crates.io!
Usage
Basic Usage
use Cell;
static MY_CELL: = new;
Working with Mutable Data (+ async)
use Cell;
async
Using with_mut for Modifications
use Cell;
Using locks to capture and/or modify
use Cell;
API Overview (for most cell types, including Cell)
Cell Operations
new()- (const) Create a new empty cellfrom(value)- (const) Create a cell with an initial valueset(value)- Set or update the cell valuecloned()- Get a clone of the value (requiresT: Clone)copy()- Get a copy of the value (requiresT: Copy)take()- for Option: take the inner value, leavingNoneclear()- for Option: clear the cell value
Locking
read()- Acquire a blocking read lockwrite()- Acquire a blocking write lockread_async()- Acquire an async read lockwrite_async()- Acquire an async write lock
Functional Access
with(func)- Apply a function to the valuewith_mut(func)- Apply a funitializedis_init()- synonym for is_initialized ction that requires a write lock to the valuewith_async(func)- Apply an async function to the value, acquiring an async lockwith_mut_async(func)- Apply an async function that requires a write lock to the value, acquiring an async lock
Status
is_initialized()- Check if the cell is in
License
This project is licensed under the MIT License or Apache License 2.0, at your option.