pub struct RelaxedAtomic<T: AtomicRepr> { /* private fields */ }Expand description
Provides inner mutability for Copy types via relaxed atomic operations.
On x86_64 and ARM, relaxed loads and stores compile to the same instructions
as regular memory accesses — no LOCK prefix is emitted. This makes
RelaxedAtomic a zero-overhead way to achieve interior mutability without
the bus-lock cost of fetch_* or CAS operations.
Deliberately exposes only load and store. The fetch_* methods are
omitted because they emit LOCK-prefixed instructions with measurable
overhead. Instead, use the load–mutate–store pattern:
let counter = RelaxedAtomic::new(0u32);
let val = counter.load();
counter.store(val + 1);§When to use
Use when a field needs interior mutability and is accessed without
contention (same pattern as the original C code using plain loads/stores).
If you need multi-step atomic operations (CAS, fetch_add), use the
underlying std::sync::atomic types directly.
§When not to use
Do not use when the operation must be atomic relative to other threads. The load–mutate–store pattern is not atomic as a whole — it can race with concurrent stores. Use only where the C code would have used a non-atomic access that happens to be race-free by design.
Implementations§
Source§impl<T: AtomicRepr> RelaxedAtomic<T>
impl<T: AtomicRepr> RelaxedAtomic<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the atomic and return the inner value.
Source§impl RelaxedAtomic<u32>
impl RelaxedAtomic<u32>
Sourcepub fn fetch_add(&self, val: u32) -> u32
pub fn fetch_add(&self, val: u32) -> u32
Atomic add with relaxed ordering.
Returns the previous value. Under Relaxed ordering this does not emit
a LOCK-prefixed bus cycle on x86_64 or ARMv8 — it compiles to a
plain locked add that implements a single-copy atomic RMW without
the costly bus-lock side effects of stronger orderings.
Trait Implementations§
Source§impl<T: AtomicRepr> Clone for RelaxedAtomic<T>
impl<T: AtomicRepr> Clone for RelaxedAtomic<T>
Source§impl<T: Debug + AtomicRepr> Debug for RelaxedAtomic<T>
impl<T: Debug + AtomicRepr> Debug for RelaxedAtomic<T>
Source§impl<T: AtomicRepr + Default> Default for RelaxedAtomic<T>
impl<T: AtomicRepr + Default> Default for RelaxedAtomic<T>
Source§impl<T: AtomicRepr + Display> Display for RelaxedAtomic<T>
impl<T: AtomicRepr + Display> Display for RelaxedAtomic<T>
Source§impl<T: AtomicRepr + PartialEq> PartialEq for RelaxedAtomic<T>
Note: this performs two separate relaxed loads. Under concurrent writes
the two values may come from different points in time. Use this only
for diagnostic assertions — never for correctness-critical decisions.
impl<T: AtomicRepr + PartialEq> PartialEq for RelaxedAtomic<T>
Note: this performs two separate relaxed loads. Under concurrent writes the two values may come from different points in time. Use this only for diagnostic assertions — never for correctness-critical decisions.
impl<T: AtomicRepr + Eq> Eq for RelaxedAtomic<T>
Auto Trait Implementations§
impl<T> Freeze for RelaxedAtomic<T>
impl<T> RefUnwindSafe for RelaxedAtomic<T>
impl<T> Send for RelaxedAtomic<T>
impl<T> Sync for RelaxedAtomic<T>
impl<T> Unpin for RelaxedAtomic<T>
impl<T> UnsafeUnpin for RelaxedAtomic<T>
impl<T> UnwindSafe for RelaxedAtomic<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read moreSource§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.