Skip to main content

Crate once_arc

Crate once_arc 

Source
Expand description

§once-arc

Initialize-once Arc<T> containers with zero-cost reads.

This crate provides two types for sharing data across threads where the value is written once and read many times:

  • OnceArc — a lock-free, CAS-based initialize-once slot. You construct the Arc<T> yourself and store it with an explicit Ordering.

  • InitOnceArc — synchronized one-time initialization via a closure (similar to OnceLock). The closure runs exactly once; its return value becomes the stored Arc<T>.

Both types share the same fast path: once the value is set, get() is a single atomic load — no locking, no CAS, no reference-count manipulation. On x86 this compiles to a plain mov.

§Why not OnceLock<Arc<T>>?

OnceLock stores its value inline, so get() returns &Arc<T> — two pointer indirections to reach T. Here, the atomic is the Arc’s pointer, so get() returns &T directly.

§Examples

use std::sync::Arc;
use std::sync::atomic::Ordering;
use once_arc::OnceArc;

let slot = OnceArc::new();
slot.store(Arc::new(42), Ordering::Release).unwrap();
assert_eq!(slot.get(Ordering::Acquire), Some(&42));
use std::sync::Arc;
use std::sync::atomic::Ordering;
use once_arc::InitOnceArc;

let cell = InitOnceArc::new();
cell.init(|| Arc::new("hello")).unwrap();
assert_eq!(cell.get(Ordering::Acquire), Some(&"hello"));

Structs§

InitOnceArc
A thread-safe container that provides synchronized one-time initialization of an Arc<T> via a closure.
OnceArc
A thread-safe container that can be atomically initialized once with an Arc<T>.