Expand description
§tspawn
A thread-safe wrapper around Arc<RwLock<T>> with convenient cloning semantics and powerful async task spawning macros.
This crate provides a simple but powerful abstraction for sharing mutable state across async tasks
in Rust applications. It’s built on top of parking_lot::RwLock for better performance and
includes convenient macros for spawning tokio tasks with automatic cloning.
§Quick Start
use tspawn::{A, tspawn};
// Create a shared counter
let counter = A::new(0);
// Spawn a task that increments the counter
tspawn!(mut counter, {
*counter += 1;
println!("Counter incremented to: {}", *counter);
}).await?;
// Read the final value
println!("Final counter value: {}", counter.get());§Core Types
A<T>- The main thread-safe wrapper aroundArc<RwLock<T>>tspawn!- Macro for spawning tokio tasks with automatic cloning and lock management
§Features
- Thread-safe shared state: Built on
parking_lot::RwLockfor better performance - Convenient cloning: Clone the wrapper without explicit
Arc::clone()calls - Async task macros: Powerful
tspawn!macro for spawning tokio tasks - Multiple access patterns: Support for read-only, write-only, and mixed access
- No poisoning: Uses
parking_lotwhich doesn’t have lock poisoning
§Usage Patterns
§Basic Shared State
use tspawn::A;
let data = A::new(42);
// Read access
let value = data.get(); // Returns a clone of the inner value
let guard = data.read(); // Returns a read guard
// Write access
data.set(100); // Set a new value
data.update(|x| *x += 1); // Update using a closure§Async Task Spawning
use tspawn::{A, tspawn};
let data = A::new(vec![1, 2, 3]);
// Read-only access
tspawn!(ref data, {
println!("Data length: {}", data.len());
}).await?;
// Write access
tspawn!(mut data, {
data.push(4);
println!("Added element, new length: {}", data.len());
}).await?;Macros§
- tspawn
- Spawns a tokio task with automatic cloning and lock management for shared state.
Structs§
- A
- A thread-safe wrapper around
Arc<RwLock<T>>that provides convenient cloning semantics and easy access to the inner value.