#atomic #reference #static #order

atomic_ref

Atomic &'a T types with support for static allocation

4 releases

Uses old Rust 2015

0.2.1 Sep 19, 2021
0.2.0 Nov 1, 2020
0.1.2 Nov 4, 2016
0.1.1 Sep 3, 2016
0.1.0 Sep 2, 2016

#111 in #order

Download history 26/week @ 2024-03-11 24/week @ 2024-03-18 25/week @ 2024-03-25 34/week @ 2024-04-01 45/week @ 2024-04-08 104/week @ 2024-04-15 19/week @ 2024-04-22 28/week @ 2024-04-29 25/week @ 2024-05-06 38/week @ 2024-05-13 102/week @ 2024-05-20 64/week @ 2024-05-27 109/week @ 2024-06-03 54/week @ 2024-06-10 85/week @ 2024-06-17 81/week @ 2024-06-24

329 downloads per month
Used in 2 crates

MIT license

18KB
161 lines

AtomicRef

Build Status

An atomic optional reference with support for static allocation.

See the documentation for more information.


lib.rs:

Atomic References

These types act similarially to the Atomic types from std::sync::atomic, Except that instead of containing an integer type or a pointer, they contain an Option<&'a T> value.

Like other option values, these types present operations which, when used correctly, synchronize updates between threads. This type is a form of interior mutability, like Cell<T>, RefCell<T>, or Mutex<T>.

This type in static position is often used for lazy global initialization.

AtomicRef may only contain Sized types, as unsized types have wide pointers which cannot be atomically written to or read from.

Ordering

It is unsound to load or store an atomic reference with the Relaxed memory ordering, as these operations provide no ordering on writes to the data behind the reference. To avoid this issue, loads and stores with Relaxed memory ordering are actually performed with Acquire, Release, or AcqRel ordering, as appropriate.

Examples

Static logger state

use atomic_ref::AtomicRef;
use std::sync::atomic::Ordering;
use std::io::{stdout, Write};

// Define the idea of a logger
trait Logger {
    fn log(&self, msg: &str) {}
}
struct LoggerInfo {
    logger: &'static (dyn Logger + Sync)
}

// The methods for working with our currently defined static logger
static LOGGER: AtomicRef<LoggerInfo> = AtomicRef::new(None);
fn log(msg: &str) -> bool {
    if let Some(info) = LOGGER.load(Ordering::SeqCst) {
        info.logger.log(msg);
        true
    } else {
        false
    }
}
fn set_logger(logger: Option<&'static LoggerInfo>) {
    LOGGER.store(logger, Ordering::SeqCst);
}

// Defining the standard out example logger
struct StdoutLogger;
impl Logger for StdoutLogger {
    fn log(&self, msg: &str) {
        stdout().write(msg.as_bytes());
    }
}
static STDOUT_LOGGER: LoggerInfo = LoggerInfo { logger: &StdoutLogger };

fn main() {
    let res = log("This will fail");
    assert!(!res);
    set_logger(Some(&STDOUT_LOGGER));
    let res = log("This will succeed");
    assert!(res);
}

No runtime deps