| Crates.io | global_cell |
| lib.rs | global_cell |
| version | 0.2.0 |
| created_at | 2025-11-29 19:49:00.363064+00 |
| updated_at | 2025-11-29 19:49:00.363064+00 |
| description | A thread-safe, async-friendly, performant global cell alternative built on Rust and Tokio primitives |
| homepage | |
| repository | https://github.com/Bitfeller/global_cell |
| max_upload_size | |
| id | 1957351 |
| size | 92,311 |
A thread-safe, async-friendly global mutable cell implementation for Rust, built on primitives. Note: this crate uses nightly 2024 features to optimize performance.
Install the binary and utilize. The crate will be shortly published to crates.io!
use global_cell::Cell;
static MY_CELL: Cell<u32> = Cell::new();
fn main() {
// Set the cell value
MY_CELL.set(42);
// Read the value
MY_CELL.with(|value| {
println!("Value: {}", value);
});
}
use global_cell::Cell;
#[tokio::main]
async fn main() {
let cell = Cell::new();
cell.set("Hello".to_string());
// Get a write lock and modify
let mut write_lock = cell.write_async().await;
lock.push_str(" world");
drop(lock); // lock.drop() is also available as a shortcut
// Read the modified value
let read_lock = cell.read_async();
assert_eq!(*read_lcok, "Hello world");
}
with_mut for Modificationsuse global_cell::Cell;
fn main() {
let cell = Cell::new();
cell.set(vec![1, 2, 3]);
// Modify the value in place
cell.with_mut(|mut vec| {
vec.push(4);
});
}
use global_cell::Cell;
fn main() {
let cell = Cell::new();
cell.set(vec![1, 2, 3]);
let read_lock = cell.read();
println!("{}", lock[0]);
// Drop the lock so that we can acquire a write lock
drop(lock);
let mut write_lock = cell.write();
lock.push(4);
}
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 (requires T: Clone)copy() - Get a copy of the value (requires T: Copy)take() - for OptionNoneclear() - for Optionread() - Acquire a blocking read lockwrite() - Acquire a blocking write lockread_async() - Acquire an async read lockwrite_async() - Acquire an async write lockwith(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 lockis_initialized() - Check if the cell is inThis project is licensed under the MIT License or Apache License 2.0, at your option.