atomic_struct
atomic_struct is a Rust proc-macro that allows you to create structs with atomic, asynchronous fields. Each field is automatically wrapped in an AtomicMember<T> (Arc<Mutex<T>), providing thread-safe async access to individual fields.
Features
#[atomic_struct]attribute macro for easy struct creation- Each field is wrapped in
AtomicMember<T> - Automatically generated
new()constructor - Async getter/setter for each field
- Cloneable atomic fields
- Optional: Serde field attributes are preserved (
#[serde(...)])
Installation
Add the crate to your Cargo.toml:
[]
= { = "../atomic_struct" } # or crates.io version when published
= { = "1", = ["macros", "rt-multi-thread"] }
Examples
use atomic_struct;
async
Advanced Usage
Serde Support
If you add Serde field attributes like #[serde(rename = "…")] or #[serde(skip)] to the original fields, they will automatically be preserved.
Global Singleton Usage
atomic_struct structs can be safely stored in Arc or OnceCell:
use OnceCell;
static GLOBAL_STATE: = new;
Benefits
- Thread-safe and async by default
- Minimal boilerplate
- Cloneable fields for parallel tasks
- Automatic getters and setters