
Atoman
Atoman is a Rust library for safe concurrent access to static asynchronous data across your application. It provides atomic flags and state wrappers with async setters, getters, and locking mechanisms, bridging synchronous static variables with async runtimes like Tokio.
Features:
- Global configuration management with file-based persistence (
TOML/JSON).
- Structured logging with async-safe output to files.
- Feature flags and shared state in async applications.
- Data is stored in
Arc for zero-copy access and thread safety.
Examples:
Atomic Flag:
use atoman::prelude::*;
static IS_ACTIVE: Lazy<Flag> = lazy_flag!(false);
#[tokio::main]
async fn main() {
assert_eq!(*IS_ACTIVE, false);
assert!(IS_ACTIVE.is_false());
IS_ACTIVE.set(true);
assert_eq!(*IS_ACTIVE, true);
IS_ACTIVE.swap(false).await;
assert_eq!(*IS_ACTIVE, false);
}
Atomic State:
use atoman::prelude::*;
static CONFIG: Lazy<State<Config>> = lazy_state!(
Config {
count: 0,
}
);
#[derive(Debug, Clone)]
pub struct Config {
pub count: i32,
}
fn main() {
CONFIG.set(Config { count: 10, });
assert_eq!(CONFIG.get().count, 10);
CONFIG.map(|cfg| cfg.count = 20);
assert_eq!(CONFIG.get().count, 20);
CONFIG.lock().count = 30;
assert_eq!(CONFIG.get().count, 30);
}
Config:
use atoman::prelude::*;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct Person {
name: String,
age: u32
}
impl Default for Person {
fn default() -> Self {
Self {
name: "Bob".to_owned(),
age: 23
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
let cfg = Config::<Person>::new(".test/person.toml")?;
assert_eq!(cfg.get().name, "Bob");
assert_eq!(cfg.get().age, 23);
cfg.lock().name = 24;
assert_eq!(cfg.get().age, 24);
Ok(())
}
Logger:
use atoman::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
Logger::init(".test/logs", 20)?;
info!("Hello, World!");
Ok(())
}
Feedback:
This library distributed under the MIT license.
You can contact me via GitHub or send a message to my telegram @fuderis.
This library is actively evolving, and your suggestions and feedback are always welcome!