atomic-state

Crates.ioatomic-state
lib.rsatomic-state
version0.1.2
created_at2025-08-21 19:37:53.792876+00
updated_at2025-09-02 22:33:57.655467+00
descriptionThis library provides a static asynchronous data that can be accessed safely and concurrently from any part of your program
homepage
repositoryhttps://github.com/fuderis/rs-atomic-state
max_upload_size
id1805252
size26,612
Bulat Sh. (fuderis)

documentation

README

githubcrates-iodocs-rs

Atomic State

It's a Rust library designed to simplify working with static asynchronous data that can be accessed safely and concurrently from any part of your program. It provides convenient abstractions for creating atomic flags and state objects with asynchronous setters, getters, and locking mechanisms.

This library bridges the gap between synchronous static variables and asynchronous runtime environments, enabling seamless and safe manipulation of shared global state in asynchronous applications, such as those using Tokio or other async runtimes.

Features:

  • Define static atomic flags to represent simple boolean states.
  • Create static state wrappers around complex data structures.
  • Perform write operations with automatic synchronization.
  • Access and modify shared state safely using async locks.
  • Use ergonomic APIs for mapping and updating internal state asynchronously.
  • Stores data inside Arc links to avoid unnecessary cloning

It's ideal for applications that require global configuration, feature flags, or any kind of shared state accessible across asynchronous tasks without compromising thread safety or requiring complicated boilerplate code.

Examples:

Atomic Flag:

use atomic_state::prelude::*;

static IS_ACTIVE: Lazy<AtomFlag> = 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 atomic_state::prelude::*;

static CONFIG: Lazy<AtomState<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);
}

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!

Commit count: 10

cargo fmt