Skip to main content

Atomic

Struct Atomic 

Source
pub struct Atomic<T>
where T: AtomicPrimitive,
{ /* private fields */ }
๐Ÿ”ฌThis is a nightly-only experimental API. (generic_atomic)
Expand description

A memory location which can be safely modified from multiple threads.

This has the same size and bit validity as the underlying type T. However, the alignment of this type is always equal to its size, even on targets where T has alignment less than its size.

For more about the differences between atomic types and non-atomic types as well as information about the portability of this type, please see the module-level documentation.

Note: This type is only available on platforms that support atomic loads and stores of T.

Implementationsยง

Sourceยง

impl Atomic<bool>

1.0.0 (const: 1.24.0) ยท Source

pub const fn new(v: bool) -> Atomic<bool>

Creates a new AtomicBool.

ยงExamples
use std::sync::atomic::AtomicBool;

let atomic_true = AtomicBool::new(true);
let atomic_false = AtomicBool::new(false);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut bool) -> &'a Atomic<bool>

Creates a new AtomicBool from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicBool};

// Get a pointer to an allocated value
let ptr: *mut bool = Box::into_raw(Box::new(false));

assert!(ptr.cast::<AtomicBool>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe { AtomicBool::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(true, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, true);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicBool>() (note that this is always true, since align_of::<AtomicBool>() == 1).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut bool) -> *const Atomic<bool>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to AtomicBool from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.15.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut bool

Returns a mutable reference to the underlying bool.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let mut some_bool = AtomicBool::new(true);
assert_eq!(*some_bool.get_mut(), true);
*some_bool.get_mut() = false;
assert_eq!(some_bool.load(Ordering::SeqCst), false);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut bool) -> &mut Atomic<bool>

Gets atomic access to a &mut bool.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let mut some_bool = true;
let a = AtomicBool::from_mut(&mut some_bool);
a.store(false, Ordering::Relaxed);
assert_eq!(some_bool, false);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<bool>]) -> &mut [bool]

Gets non-atomic access to a &mut [AtomicBool] slice.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicBool, Ordering};

let mut some_bools = [const { AtomicBool::new(false) }; 10];

let view: &mut [bool] = AtomicBool::get_mut_slice(&mut some_bools);
assert_eq!(view, [false; 10]);
view[..5].copy_from_slice(&[true; 5]);

std::thread::scope(|s| {
    for t in &some_bools[..5] {
        s.spawn(move || assert_eq!(t.load(Ordering::Relaxed), true));
    }

    for f in &some_bools[5..] {
        s.spawn(move || assert_eq!(f.load(Ordering::Relaxed), false));
    }
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [bool]) -> &mut [Atomic<bool>]

Gets atomic access to a &mut [bool] slice.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicBool, Ordering};

let mut some_bools = [false; 10];
let a = &*AtomicBool::from_mut_slice(&mut some_bools);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(true, Ordering::Relaxed));
    }
});
assert_eq!(some_bools, [true; 10]);
1.15.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> bool

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.into_inner(), true);
1.0.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> bool

Loads a value from the bool.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.load(Ordering::Relaxed), true);
Source

pub const unsafe fn load_volatile( self: *const Atomic<bool>, order: Ordering, ) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile atomic load from the bool.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • Reading from self must produce a properly initialized value of type bool.

ยงPanics

Panics if order is Release or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn store(&self, val: bool, order: Ordering)

Stores a value into the bool.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let some_bool = AtomicBool::new(true);

some_bool.store(false, Ordering::Relaxed);
assert_eq!(some_bool.load(Ordering::Relaxed), false);
Source

pub const unsafe fn store_volatile( self: *const Atomic<bool>, val: bool, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile atomic store into the bool.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:
    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
ยงPanics

Panics if order is Acquire or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn swap(&self, val: bool, order: Ordering) -> bool

Stores a value into the bool, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.swap(false, Ordering::Relaxed), true);
assert_eq!(some_bool.load(Ordering::Relaxed), false);
1.0.0 (const: unstable) ยท Source

pub fn compare_and_swap( &self, current: bool, new: bool, order: Ordering, ) -> bool

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the bool if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.compare_and_swap(true, false, Ordering::Relaxed), true);
assert_eq!(some_bool.load(Ordering::Relaxed), false);

assert_eq!(some_bool.compare_and_swap(true, true, Ordering::Relaxed), false);
assert_eq!(some_bool.load(Ordering::Relaxed), false);
1.10.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>

Stores a value into the bool if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.compare_exchange(true,
                                      false,
                                      Ordering::Acquire,
                                      Ordering::Relaxed),
           Ok(true));
assert_eq!(some_bool.load(Ordering::Relaxed), false);

assert_eq!(some_bool.compare_exchange(true, true,
                                      Ordering::SeqCst,
                                      Ordering::Acquire),
           Err(false));
assert_eq!(some_bool.load(Ordering::Relaxed), false);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. In this case, compare_exchange can lead to the ABA problem.

1.10.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>

Stores a value into the bool if the current value is the same as the current value.

Unlike AtomicBool::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let val = AtomicBool::new(false);

let new = true;
let mut old = val.load(Ordering::Relaxed);
loop {
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. In this case, compare_exchange can lead to the ABA problem.

1.0.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: bool, order: Ordering) -> bool

Logical โ€œandโ€ with a boolean value.

Performs a logical โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), false);

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_and(true, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_and(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), false);
1.0.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: bool, order: Ordering) -> bool

Logical โ€œnandโ€ with a boolean value.

Performs a logical โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_nand(true, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst) as usize, 0);
assert_eq!(foo.load(Ordering::SeqCst), false);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_nand(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), true);
1.0.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: bool, order: Ordering) -> bool

Logical โ€œorโ€ with a boolean value.

Performs a logical โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_or(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_or(true, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), true);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_or(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), false);
1.0.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool

Logical โ€œxorโ€ with a boolean value.

Performs a logical โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), true);

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_xor(true, Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), false);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_xor(false, Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), false);
1.81.0 (const: unstable) ยท Source

pub fn fetch_not(&self, order: Ordering) -> bool

Logical โ€œnotโ€ with a boolean value.

Performs a logical โ€œnotโ€ operation on the current value, and sets the new value to the result.

Returns the previous value.

fetch_not takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let foo = AtomicBool::new(true);
assert_eq!(foo.fetch_not(Ordering::SeqCst), true);
assert_eq!(foo.load(Ordering::SeqCst), false);

let foo = AtomicBool::new(false);
assert_eq!(foo.fetch_not(Ordering::SeqCst), false);
assert_eq!(foo.load(Ordering::SeqCst), true);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut bool

Returns a mutable pointer to the underlying bool.

Doing non-atomic reads and writes on the resulting boolean can be a data race. This method is mostly useful for FFI, where the function signature may use *mut bool instead of &AtomicBool.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicBool;

extern "C" {
    fn my_atomic_op(arg: *mut bool);
}

let mut atomic = AtomicBool::new(true);
unsafe {
    my_atomic_op(atomic.as_ptr());
}
1.53.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<bool, bool>
where F: FnMut(bool) -> Option<bool>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicBool::try_update.

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(bool) -> Option<bool>, ) -> Result<bool, bool>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicBool::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem.

ยงExamples
use std::sync::atomic::{AtomicBool, Ordering};

let x = AtomicBool::new(false);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(false));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(!x)), Ok(false));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(!x)), Ok(true));
assert_eq!(x.load(Ordering::SeqCst), false);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(bool) -> bool, ) -> bool

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicBool::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem.

ยงExamples

use std::sync::atomic::{AtomicBool, Ordering};

let x = AtomicBool::new(false);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| !x), false);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| !x), true);
assert_eq!(x.load(Ordering::SeqCst), false);
Sourceยง

impl<T> Atomic<*mut T>

1.0.0 (const: 1.24.0) ยท Source

pub const fn new(p: *mut T) -> Atomic<*mut T>

Creates a new AtomicPtr.

ยงExamples
use std::sync::atomic::AtomicPtr;

let ptr = &mut 5;
let atomic_ptr = AtomicPtr::new(ptr);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut *mut T) -> &'a Atomic<*mut T>

Creates a new AtomicPtr from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicPtr};

// Get a pointer to an allocated value
let ptr: *mut *mut u8 = Box::into_raw(Box::new(std::ptr::null_mut()));

assert!(ptr.cast::<AtomicPtr<u8>>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe { AtomicPtr::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(std::ptr::NonNull::dangling().as_ptr(), atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert!(!unsafe { *ptr }.is_null());

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicPtr<T>>() (note that on some platforms this can be bigger than align_of::<*mut T>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut *mut T) -> *const Atomic<*mut T>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to AtomicPtr from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

Source

pub const fn null() -> Atomic<*mut T>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_ptr_null)

Creates a new AtomicPtr initialized with a null pointer.

ยงExamples
#![feature(atomic_ptr_null)]
use std::sync::atomic::{AtomicPtr, Ordering};

let atomic_ptr = AtomicPtr::<()>::null();
assert!(atomic_ptr.load(Ordering::Relaxed).is_null());
1.15.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut *mut T

Returns a mutable reference to the underlying pointer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let mut data = 10;
let mut atomic_ptr = AtomicPtr::new(&mut data);
let mut other_data = 5;
*atomic_ptr.get_mut() = &mut other_data;
assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut *mut T) -> &mut Atomic<*mut T>

Gets atomic access to a pointer.

Note: This function is only available on targets where AtomicPtr<T> has the same alignment as *const T

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let mut data = 123;
let mut some_ptr = &mut data as *mut i32;
let a = AtomicPtr::from_mut(&mut some_ptr);
let mut other_data = 456;
a.store(&mut other_data, Ordering::Relaxed);
assert_eq!(unsafe { *some_ptr }, 456);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<*mut T>]) -> &mut [*mut T]

Gets non-atomic access to a &mut [AtomicPtr] slice.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::ptr::null_mut;
use std::sync::atomic::{AtomicPtr, Ordering};

let mut some_ptrs = [const { AtomicPtr::new(null_mut::<String>()) }; 10];

let view: &mut [*mut String] = AtomicPtr::get_mut_slice(&mut some_ptrs);
assert_eq!(view, [null_mut::<String>(); 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(i, ptr)| *ptr = Box::into_raw(Box::new(format!("iteration#{i}"))));

std::thread::scope(|s| {
    for ptr in &some_ptrs {
        s.spawn(move || {
            let ptr = ptr.load(Ordering::Relaxed);
            assert!(!ptr.is_null());

            let name = unsafe { Box::from_raw(ptr) };
            println!("Hello, {name}!");
        });
    }
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [*mut T]) -> &mut [Atomic<*mut T>]

Gets atomic access to a slice of pointers.

Note: This function is only available on targets where AtomicPtr<T> has the same alignment as *const T

ยงExamples
โ“˜
use std::ptr::null_mut;
use std::sync::atomic::{AtomicPtr, Ordering};

let mut some_ptrs = [null_mut::<String>(); 10];
let a = &*AtomicPtr::from_mut_slice(&mut some_ptrs);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || {
            let name = Box::new(format!("thread{i}"));
            a[i].store(Box::into_raw(name), Ordering::Relaxed);
        });
    }
});
for p in some_ptrs {
    assert!(!p.is_null());
    let name = unsafe { Box::from_raw(p) };
    println!("Hello, {name}!");
}
1.15.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> *mut T

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicPtr;

let mut data = 5;
let atomic_ptr = AtomicPtr::new(&mut data);
assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);
1.0.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> *mut T

Loads a value from the pointer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let value = some_ptr.load(Ordering::Relaxed);
Source

pub const unsafe fn load_volatile( self: *const Atomic<*mut T>, order: Ordering, ) -> *mut T

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile atomic load from the pointer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicPtr<T>>() (note that on some platforms this can be bigger than align_of::<*mut T>()).

  • Reading from self must produce a properly initialized value of type *mut T.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples

Assuming an MMIO region at MMIO_ADDR that belongs to a device with direct memory access, we may receive a buffer in shared memory from that device as follows:

#![feature(atomic_volatile)]
use std::sync::atomic::{fence, AtomicPtr, Ordering};
use std::ptr;

const MMIO_ADDR: *mut *mut u8 = ptr::without_provenance_mut(0xCAF0);
let atomic_ptr = AtomicPtr::<u8>::from_ptr_raw(MMIO_ADDR);

// Spin until we see a non-zero value.
let buf = 'buf: loop {
    let buf = unsafe { atomic_ptr.load_volatile(Ordering::Relaxed) };
    if !buf.is_null() {
        break 'buf buf;
    }
};
// Synchronize with the store whose value we just read.
// Note: a standard acquire fence may not be sufficient to synchronize with DMA devices.
// Depending on your target, you may have to use inline assembly to emit a special fence.
fence(Ordering::Acquire);

// Now process the data in `buf`.
1.0.0 (const: unstable) ยท Source

pub fn store(&self, ptr: *mut T, order: Ordering)

Stores a value into the pointer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let other_ptr = &mut 10;

some_ptr.store(other_ptr, Ordering::Relaxed);
Source

pub const unsafe fn store_volatile( self: *const Atomic<*mut T>, ptr: *mut T, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile atomic store into the pointer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicPtr<T>>() (note that on some platforms this can be bigger than align_of::<*mut T>()).

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples

Assuming an MMIO region at MMIO_ADDR that belongs to a device with direct memory access, we may submit a buffer in shared memory to that device as follows:

#![feature(atomic_volatile)]
use std::sync::atomic::{fence, AtomicPtr, Ordering};
use std::ptr;

const MMIO_ADDR: *mut *mut u8 = ptr::without_provenance_mut(0xCAF0);
let atomic_ptr = AtomicPtr::<u8>::from_ptr_raw(MMIO_ADDR);

// Prepare some data for the DMA device.
let buf = get_dma_buffer();

// Ensure the other side can synchronize with the store we do below.
// Note: a standard release fence may not be sufficient to synchronize with DMA devices.
// Depending on your target, you may have to use inline assembly to emit a special fence.
fence(Ordering::Release);

unsafe { atomic_ptr.store_volatile(buf, Ordering::Relaxed) };
1.0.0 (const: unstable) ยท Source

pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T

Stores a value into the pointer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let other_ptr = &mut 10;

let value = some_ptr.swap(other_ptr, Ordering::Relaxed);
1.0.0 ยท Source

pub fn compare_and_swap( &self, current: *mut T, new: *mut T, order: Ordering, ) -> *mut T

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the pointer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let other_ptr = &mut 10;

let value = some_ptr.compare_and_swap(ptr, other_ptr, Ordering::Relaxed);
1.10.0 ยท Source

pub fn compare_exchange( &self, current: *mut T, new: *mut T, success: Ordering, failure: Ordering, ) -> Result<*mut T, *mut T>

Stores a value into the pointer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let other_ptr = &mut 10;

let value = some_ptr.compare_exchange(ptr, other_ptr,
                                      Ordering::SeqCst, Ordering::Relaxed);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.10.0 ยท Source

pub fn compare_exchange_weak( &self, current: *mut T, new: *mut T, success: Ordering, failure: Ordering, ) -> Result<*mut T, *mut T>

Stores a value into the pointer if the current value is the same as the current value.

Unlike AtomicPtr::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let some_ptr = AtomicPtr::new(&mut 5);

let new = &mut 10;
let mut old = some_ptr.load(Ordering::Relaxed);
loop {
    match some_ptr.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.53.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<*mut T, *mut T>
where F: FnMut(*mut T) -> Option<*mut T>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicPtr::try_update.

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(*mut T) -> Option<*mut T>, ) -> Result<*mut T, *mut T>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicPtr::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem, which is a particularly common pitfall for pointers!

ยงExamples
use std::sync::atomic::{AtomicPtr, Ordering};

let ptr: *mut _ = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let new: *mut _ = &mut 10;
assert_eq!(some_ptr.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(ptr));
let result = some_ptr.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| {
    if x == ptr {
        Some(new)
    } else {
        None
    }
});
assert_eq!(result, Ok(ptr));
assert_eq!(some_ptr.load(Ordering::SeqCst), new);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(*mut T) -> *mut T, ) -> *mut T

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicPtr::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on pointers.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem, which is a particularly common pitfall for pointers!

ยงExamples

use std::sync::atomic::{AtomicPtr, Ordering};

let ptr: *mut _ = &mut 5;
let some_ptr = AtomicPtr::new(ptr);

let new: *mut _ = &mut 10;
let result = some_ptr.update(Ordering::SeqCst, Ordering::SeqCst, |_| new);
assert_eq!(result, ptr);
assert_eq!(some_ptr.load(Ordering::SeqCst), new);
1.91.0 ยท Source

pub fn fetch_ptr_add(&self, val: usize, order: Ordering) -> *mut T

Offsets the pointerโ€™s address by adding val (in units of T), returning the previous pointer.

This is equivalent to using wrapping_add to atomically perform the equivalent of ptr = ptr.wrapping_add(val);.

This method operates in units of T, which means that it cannot be used to offset the pointer by an amount which is not a multiple of size_of::<T>(). This can sometimes be inconvenient, as you may want to work with a deliberately misaligned pointer. In such cases, you may use the fetch_byte_add method instead.

fetch_ptr_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
assert_eq!(atom.fetch_ptr_add(1, Ordering::Relaxed).addr(), 0);
// Note: units of `size_of::<i64>()`.
assert_eq!(atom.load(Ordering::Relaxed).addr(), 8);
1.91.0 ยท Source

pub fn fetch_ptr_sub(&self, val: usize, order: Ordering) -> *mut T

Offsets the pointerโ€™s address by subtracting val (in units of T), returning the previous pointer.

This is equivalent to using wrapping_sub to atomically perform the equivalent of ptr = ptr.wrapping_sub(val);.

This method operates in units of T, which means that it cannot be used to offset the pointer by an amount which is not a multiple of size_of::<T>(). This can sometimes be inconvenient, as you may want to work with a deliberately misaligned pointer. In such cases, you may use the fetch_byte_sub method instead.

fetch_ptr_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let array = [1i32, 2i32];
let atom = AtomicPtr::new(array.as_ptr().wrapping_add(1) as *mut _);

assert!(core::ptr::eq(
    atom.fetch_ptr_sub(1, Ordering::Relaxed),
    &array[1],
));
assert!(core::ptr::eq(atom.load(Ordering::Relaxed), &array[0]));
1.91.0 ยท Source

pub fn fetch_byte_add(&self, val: usize, order: Ordering) -> *mut T

Offsets the pointerโ€™s address by adding val bytes, returning the previous pointer.

This is equivalent to using wrapping_byte_add to atomically perform ptr = ptr.wrapping_byte_add(val).

fetch_byte_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let atom = AtomicPtr::<i64>::new(core::ptr::null_mut());
assert_eq!(atom.fetch_byte_add(1, Ordering::Relaxed).addr(), 0);
// Note: in units of bytes, not `size_of::<i64>()`.
assert_eq!(atom.load(Ordering::Relaxed).addr(), 1);
1.91.0 ยท Source

pub fn fetch_byte_sub(&self, val: usize, order: Ordering) -> *mut T

Offsets the pointerโ€™s address by subtracting val bytes, returning the previous pointer.

This is equivalent to using wrapping_byte_sub to atomically perform ptr = ptr.wrapping_byte_sub(val).

fetch_byte_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let mut arr = [0i64, 1];
let atom = AtomicPtr::<i64>::new(&raw mut arr[1]);
assert_eq!(atom.fetch_byte_sub(8, Ordering::Relaxed).addr(), (&raw const arr[1]).addr());
assert_eq!(atom.load(Ordering::Relaxed).addr(), (&raw const arr[0]).addr());
1.91.0 ยท Source

pub fn fetch_or(&self, val: usize, order: Ordering) -> *mut T

Performs a bitwise โ€œorโ€ operation on the address of the current pointer, and the argument val, and stores a pointer with provenance of the current pointer and the resulting address.

This is equivalent to using map_addr to atomically perform ptr = ptr.map_addr(|a| a | val). This can be used in tagged pointer schemes to atomically set tag bits.

Caveat: This operation returns the previous value. To compute the stored value without losing provenance, you may use map_addr. For example: a.fetch_or(val).map_addr(|a| a | val).

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

This API and its claimed semantics are part of the Strict Provenance experiment, see the module documentation for ptr for details.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let pointer = &mut 3i64 as *mut i64;

let atom = AtomicPtr::<i64>::new(pointer);
// Tag the bottom bit of the pointer.
assert_eq!(atom.fetch_or(1, Ordering::Relaxed).addr() & 1, 0);
// Extract and untag.
let tagged = atom.load(Ordering::Relaxed);
assert_eq!(tagged.addr() & 1, 1);
assert_eq!(tagged.map_addr(|p| p & !1), pointer);
1.91.0 ยท Source

pub fn fetch_and(&self, val: usize, order: Ordering) -> *mut T

Performs a bitwise โ€œandโ€ operation on the address of the current pointer, and the argument val, and stores a pointer with provenance of the current pointer and the resulting address.

This is equivalent to using map_addr to atomically perform ptr = ptr.map_addr(|a| a & val). This can be used in tagged pointer schemes to atomically unset tag bits.

Caveat: This operation returns the previous value. To compute the stored value without losing provenance, you may use map_addr. For example: a.fetch_and(val).map_addr(|a| a & val).

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

This API and its claimed semantics are part of the Strict Provenance experiment, see the module documentation for ptr for details.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let pointer = &mut 3i64 as *mut i64;
// A tagged pointer
let atom = AtomicPtr::<i64>::new(pointer.map_addr(|a| a | 1));
assert_eq!(atom.fetch_or(1, Ordering::Relaxed).addr() & 1, 1);
// Untag, and extract the previously tagged pointer.
let untagged = atom.fetch_and(!1, Ordering::Relaxed)
    .map_addr(|a| a & !1);
assert_eq!(untagged, pointer);
1.91.0 ยท Source

pub fn fetch_xor(&self, val: usize, order: Ordering) -> *mut T

Performs a bitwise โ€œxorโ€ operation on the address of the current pointer, and the argument val, and stores a pointer with provenance of the current pointer and the resulting address.

This is equivalent to using map_addr to atomically perform ptr = ptr.map_addr(|a| a ^ val). This can be used in tagged pointer schemes to atomically toggle tag bits.

Caveat: This operation returns the previous value. To compute the stored value without losing provenance, you may use map_addr. For example: a.fetch_xor(val).map_addr(|a| a ^ val).

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on AtomicPtr.

This API and its claimed semantics are part of the Strict Provenance experiment, see the module documentation for ptr for details.

ยงExamples
use core::sync::atomic::{AtomicPtr, Ordering};

let pointer = &mut 3i64 as *mut i64;
let atom = AtomicPtr::<i64>::new(pointer);

// Toggle a tag bit on the pointer.
atom.fetch_xor(1, Ordering::Relaxed);
assert_eq!(atom.load(Ordering::Relaxed).addr() & 1, 1);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut *mut T

Returns a mutable pointer to the underlying pointer.

Doing non-atomic reads and writes on the resulting pointer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut *mut T instead of &AtomicPtr<T>.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicPtr;

extern "C" {
    fn my_atomic_op(arg: *mut *mut u32);
}

let mut value = 17;
let atomic = AtomicPtr::new(&mut value);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<i8>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: i8) -> Atomic<i8>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicI8;

let atomic_forty_two = AtomicI8::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut i8) -> &'a Atomic<i8>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicI8};

// Get a pointer to an allocated value
let ptr: *mut i8 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicI8>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicI8::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicI8>() (note that this is always true, since align_of::<AtomicI8>() == 1).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut i8) -> *const Atomic<i8>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut i8

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let mut some_var = AtomicI8::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut i8) -> &mut Atomic<i8>

Get atomic access to a &mut i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let mut some_int = 123;
let a = AtomicI8::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<i8>]) -> &mut [i8]

Get non-atomic access to a &mut [AtomicI8] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI8, Ordering};

let mut some_ints = [const { AtomicI8::new(0) }; 10];

let view: &mut [i8] = AtomicI8::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [i8]) -> &mut [Atomic<i8>]

Get atomic access to a &mut [i8] slice.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI8, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicI8::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> i8

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicI8;

let some_var = AtomicI8::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> i8

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let some_var = AtomicI8::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<i8>, order: Ordering, ) -> i8

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI8>() (note that this is always true, since align_of::<AtomicI8>() == 1).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: i8, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let some_var = AtomicI8::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<i8>, val: i8, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI8>() (note that this is always true, since align_of::<AtomicI8>() == 1).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: i8, order: Ordering) -> i8

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let some_var = AtomicI8::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: i8, new: i8, order: Ordering) -> i8

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let some_var = AtomicI8::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: i8, new: i8, success: Ordering, failure: Ordering, ) -> Result<i8, i8>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let some_var = AtomicI8::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: i8, new: i8, success: Ordering, failure: Ordering, ) -> Result<i8, i8>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicI8::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let val = AtomicI8::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: i8, order: Ordering) -> i8

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: i8, order: Ordering) -> i8

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: i8, order: Ordering) -> i8

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: i8, order: Ordering) -> i8

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: i8, order: Ordering) -> i8

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: i8, order: Ordering) -> i8

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i8, i8>
where F: FnMut(i8) -> Option<i8>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicI8::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i8) -> Option<i8>, ) -> Result<i8, i8>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI8::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let x = AtomicI8::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i8) -> i8, ) -> i8

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI8::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let x = AtomicI8::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: i8, order: Ordering) -> i8

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: i8, order: Ordering) -> i8

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i8.

ยงExamples
use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicI8, Ordering};

let foo = AtomicI8::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut i8

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut i8 instead of &AtomicI8.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicI8;

extern "C" {
    fn my_atomic_op(arg: *mut i8);
}

let atomic = AtomicI8::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<u8>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: u8) -> Atomic<u8>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicU8;

let atomic_forty_two = AtomicU8::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut u8) -> &'a Atomic<u8>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicU8};

// Get a pointer to an allocated value
let ptr: *mut u8 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicU8>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicU8::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicU8>() (note that this is always true, since align_of::<AtomicU8>() == 1).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut u8) -> *const Atomic<u8>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut u8

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let mut some_var = AtomicU8::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut u8) -> &mut Atomic<u8>

Get atomic access to a &mut u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let mut some_int = 123;
let a = AtomicU8::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<u8>]) -> &mut [u8] โ“˜

Get non-atomic access to a &mut [AtomicU8] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU8, Ordering};

let mut some_ints = [const { AtomicU8::new(0) }; 10];

let view: &mut [u8] = AtomicU8::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [u8]) -> &mut [Atomic<u8>]

Get atomic access to a &mut [u8] slice.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU8, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicU8::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> u8

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicU8;

let some_var = AtomicU8::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> u8

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let some_var = AtomicU8::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<u8>, order: Ordering, ) -> u8

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU8>() (note that this is always true, since align_of::<AtomicU8>() == 1).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: u8, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let some_var = AtomicU8::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<u8>, val: u8, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU8>() (note that this is always true, since align_of::<AtomicU8>() == 1).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: u8, order: Ordering) -> u8

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let some_var = AtomicU8::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: u8, new: u8, order: Ordering) -> u8

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let some_var = AtomicU8::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: u8, new: u8, success: Ordering, failure: Ordering, ) -> Result<u8, u8>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let some_var = AtomicU8::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: u8, new: u8, success: Ordering, failure: Ordering, ) -> Result<u8, u8>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicU8::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let val = AtomicU8::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: u8, order: Ordering) -> u8

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: u8, order: Ordering) -> u8

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: u8, order: Ordering) -> u8

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: u8, order: Ordering) -> u8

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: u8, order: Ordering) -> u8

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: u8, order: Ordering) -> u8

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u8, u8>
where F: FnMut(u8) -> Option<u8>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicU8::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u8) -> Option<u8>, ) -> Result<u8, u8>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU8::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let x = AtomicU8::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u8) -> u8, ) -> u8

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU8::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let x = AtomicU8::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: u8, order: Ordering) -> u8

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: u8, order: Ordering) -> u8

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u8.

ยงExamples
use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicU8, Ordering};

let foo = AtomicU8::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut u8

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut u8 instead of &AtomicU8.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicU8;

extern "C" {
    fn my_atomic_op(arg: *mut u8);
}

let atomic = AtomicU8::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<i16>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: i16) -> Atomic<i16>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicI16;

let atomic_forty_two = AtomicI16::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut i16) -> &'a Atomic<i16>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicI16};

// Get a pointer to an allocated value
let ptr: *mut i16 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicI16>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicI16::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicI16>() (note that on some platforms this can be bigger than align_of::<i16>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut i16) -> *const Atomic<i16>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut i16

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let mut some_var = AtomicI16::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut i16) -> &mut Atomic<i16>

Get atomic access to a &mut i16.

Note: This function is only available on targets where AtomicI16 has the same alignment as i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let mut some_int = 123;
let a = AtomicI16::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<i16>]) -> &mut [i16]

Get non-atomic access to a &mut [AtomicI16] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI16, Ordering};

let mut some_ints = [const { AtomicI16::new(0) }; 10];

let view: &mut [i16] = AtomicI16::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [i16]) -> &mut [Atomic<i16>]

Get atomic access to a &mut [i16] slice.

Note: This function is only available on targets where AtomicI16 has the same alignment as i16.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI16, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicI16::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> i16

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicI16;

let some_var = AtomicI16::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> i16

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let some_var = AtomicI16::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<i16>, order: Ordering, ) -> i16

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI16>() (note that on some platforms this can be bigger than align_of::<i16>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: i16, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let some_var = AtomicI16::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<i16>, val: i16, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI16>() (note that on some platforms this can be bigger than align_of::<i16>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: i16, order: Ordering) -> i16

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let some_var = AtomicI16::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: i16, new: i16, order: Ordering) -> i16

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let some_var = AtomicI16::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: i16, new: i16, success: Ordering, failure: Ordering, ) -> Result<i16, i16>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let some_var = AtomicI16::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: i16, new: i16, success: Ordering, failure: Ordering, ) -> Result<i16, i16>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicI16::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let val = AtomicI16::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: i16, order: Ordering) -> i16

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: i16, order: Ordering) -> i16

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: i16, order: Ordering) -> i16

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: i16, order: Ordering) -> i16

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: i16, order: Ordering) -> i16

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: i16, order: Ordering) -> i16

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i16, i16>
where F: FnMut(i16) -> Option<i16>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicI16::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i16) -> Option<i16>, ) -> Result<i16, i16>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI16::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let x = AtomicI16::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i16) -> i16, ) -> i16

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI16::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let x = AtomicI16::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: i16, order: Ordering) -> i16

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: i16, order: Ordering) -> i16

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i16.

ยงExamples
use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicI16, Ordering};

let foo = AtomicI16::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut i16

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut i16 instead of &AtomicI16.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicI16;

extern "C" {
    fn my_atomic_op(arg: *mut i16);
}

let atomic = AtomicI16::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<u16>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: u16) -> Atomic<u16>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicU16;

let atomic_forty_two = AtomicU16::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut u16) -> &'a Atomic<u16>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicU16};

// Get a pointer to an allocated value
let ptr: *mut u16 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicU16>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicU16::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicU16>() (note that on some platforms this can be bigger than align_of::<u16>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut u16) -> *const Atomic<u16>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut u16

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let mut some_var = AtomicU16::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut u16) -> &mut Atomic<u16>

Get atomic access to a &mut u16.

Note: This function is only available on targets where AtomicU16 has the same alignment as u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let mut some_int = 123;
let a = AtomicU16::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<u16>]) -> &mut [u16]

Get non-atomic access to a &mut [AtomicU16] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU16, Ordering};

let mut some_ints = [const { AtomicU16::new(0) }; 10];

let view: &mut [u16] = AtomicU16::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [u16]) -> &mut [Atomic<u16>]

Get atomic access to a &mut [u16] slice.

Note: This function is only available on targets where AtomicU16 has the same alignment as u16.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU16, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicU16::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> u16

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicU16;

let some_var = AtomicU16::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> u16

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let some_var = AtomicU16::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<u16>, order: Ordering, ) -> u16

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU16>() (note that on some platforms this can be bigger than align_of::<u16>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: u16, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let some_var = AtomicU16::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<u16>, val: u16, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU16>() (note that on some platforms this can be bigger than align_of::<u16>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: u16, order: Ordering) -> u16

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let some_var = AtomicU16::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: u16, new: u16, order: Ordering) -> u16

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let some_var = AtomicU16::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: u16, new: u16, success: Ordering, failure: Ordering, ) -> Result<u16, u16>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let some_var = AtomicU16::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: u16, new: u16, success: Ordering, failure: Ordering, ) -> Result<u16, u16>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicU16::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let val = AtomicU16::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: u16, order: Ordering) -> u16

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: u16, order: Ordering) -> u16

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: u16, order: Ordering) -> u16

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: u16, order: Ordering) -> u16

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: u16, order: Ordering) -> u16

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: u16, order: Ordering) -> u16

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u16, u16>
where F: FnMut(u16) -> Option<u16>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicU16::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u16) -> Option<u16>, ) -> Result<u16, u16>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU16::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let x = AtomicU16::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u16) -> u16, ) -> u16

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU16::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let x = AtomicU16::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: u16, order: Ordering) -> u16

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: u16, order: Ordering) -> u16

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u16.

ยงExamples
use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicU16, Ordering};

let foo = AtomicU16::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut u16

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut u16 instead of &AtomicU16.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicU16;

extern "C" {
    fn my_atomic_op(arg: *mut u16);
}

let atomic = AtomicU16::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<i32>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: i32) -> Atomic<i32>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicI32;

let atomic_forty_two = AtomicI32::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut i32) -> &'a Atomic<i32>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicI32};

// Get a pointer to an allocated value
let ptr: *mut i32 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicI32>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicI32::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicI32>() (note that on some platforms this can be bigger than align_of::<i32>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut i32) -> *const Atomic<i32>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut i32

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let mut some_var = AtomicI32::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut i32) -> &mut Atomic<i32>

Get atomic access to a &mut i32.

Note: This function is only available on targets where AtomicI32 has the same alignment as i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let mut some_int = 123;
let a = AtomicI32::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<i32>]) -> &mut [i32]

Get non-atomic access to a &mut [AtomicI32] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI32, Ordering};

let mut some_ints = [const { AtomicI32::new(0) }; 10];

let view: &mut [i32] = AtomicI32::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [i32]) -> &mut [Atomic<i32>]

Get atomic access to a &mut [i32] slice.

Note: This function is only available on targets where AtomicI32 has the same alignment as i32.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI32, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicI32::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> i32

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicI32;

let some_var = AtomicI32::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> i32

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let some_var = AtomicI32::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<i32>, order: Ordering, ) -> i32

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI32>() (note that on some platforms this can be bigger than align_of::<i32>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: i32, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let some_var = AtomicI32::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<i32>, val: i32, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI32>() (note that on some platforms this can be bigger than align_of::<i32>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: i32, order: Ordering) -> i32

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let some_var = AtomicI32::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: i32, new: i32, order: Ordering) -> i32

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let some_var = AtomicI32::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: i32, new: i32, success: Ordering, failure: Ordering, ) -> Result<i32, i32>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let some_var = AtomicI32::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: i32, new: i32, success: Ordering, failure: Ordering, ) -> Result<i32, i32>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicI32::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let val = AtomicI32::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: i32, order: Ordering) -> i32

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: i32, order: Ordering) -> i32

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: i32, order: Ordering) -> i32

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: i32, order: Ordering) -> i32

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: i32, order: Ordering) -> i32

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: i32, order: Ordering) -> i32

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i32, i32>
where F: FnMut(i32) -> Option<i32>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicI32::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i32) -> Option<i32>, ) -> Result<i32, i32>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI32::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let x = AtomicI32::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i32) -> i32, ) -> i32

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI32::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let x = AtomicI32::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: i32, order: Ordering) -> i32

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: i32, order: Ordering) -> i32

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i32.

ยงExamples
use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicI32, Ordering};

let foo = AtomicI32::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut i32

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut i32 instead of &AtomicI32.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicI32;

extern "C" {
    fn my_atomic_op(arg: *mut i32);
}

let atomic = AtomicI32::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<u32>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: u32) -> Atomic<u32>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicU32;

let atomic_forty_two = AtomicU32::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut u32) -> &'a Atomic<u32>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicU32};

// Get a pointer to an allocated value
let ptr: *mut u32 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicU32>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicU32::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicU32>() (note that on some platforms this can be bigger than align_of::<u32>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut u32) -> *const Atomic<u32>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut u32

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let mut some_var = AtomicU32::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut u32) -> &mut Atomic<u32>

Get atomic access to a &mut u32.

Note: This function is only available on targets where AtomicU32 has the same alignment as u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let mut some_int = 123;
let a = AtomicU32::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<u32>]) -> &mut [u32]

Get non-atomic access to a &mut [AtomicU32] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU32, Ordering};

let mut some_ints = [const { AtomicU32::new(0) }; 10];

let view: &mut [u32] = AtomicU32::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [u32]) -> &mut [Atomic<u32>]

Get atomic access to a &mut [u32] slice.

Note: This function is only available on targets where AtomicU32 has the same alignment as u32.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU32, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicU32::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> u32

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicU32;

let some_var = AtomicU32::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> u32

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let some_var = AtomicU32::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<u32>, order: Ordering, ) -> u32

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU32>() (note that on some platforms this can be bigger than align_of::<u32>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: u32, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let some_var = AtomicU32::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<u32>, val: u32, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU32>() (note that on some platforms this can be bigger than align_of::<u32>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: u32, order: Ordering) -> u32

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let some_var = AtomicU32::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: u32, new: u32, order: Ordering) -> u32

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let some_var = AtomicU32::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: u32, new: u32, success: Ordering, failure: Ordering, ) -> Result<u32, u32>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let some_var = AtomicU32::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: u32, new: u32, success: Ordering, failure: Ordering, ) -> Result<u32, u32>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicU32::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let val = AtomicU32::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: u32, order: Ordering) -> u32

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: u32, order: Ordering) -> u32

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: u32, order: Ordering) -> u32

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: u32, order: Ordering) -> u32

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: u32, order: Ordering) -> u32

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: u32, order: Ordering) -> u32

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u32, u32>
where F: FnMut(u32) -> Option<u32>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicU32::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u32) -> Option<u32>, ) -> Result<u32, u32>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU32::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let x = AtomicU32::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u32) -> u32, ) -> u32

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU32::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let x = AtomicU32::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: u32, order: Ordering) -> u32

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: u32, order: Ordering) -> u32

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u32.

ยงExamples
use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicU32, Ordering};

let foo = AtomicU32::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut u32

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut u32 instead of &AtomicU32.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicU32;

extern "C" {
    fn my_atomic_op(arg: *mut u32);
}

let atomic = AtomicU32::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<i64>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: i64) -> Atomic<i64>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicI64;

let atomic_forty_two = AtomicI64::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut i64) -> &'a Atomic<i64>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicI64};

// Get a pointer to an allocated value
let ptr: *mut i64 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicI64>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicI64::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicI64>() (note that on some platforms this can be bigger than align_of::<i64>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut i64) -> *const Atomic<i64>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut i64

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let mut some_var = AtomicI64::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut i64) -> &mut Atomic<i64>

Get atomic access to a &mut i64.

Note: This function is only available on targets where AtomicI64 has the same alignment as i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let mut some_int = 123;
let a = AtomicI64::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<i64>]) -> &mut [i64]

Get non-atomic access to a &mut [AtomicI64] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI64, Ordering};

let mut some_ints = [const { AtomicI64::new(0) }; 10];

let view: &mut [i64] = AtomicI64::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [i64]) -> &mut [Atomic<i64>]

Get atomic access to a &mut [i64] slice.

Note: This function is only available on targets where AtomicI64 has the same alignment as i64.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicI64, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicI64::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> i64

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicI64;

let some_var = AtomicI64::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> i64

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let some_var = AtomicI64::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<i64>, order: Ordering, ) -> i64

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI64>() (note that on some platforms this can be bigger than align_of::<i64>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: i64, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let some_var = AtomicI64::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<i64>, val: i64, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicI64>() (note that on some platforms this can be bigger than align_of::<i64>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: i64, order: Ordering) -> i64

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let some_var = AtomicI64::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: i64, new: i64, order: Ordering) -> i64

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let some_var = AtomicI64::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: i64, new: i64, success: Ordering, failure: Ordering, ) -> Result<i64, i64>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let some_var = AtomicI64::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: i64, new: i64, success: Ordering, failure: Ordering, ) -> Result<i64, i64>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicI64::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let val = AtomicI64::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: i64, order: Ordering) -> i64

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: i64, order: Ordering) -> i64

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: i64, order: Ordering) -> i64

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: i64, order: Ordering) -> i64

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: i64, order: Ordering) -> i64

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: i64, order: Ordering) -> i64

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i64, i64>
where F: FnMut(i64) -> Option<i64>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicI64::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i64) -> Option<i64>, ) -> Result<i64, i64>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI64::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let x = AtomicI64::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(i64) -> i64, ) -> i64

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicI64::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let x = AtomicI64::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: i64, order: Ordering) -> i64

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: i64, order: Ordering) -> i64

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on i64.

ยงExamples
use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicI64, Ordering};

let foo = AtomicI64::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut i64

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut i64 instead of &AtomicI64.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicI64;

extern "C" {
    fn my_atomic_op(arg: *mut i64);
}

let atomic = AtomicI64::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<u64>

1.34.0 (const: 1.34.0) ยท Source

pub const fn new(v: u64) -> Atomic<u64>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicU64;

let atomic_forty_two = AtomicU64::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut u64) -> &'a Atomic<u64>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicU64};

// Get a pointer to an allocated value
let ptr: *mut u64 = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicU64>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicU64::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicU64>() (note that on some platforms this can be bigger than align_of::<u64>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut u64) -> *const Atomic<u64>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.34.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut u64

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let mut some_var = AtomicU64::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut u64) -> &mut Atomic<u64>

Get atomic access to a &mut u64.

Note: This function is only available on targets where AtomicU64 has the same alignment as u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let mut some_int = 123;
let a = AtomicU64::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<u64>]) -> &mut [u64]

Get non-atomic access to a &mut [AtomicU64] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU64, Ordering};

let mut some_ints = [const { AtomicU64::new(0) }; 10];

let view: &mut [u64] = AtomicU64::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [u64]) -> &mut [Atomic<u64>]

Get atomic access to a &mut [u64] slice.

Note: This function is only available on targets where AtomicU64 has the same alignment as u64.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicU64, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicU64::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.34.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> u64

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicU64;

let some_var = AtomicU64::new(5);
assert_eq!(some_var.into_inner(), 5);
1.34.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> u64

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let some_var = AtomicU64::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<u64>, order: Ordering, ) -> u64

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU64>() (note that on some platforms this can be bigger than align_of::<u64>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn store(&self, val: u64, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let some_var = AtomicU64::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<u64>, val: u64, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicU64>() (note that on some platforms this can be bigger than align_of::<u64>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.34.0 (const: unstable) ยท Source

pub fn swap(&self, val: u64, order: Ordering) -> u64

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let some_var = AtomicU64::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.34.0 (const: unstable) ยท Source

pub fn compare_and_swap(&self, current: u64, new: u64, order: Ordering) -> u64

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let some_var = AtomicU64::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.34.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: u64, new: u64, success: Ordering, failure: Ordering, ) -> Result<u64, u64>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let some_var = AtomicU64::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: u64, new: u64, success: Ordering, failure: Ordering, ) -> Result<u64, u64>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicU64::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let val = AtomicU64::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.34.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: u64, order: Ordering) -> u64

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: u64, order: Ordering) -> u64

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.34.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: u64, order: Ordering) -> u64

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.34.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: u64, order: Ordering) -> u64

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.34.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: u64, order: Ordering) -> u64

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.34.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: u64, order: Ordering) -> u64

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u64, u64>
where F: FnMut(u64) -> Option<u64>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicU64::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u64) -> Option<u64>, ) -> Result<u64, u64>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU64::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let x = AtomicU64::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(u64) -> u64, ) -> u64

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicU64::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let x = AtomicU64::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: u64, order: Ordering) -> u64

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: u64, order: Ordering) -> u64

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on u64.

ยงExamples
use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicU64, Ordering};

let foo = AtomicU64::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut u64

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut u64 instead of &AtomicU64.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicU64;

extern "C" {
    fn my_atomic_op(arg: *mut u64);
}

let atomic = AtomicU64::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<isize>

1.0.0 (const: 1.24.0) ยท Source

pub const fn new(v: isize) -> Atomic<isize>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicIsize;

let atomic_forty_two = AtomicIsize::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut isize) -> &'a Atomic<isize>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicIsize};

// Get a pointer to an allocated value
let ptr: *mut isize = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicIsize>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicIsize::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicIsize>() (note that on some platforms this can be bigger than align_of::<isize>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut isize) -> *const Atomic<isize>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.15.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut isize

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let mut some_var = AtomicIsize::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut isize) -> &mut Atomic<isize>

Get atomic access to a &mut isize.

Note: This function is only available on targets where AtomicIsize has the same alignment as isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let mut some_int = 123;
let a = AtomicIsize::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<isize>]) -> &mut [isize]

Get non-atomic access to a &mut [AtomicIsize] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicIsize, Ordering};

let mut some_ints = [const { AtomicIsize::new(0) }; 10];

let view: &mut [isize] = AtomicIsize::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [isize]) -> &mut [Atomic<isize>]

Get atomic access to a &mut [isize] slice.

Note: This function is only available on targets where AtomicIsize has the same alignment as isize.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicIsize, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicIsize::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.15.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> isize

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicIsize;

let some_var = AtomicIsize::new(5);
assert_eq!(some_var.into_inner(), 5);
1.0.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> isize

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let some_var = AtomicIsize::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<isize>, order: Ordering, ) -> isize

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicIsize>() (note that on some platforms this can be bigger than align_of::<isize>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn store(&self, val: isize, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let some_var = AtomicIsize::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<isize>, val: isize, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicIsize>() (note that on some platforms this can be bigger than align_of::<isize>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn swap(&self, val: isize, order: Ordering) -> isize

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let some_var = AtomicIsize::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.0.0 (const: unstable) ยท Source

pub fn compare_and_swap( &self, current: isize, new: isize, order: Ordering, ) -> isize

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let some_var = AtomicIsize::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.10.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: isize, new: isize, success: Ordering, failure: Ordering, ) -> Result<isize, isize>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let some_var = AtomicIsize::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.10.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: isize, new: isize, success: Ordering, failure: Ordering, ) -> Result<isize, isize>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicIsize::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let val = AtomicIsize::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.0.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: isize, order: Ordering) -> isize

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.0.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: isize, order: Ordering) -> isize

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.0.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: isize, order: Ordering) -> isize

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.27.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: isize, order: Ordering) -> isize

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.0.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: isize, order: Ordering) -> isize

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.0.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: isize, order: Ordering) -> isize

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<isize, isize>
where F: FnMut(isize) -> Option<isize>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicIsize::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(isize) -> Option<isize>, ) -> Result<isize, isize>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicIsize::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let x = AtomicIsize::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(isize) -> isize, ) -> isize

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicIsize::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let x = AtomicIsize::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: isize, order: Ordering) -> isize

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: isize, order: Ordering) -> isize

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on isize.

ยงExamples
use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicIsize, Ordering};

let foo = AtomicIsize::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut isize

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut isize instead of &AtomicIsize.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicIsize;

extern "C" {
    fn my_atomic_op(arg: *mut isize);
}

let atomic = AtomicIsize::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}
Sourceยง

impl Atomic<usize>

1.0.0 (const: 1.24.0) ยท Source

pub const fn new(v: usize) -> Atomic<usize>

Creates a new atomic integer.

ยงExamples
use std::sync::atomic::AtomicUsize;

let atomic_forty_two = AtomicUsize::new(42);
1.75.0 (const: 1.84.0) ยท Source

pub const unsafe fn from_ptr<'a>(ptr: *mut usize) -> &'a Atomic<usize>

Creates a new reference to an atomic integer from a pointer.

ยงExamples
use std::sync::atomic::{self, AtomicUsize};

// Get a pointer to an allocated value
let ptr: *mut usize = Box::into_raw(Box::new(0));

assert!(ptr.cast::<AtomicUsize>().is_aligned());

{
    // Create an atomic view of the allocated value
    let atomic = unsafe {AtomicUsize::from_ptr(ptr) };

    // Use `atomic` for atomic operations, possibly share it with other threads
    atomic.store(1, atomic::Ordering::Relaxed);
}

// It's ok to non-atomically access the value behind `ptr`,
// since the reference to the atomic ended its lifetime in the block above
assert_eq!(unsafe { *ptr }, 1);

// Deallocate the value
unsafe { drop(Box::from_raw(ptr)) }
ยงSafety
  • ptr must be aligned to align_of::<AtomicUsize>() (note that on some platforms this can be bigger than align_of::<usize>()).
  • ptr must be valid for both reads and writes for the whole lifetime 'a.
  • You must adhere to the Memory model for atomic accesses. In particular, it is not allowed to mix conflicting atomic and non-atomic accesses, or atomic accesses of different sizes, without synchronization.
Source

pub const fn from_ptr_raw(ptr: *mut usize) -> *const Atomic<usize>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Creates a new pointer to an atomic integer from a pointer.

This is useful if you want to do volatile atomic accesses, and thus avoid creating a reference to the destination.

1.15.0 (const: unstable) ยท Source

pub fn get_mut(&mut self) -> &mut usize

Returns a mutable reference to the underlying integer.

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let mut some_var = AtomicUsize::new(10);
assert_eq!(*some_var.get_mut(), 10);
*some_var.get_mut() = 5;
assert_eq!(some_var.load(Ordering::SeqCst), 5);
1.98.0 (const: unstable) ยท Source

pub fn from_mut(v: &mut usize) -> &mut Atomic<usize>

Get atomic access to a &mut usize.

Note: This function is only available on targets where AtomicUsize has the same alignment as usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let mut some_int = 123;
let a = AtomicUsize::from_mut(&mut some_int);
a.store(100, Ordering::Relaxed);
assert_eq!(some_int, 100);
1.98.0 (const: unstable) ยท Source

pub fn get_mut_slice(this: &mut [Atomic<usize>]) -> &mut [usize]

Get non-atomic access to a &mut [AtomicUsize] slice

This is safe because the mutable reference guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicUsize, Ordering};

let mut some_ints = [const { AtomicUsize::new(0) }; 10];

let view: &mut [usize] = AtomicUsize::get_mut_slice(&mut some_ints);
assert_eq!(view, [0; 10]);
view
    .iter_mut()
    .enumerate()
    .for_each(|(idx, int)| *int = idx as _);

std::thread::scope(|s| {
    some_ints
        .iter()
        .enumerate()
        .for_each(|(idx, int)| {
            s.spawn(move || assert_eq!(int.load(Ordering::Relaxed), idx as _));
        })
});
1.98.0 (const: unstable) ยท Source

pub fn from_mut_slice(v: &mut [usize]) -> &mut [Atomic<usize>]

Get atomic access to a &mut [usize] slice.

Note: This function is only available on targets where AtomicUsize has the same alignment as usize.

ยงExamples
โ“˜
use std::sync::atomic::{AtomicUsize, Ordering};

let mut some_ints = [0; 10];
let a = &*AtomicUsize::from_mut_slice(&mut some_ints);
std::thread::scope(|s| {
    for i in 0..a.len() {
        s.spawn(move || a[i].store(i as _, Ordering::Relaxed));
    }
});
for (i, n) in some_ints.into_iter().enumerate() {
    assert_eq!(i, n as usize);
}
1.15.0 (const: 1.79.0) ยท Source

pub const fn into_inner(self) -> usize

Consumes the atomic and returns the contained value.

This is safe because passing self by value guarantees that no other threads are concurrently accessing the atomic data.

ยงExamples
use std::sync::atomic::AtomicUsize;

let some_var = AtomicUsize::new(5);
assert_eq!(some_var.into_inner(), 5);
1.0.0 (const: unstable) ยท Source

pub fn load(&self, order: Ordering) -> usize

Loads a value from the atomic integer.

load takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

ยงPanics

Panics if order is Release or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let some_var = AtomicUsize::new(5);

assert_eq!(some_var.load(Ordering::Relaxed), 5);
Source

pub const unsafe fn load_volatile( self: *const Atomic<usize>, order: Ordering, ) -> usize

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Perform a volatile load from the atomic integer.

load_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Acquire and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls, but less opaque), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like load, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to reusing data from a previous load. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for reads. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a read are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic load. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be valid for reads, or self must point to memory outside of all Rust allocations and reading from that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicUsize>() (note that on some platforms this can be bigger than align_of::<usize>()).

  • Reading from self must produce a properly initialized value of the underlying integer type.

ยงPanics

Panics if order is Release or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn store(&self, val: usize, order: Ordering)

Stores a value into the atomic integer.

store takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

ยงPanics

Panics if order is Acquire or AcqRel.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let some_var = AtomicUsize::new(5);

some_var.store(10, Ordering::Relaxed);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
Source

pub const unsafe fn store_volatile( self: *const Atomic<usize>, val: usize, order: Ordering, )

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_volatile)

Performs a volatile store into the atomic integer.

store_volatile takes an Ordering argument which describes the memory ordering of this operation. Possible values are SeqCst, Release and Relaxed.

Volatile operations are intended to act on I/O memory. As such, they are considered externally observable events (just like syscalls), and are guaranteed to not be elided or reordered by the compiler across other externally observable events. With this in mind, there are two cases of usage that need to be distinguished:

  • When a volatile operation is used for memory inside an allocation, it behaves exactly like store, except for the additional guarantee that it wonโ€™t be elided or reordered across other externally observable events (see above). This implies that the operation will actually access memory and not e.g. be lowered to a register access. Other than that, all the usual rules for memory accesses apply (including provenance).

  • Volatile operations, however, may also be used to access memory that is outside of any Rust allocation. In this use-case, the pointer does not have to be valid for writes. This is typically used for CPU and peripheral registers that must be accessed via an I/O memory mapping, most commonly at fixed addresses reserved by the hardware. These often have special semantics associated to their manipulation, and cannot be used as general purpose memory. Here, any address value is possible, including 0 and usize::MAX, so long as the semantics of such a write are well-defined by the target hardware. The provenance of the pointer is irrelevant, and it can be created with without_provenance_mut. The access is allowed to trap, which must immediately abort the process. It can also cause other side-effects, but those must not affect Rust-allocated memory in any way.

In both cases, the access is also considered atomic with the given order. This allows synchronization with other threads or devices that share memory with this program.

When invoked during const evaluation, this behaves like a regular atomic store. In particular, such reads must always follow the first of the two cases above.

ยงSafety

Behavior is undefined if any of the following conditions are violated:

  • self must be either valid for writes, or self must point to memory outside of all Rust allocations and writing to that memory must:

    • not trap, and
    • not cause any memory inside a Rust allocation to be modified.
  • self must be aligned to align_of::<AtomicUsize>() (note that on some platforms this can be bigger than align_of::<usize>()).

ยงPanics

Panics if order is Acquire or AcqRel.

1.0.0 (const: unstable) ยท Source

pub fn swap(&self, val: usize, order: Ordering) -> usize

Stores a value into the atomic integer, returning the previous value.

swap takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let some_var = AtomicUsize::new(5);

assert_eq!(some_var.swap(10, Ordering::Relaxed), 5);
1.0.0 (const: unstable) ยท Source

pub fn compare_and_swap( &self, current: usize, new: usize, order: Ordering, ) -> usize

๐Ÿ‘ŽDeprecated since 1.50.0:

Use compare_exchange or compare_exchange_weak instead

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is always the previous value. If it is equal to current, then the value was updated.

compare_and_swap also takes an Ordering argument which describes the memory ordering of this operation. Notice that even when using AcqRel, the operation might fail and hence just perform an Acquire load, but not have Release semantics. Using Acquire makes the store part of this operation Relaxed if it happens, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงMigrating to compare_exchange and compare_exchange_weak

compare_and_swap is equivalent to compare_exchange with the following mapping for memory orderings:

OriginalSuccessFailure
RelaxedRelaxedRelaxed
AcquireAcquireAcquire
ReleaseReleaseRelaxed
AcqRelAcqRelAcquire
SeqCstSeqCstSeqCst

compare_and_swap and compare_exchange also differ in their return type. You can use compare_exchange(...).unwrap_or_else(|x| x) to recover the behavior of compare_and_swap, but in most cases it is more idiomatic to check whether the return value is Ok or Err rather than to infer success vs failure based on the value that was read.

During migration, consider whether it makes sense to use compare_exchange_weak instead. compare_exchange_weak is allowed to fail spuriously even when the comparison succeeds, which allows the compiler to generate better assembly code when the compare and swap is used in a loop.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let some_var = AtomicUsize::new(5);

assert_eq!(some_var.compare_and_swap(5, 10, Ordering::Relaxed), 5);
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_and_swap(6, 12, Ordering::Relaxed), 10);
assert_eq!(some_var.load(Ordering::Relaxed), 10);
1.10.0 (const: unstable) ยท Source

pub fn compare_exchange( &self, current: usize, new: usize, success: Ordering, failure: Ordering, ) -> Result<usize, usize>

Stores a value into the atomic integer if the current value is the same as the current value.

The return value is a result indicating whether the new value was written and containing the previous value. On success this value is guaranteed to be equal to current.

compare_exchange takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let some_var = AtomicUsize::new(5);

assert_eq!(some_var.compare_exchange(5, 10,
                                     Ordering::Acquire,
                                     Ordering::Relaxed),
           Ok(5));
assert_eq!(some_var.load(Ordering::Relaxed), 10);

assert_eq!(some_var.compare_exchange(6, 12,
                                     Ordering::SeqCst,
                                     Ordering::Acquire),
           Err(10));
assert_eq!(some_var.load(Ordering::Relaxed), 10);
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim! This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.10.0 (const: unstable) ยท Source

pub fn compare_exchange_weak( &self, current: usize, new: usize, success: Ordering, failure: Ordering, ) -> Result<usize, usize>

Stores a value into the atomic integer if the current value is the same as the current value.

Unlike AtomicUsize::compare_exchange, this function is allowed to spuriously fail even when the comparison succeeds, which can result in more efficient code on some platforms. The return value is a result indicating whether the new value was written and containing the previous value.

compare_exchange_weak takes two Ordering arguments to describe the memory ordering of this operation. success describes the required ordering for the read-modify-write operation that takes place if the comparison with current succeeds. failure describes the required ordering for the load operation that takes place when the comparison fails. Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the successful load Relaxed. The failure ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let val = AtomicUsize::new(4);

let mut old = val.load(Ordering::Relaxed);
loop {
    let new = old * 2;
    match val.compare_exchange_weak(old, new, Ordering::SeqCst, Ordering::Relaxed) {
        Ok(_) => break,
        Err(x) => old = x,
    }
}
ยงConsiderations

compare_exchange is a compare-and-swap operation and thus exhibits the usual downsides of CAS operations. In particular, a load of the value followed by a successful compare_exchange with the previous load does not ensure that other threads have not changed the value in the interim. This is usually important when the equality check in the compare_exchange is being used to check the identity of a value, but equality does not necessarily imply identity. This is a particularly common case for pointers, as a pointer holding the same address does not imply that the same object exists at that address! In this case, compare_exchange can lead to the ABA problem.

1.0.0 (const: unstable) ยท Source

pub fn fetch_add(&self, val: usize, order: Ordering) -> usize

Adds to the current value, returning the previous value.

This operation wraps around on overflow.

fetch_add takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(0);
assert_eq!(foo.fetch_add(10, Ordering::SeqCst), 0);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.0.0 (const: unstable) ยท Source

pub fn fetch_sub(&self, val: usize, order: Ordering) -> usize

Subtracts from the current value, returning the previous value.

This operation wraps around on overflow.

fetch_sub takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(20);
assert_eq!(foo.fetch_sub(10, Ordering::SeqCst), 20);
assert_eq!(foo.load(Ordering::SeqCst), 10);
1.0.0 (const: unstable) ยท Source

pub fn fetch_and(&self, val: usize, order: Ordering) -> usize

Bitwise โ€œandโ€ with the current value.

Performs a bitwise โ€œandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_and takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_and(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b100001);
1.27.0 (const: unstable) ยท Source

pub fn fetch_nand(&self, val: usize, order: Ordering) -> usize

Bitwise โ€œnandโ€ with the current value.

Performs a bitwise โ€œnandโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_nand takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(0x13);
assert_eq!(foo.fetch_nand(0x31, Ordering::SeqCst), 0x13);
assert_eq!(foo.load(Ordering::SeqCst), !(0x13 & 0x31));
1.0.0 (const: unstable) ยท Source

pub fn fetch_or(&self, val: usize, order: Ordering) -> usize

Bitwise โ€œorโ€ with the current value.

Performs a bitwise โ€œorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_or takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_or(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b111111);
1.0.0 (const: unstable) ยท Source

pub fn fetch_xor(&self, val: usize, order: Ordering) -> usize

Bitwise โ€œxorโ€ with the current value.

Performs a bitwise โ€œxorโ€ operation on the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_xor takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(0b101101);
assert_eq!(foo.fetch_xor(0b110011, Ordering::SeqCst), 0b101101);
assert_eq!(foo.load(Ordering::SeqCst), 0b011110);
1.45.0 ยท Source

pub fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<usize, usize>
where F: FnMut(usize) -> Option<usize>,

๐Ÿ‘ŽDeprecated since 1.99.0:

renamed to try_update for consistency

An alias for AtomicUsize::try_update .

1.95.0 ยท Source

pub fn try_update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(usize) -> Option<usize>, ) -> Result<usize, usize>

Fetches the value, and applies a function to it that returns an optional new value. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

See also: update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, as long as the function returns Some(_), but the function will have been applied only once to the stored value.

try_update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicUsize::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let x = AtomicUsize::new(7);
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |_| None), Err(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(7));
assert_eq!(x.try_update(Ordering::SeqCst, Ordering::SeqCst, |x| Some(x + 1)), Ok(8));
assert_eq!(x.load(Ordering::SeqCst), 9);
1.95.0 ยท Source

pub fn update( &self, set_order: Ordering, fetch_order: Ordering, f: impl FnMut(usize) -> usize, ) -> usize

Fetches the value, and applies a function to it that returns a new value. The new value is stored and the old value is returned.

See also: try_update.

Note: This may call the function multiple times if the value has been changed from other threads in the meantime, but the function will have been applied only once to the stored value.

update takes two Ordering arguments to describe the memory ordering of this operation. The first describes the required ordering for when the operation finally succeeds while the second describes the required ordering for loads. These correspond to the success and failure orderings of AtomicUsize::compare_exchange respectively.

Using Acquire as success ordering makes the store part of this operation Relaxed, and using Release makes the final successful load Relaxed. The (failed) load ordering can only be SeqCst, Acquire or Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงConsiderations

This method is not magic; it is not provided by the hardware, and does not act like a critical section or mutex.

It is implemented on top of an atomic compare-and-swap operation, and thus is subject to the usual drawbacks of CAS operations. In particular, be careful of the ABA problem if this atomic integer is an index or more generally if knowledge of only the bitwise value of the atomic is not in and of itself sufficient to ensure any required preconditions.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let x = AtomicUsize::new(7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 7);
assert_eq!(x.update(Ordering::SeqCst, Ordering::SeqCst, |x| x + 1), 8);
assert_eq!(x.load(Ordering::SeqCst), 9);
1.45.0 (const: unstable) ยท Source

pub fn fetch_max(&self, val: usize, order: Ordering) -> usize

Maximum with the current value.

Finds the maximum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_max takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(23);
assert_eq!(foo.fetch_max(42, Ordering::SeqCst), 23);
assert_eq!(foo.load(Ordering::SeqCst), 42);

If you want to obtain the maximum value in one step, you can use the following:

use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(23);
let bar = 42;
let max_foo = foo.fetch_max(bar, Ordering::SeqCst).max(bar);
assert!(max_foo == 42);
1.45.0 (const: unstable) ยท Source

pub fn fetch_min(&self, val: usize, order: Ordering) -> usize

Minimum with the current value.

Finds the minimum of the current value and the argument val, and sets the new value to the result.

Returns the previous value.

fetch_min takes an Ordering argument which describes the memory ordering of this operation. All ordering modes are possible. Note that using Acquire makes the store part of this operation Relaxed, and using Release makes the load part Relaxed.

Note: This method is only available on platforms that support atomic operations on usize.

ยงExamples
use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(23);
assert_eq!(foo.fetch_min(42, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 23);
assert_eq!(foo.fetch_min(22, Ordering::Relaxed), 23);
assert_eq!(foo.load(Ordering::Relaxed), 22);

If you want to obtain the minimum value in one step, you can use the following:

use std::sync::atomic::{AtomicUsize, Ordering};

let foo = AtomicUsize::new(23);
let bar = 12;
let min_foo = foo.fetch_min(bar, Ordering::SeqCst).min(bar);
assert_eq!(min_foo, 12);
1.70.0 (const: 1.70.0) ยท Source

pub const fn as_ptr(&self) -> *mut usize

Returns a mutable pointer to the underlying integer.

Doing non-atomic reads and writes on the resulting integer can be a data race. This method is mostly useful for FFI, where the function signature may use *mut usize instead of &AtomicUsize.

Returning an *mut pointer from a shared reference to this atomic is safe because the atomic types work with interior mutability. All modifications of an atomic change the value through a shared reference, and can do so safely as long as they use atomic operations. Any use of the returned raw pointer requires an unsafe block and still has to uphold the requirements of the memory model.

ยงExamples
โ“˜
use std::sync::atomic::AtomicUsize;

extern "C" {
    fn my_atomic_op(arg: *mut usize);
}

let atomic = AtomicUsize::new(1);

// SAFETY: Safe as long as `my_atomic_op` is atomic.
unsafe {
    my_atomic_op(atomic.as_ptr());
}

Trait Implementationsยง

ยง

impl Arbitrary for Atomic<bool>

ยง

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
ยง

type Strategy = Map<<bool as Arbitrary>::Strategy, fn(bool) -> Atomic<bool>>

The type of Strategy used to generate values of type Self.
ยง

fn arbitrary_with( _args: <Atomic<bool> as Arbitrary>::Parameters, ) -> <Atomic<bool> as Arbitrary>::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
ยง

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
ยง

impl Arbitrary for Atomic<isize>

ยง

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
ยง

type Strategy = Map<<isize as Arbitrary>::Strategy, fn(isize) -> Atomic<isize>>

The type of Strategy used to generate values of type Self.
ยง

fn arbitrary_with( _args: <Atomic<isize> as Arbitrary>::Parameters, ) -> <Atomic<isize> as Arbitrary>::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
ยง

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
ยง

impl Arbitrary for Atomic<usize>

ยง

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
ยง

type Strategy = Map<<usize as Arbitrary>::Strategy, fn(usize) -> Atomic<usize>>

The type of Strategy used to generate values of type Self.
ยง

fn arbitrary_with( _args: <Atomic<usize> as Arbitrary>::Parameters, ) -> <Atomic<usize> as Arbitrary>::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
ยง

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
ยง

impl<'a> Arbitrary<'a> for Atomic<bool>

Returns false, not an error, if this Unstructured [is empty][Unstructured::is_empty].

ยง

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Atomic<bool>, Error>

Generate an arbitrary value of Self from the given unstructured data. Read more
ยง

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
ยง

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

impl<'a> Arbitrary<'a> for Atomic<isize>

Returns zero, not an error, if this Unstructured [is empty][Unstructured::is_empty].

ยง

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Atomic<isize>, Error>

Generate an arbitrary value of Self from the given unstructured data. Read more
ยง

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
ยง

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

impl<'a> Arbitrary<'a> for Atomic<usize>

Returns zero, not an error, if this Unstructured [is empty][Unstructured::is_empty].

ยง

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Atomic<usize>, Error>

Generate an arbitrary value of Self from the given unstructured data. Read more
ยง

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
ยง

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
ยง

impl AtomicConsume for Atomic<bool>

Available on non-crossbeam_no_atomic only.
ยง

type Val = bool

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<bool> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<usize>

Available on non-crossbeam_no_atomic only.
ยง

type Val = usize

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<usize> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<isize>

Available on non-crossbeam_no_atomic only.
ยง

type Val = isize

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<isize> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<u8>

Available on non-crossbeam_no_atomic only.
ยง

type Val = u8

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<u8> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<i8>

Available on non-crossbeam_no_atomic only.
ยง

type Val = i8

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<i8> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<u16>

Available on non-crossbeam_no_atomic only.
ยง

type Val = u16

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<u16> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<i16>

Available on non-crossbeam_no_atomic only.
ยง

type Val = i16

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<i16> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<u32>

Available on non-crossbeam_no_atomic only.
ยง

type Val = u32

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<u32> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<i32>

Available on non-crossbeam_no_atomic only.
ยง

type Val = i32

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<i32> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<u64>

Available on non-crossbeam_no_atomic only.
ยง

type Val = u64

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<u64> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl AtomicConsume for Atomic<i64>

Available on non-crossbeam_no_atomic only.
ยง

type Val = i64

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<i64> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl<T> AtomicConsume for Atomic<*mut T>

Available on non-crossbeam_no_atomic only.
ยง

type Val = *mut T

Type returned by load_consume.
ยง

fn load_consume(&self) -> <Atomic<*mut T> as AtomicConsume>::Val

Loads a value from the atomic using a โ€œconsumeโ€ memory ordering. Read more
ยง

impl BitStore for Atomic<u8>

ยง

const ZERO: Atomic<u8>

The zero constant.
ยง

const ALIGNED_TO_SIZE: [(); 1]

All implementors are required to have their alignment match their size. Read more
ยง

const ALIAS_WIDTH: [(); 1]

All implementors are required to have Self and Self::Alias be equal in representation. This is true by fiat for all types except the unsigned integers. Read more
ยง

type Mem = u8

The element type used in the memory region underlying a BitSlice. It is always one of the unsigned integer fundamentals.
ยง

type Access = Atomic<u8>

A type that selects the appropriate load/store instructions when accessing the memory bus. It determines what instructions are used when moving a Self::Mem value between the processor and the memory system. Read more
ยง

type Alias = Atomic<u8>

A sibling BitStore implementor that is known to be alias-safe. It is used when a BitSlice introduces multiple handles that view the same memory location, and at least one of them has write capabilities to it. It must have the same underlying memory type, and can only change access patterns or public-facing usage.
ยง

type Unalias = Atomic<u8>

The inverse of ::Alias. It is used when a BitSlice removes the conditions that required a T -> T::Alias transition.
ยง

fn new(value: <Atomic<u8> as BitStore>::Mem) -> Atomic<u8>

Wraps a raw memory value as a BitStore type.
ยง

fn load_value(&self) -> <Atomic<u8> as BitStore>::Mem

Loads a value out of the memory system according to the ::Access rules. This may be called when the value is aliased by a write-capable reference.
ยง

fn store_value(&mut self, value: <Atomic<u8> as BitStore>::Mem)

Stores a value into the memory system. This is only called when there are no other handles to the value, and it may bypass ::Access constraints.
ยง

fn get_bit<O>(&self, index: BitIdx<Self::Mem>) -> bool
where O: BitOrder,

Reads a single bit out of the memory system according to the ::Access rules. This is lifted from BitAccess so that it can be used elsewhere without additional casts. Read more
ยง

impl BitStore for Atomic<u16>

ยง

const ZERO: Atomic<u16>

The zero constant.
ยง

const ALIGNED_TO_SIZE: [(); 1]

All implementors are required to have their alignment match their size. Read more
ยง

const ALIAS_WIDTH: [(); 1]

All implementors are required to have Self and Self::Alias be equal in representation. This is true by fiat for all types except the unsigned integers. Read more
ยง

type Mem = u16

The element type used in the memory region underlying a BitSlice. It is always one of the unsigned integer fundamentals.
ยง

type Access = Atomic<u16>

A type that selects the appropriate load/store instructions when accessing the memory bus. It determines what instructions are used when moving a Self::Mem value between the processor and the memory system. Read more
ยง

type Alias = Atomic<u16>

A sibling BitStore implementor that is known to be alias-safe. It is used when a BitSlice introduces multiple handles that view the same memory location, and at least one of them has write capabilities to it. It must have the same underlying memory type, and can only change access patterns or public-facing usage.
ยง

type Unalias = Atomic<u16>

The inverse of ::Alias. It is used when a BitSlice removes the conditions that required a T -> T::Alias transition.
ยง

fn new(value: <Atomic<u16> as BitStore>::Mem) -> Atomic<u16>

Wraps a raw memory value as a BitStore type.
ยง

fn load_value(&self) -> <Atomic<u16> as BitStore>::Mem

Loads a value out of the memory system according to the ::Access rules. This may be called when the value is aliased by a write-capable reference.
ยง

fn store_value(&mut self, value: <Atomic<u16> as BitStore>::Mem)

Stores a value into the memory system. This is only called when there are no other handles to the value, and it may bypass ::Access constraints.
ยง

fn get_bit<O>(&self, index: BitIdx<Self::Mem>) -> bool
where O: BitOrder,

Reads a single bit out of the memory system according to the ::Access rules. This is lifted from BitAccess so that it can be used elsewhere without additional casts. Read more
ยง

impl BitStore for Atomic<u32>

ยง

const ZERO: Atomic<u32>

The zero constant.
ยง

const ALIGNED_TO_SIZE: [(); 1]

All implementors are required to have their alignment match their size. Read more
ยง

const ALIAS_WIDTH: [(); 1]

All implementors are required to have Self and Self::Alias be equal in representation. This is true by fiat for all types except the unsigned integers. Read more
ยง

type Mem = u32

The element type used in the memory region underlying a BitSlice. It is always one of the unsigned integer fundamentals.
ยง

type Access = Atomic<u32>

A type that selects the appropriate load/store instructions when accessing the memory bus. It determines what instructions are used when moving a Self::Mem value between the processor and the memory system. Read more
ยง

type Alias = Atomic<u32>

A sibling BitStore implementor that is known to be alias-safe. It is used when a BitSlice introduces multiple handles that view the same memory location, and at least one of them has write capabilities to it. It must have the same underlying memory type, and can only change access patterns or public-facing usage.
ยง

type Unalias = Atomic<u32>

The inverse of ::Alias. It is used when a BitSlice removes the conditions that required a T -> T::Alias transition.
ยง

fn new(value: <Atomic<u32> as BitStore>::Mem) -> Atomic<u32>

Wraps a raw memory value as a BitStore type.
ยง

fn load_value(&self) -> <Atomic<u32> as BitStore>::Mem

Loads a value out of the memory system according to the ::Access rules. This may be called when the value is aliased by a write-capable reference.
ยง

fn store_value(&mut self, value: <Atomic<u32> as BitStore>::Mem)

Stores a value into the memory system. This is only called when there are no other handles to the value, and it may bypass ::Access constraints.
ยง

fn get_bit<O>(&self, index: BitIdx<Self::Mem>) -> bool
where O: BitOrder,

Reads a single bit out of the memory system according to the ::Access rules. This is lifted from BitAccess so that it can be used elsewhere without additional casts. Read more
ยง

impl BitStore for Atomic<u64>

ยง

const ZERO: Atomic<u64>

The zero constant.
ยง

const ALIGNED_TO_SIZE: [(); 1]

All implementors are required to have their alignment match their size. Read more
ยง

const ALIAS_WIDTH: [(); 1]

All implementors are required to have Self and Self::Alias be equal in representation. This is true by fiat for all types except the unsigned integers. Read more
ยง

type Mem = u64

The element type used in the memory region underlying a BitSlice. It is always one of the unsigned integer fundamentals.
ยง

type Access = Atomic<u64>

A type that selects the appropriate load/store instructions when accessing the memory bus. It determines what instructions are used when moving a Self::Mem value between the processor and the memory system. Read more
ยง

type Alias = Atomic<u64>

A sibling BitStore implementor that is known to be alias-safe. It is used when a BitSlice introduces multiple handles that view the same memory location, and at least one of them has write capabilities to it. It must have the same underlying memory type, and can only change access patterns or public-facing usage.
ยง

type Unalias = Atomic<u64>

The inverse of ::Alias. It is used when a BitSlice removes the conditions that required a T -> T::Alias transition.
ยง

fn new(value: <Atomic<u64> as BitStore>::Mem) -> Atomic<u64>

Wraps a raw memory value as a BitStore type.
ยง

fn load_value(&self) -> <Atomic<u64> as BitStore>::Mem

Loads a value out of the memory system according to the ::Access rules. This may be called when the value is aliased by a write-capable reference.
ยง

fn store_value(&mut self, value: <Atomic<u64> as BitStore>::Mem)

Stores a value into the memory system. This is only called when there are no other handles to the value, and it may bypass ::Access constraints.
ยง

fn get_bit<O>(&self, index: BitIdx<Self::Mem>) -> bool
where O: BitOrder,

Reads a single bit out of the memory system according to the ::Access rules. This is lifted from BitAccess so that it can be used elsewhere without additional casts. Read more
ยง

impl BitStore for Atomic<usize>

ยง

const ZERO: Atomic<usize>

The zero constant.
ยง

const ALIGNED_TO_SIZE: [(); 1]

All implementors are required to have their alignment match their size. Read more
ยง

const ALIAS_WIDTH: [(); 1]

All implementors are required to have Self and Self::Alias be equal in representation. This is true by fiat for all types except the unsigned integers. Read more
ยง

type Mem = usize

The element type used in the memory region underlying a BitSlice. It is always one of the unsigned integer fundamentals.
ยง

type Access = Atomic<usize>

A type that selects the appropriate load/store instructions when accessing the memory bus. It determines what instructions are used when moving a Self::Mem value between the processor and the memory system. Read more
ยง

type Alias = Atomic<usize>

A sibling BitStore implementor that is known to be alias-safe. It is used when a BitSlice introduces multiple handles that view the same memory location, and at least one of them has write capabilities to it. It must have the same underlying memory type, and can only change access patterns or public-facing usage.
ยง

type Unalias = Atomic<usize>

The inverse of ::Alias. It is used when a BitSlice removes the conditions that required a T -> T::Alias transition.
ยง

fn new(value: <Atomic<usize> as BitStore>::Mem) -> Atomic<usize>

Wraps a raw memory value as a BitStore type.
ยง

fn load_value(&self) -> <Atomic<usize> as BitStore>::Mem

Loads a value out of the memory system according to the ::Access rules. This may be called when the value is aliased by a write-capable reference.
ยง

fn store_value(&mut self, value: <Atomic<usize> as BitStore>::Mem)

Stores a value into the memory system. This is only called when there are no other handles to the value, and it may bypass ::Access constraints.
ยง

fn get_bit<O>(&self, index: BitIdx<Self::Mem>) -> bool
where O: BitOrder,

Reads a single bit out of the memory system according to the ::Access rules. This is lifted from BitAccess so that it can be used elsewhere without additional casts. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<i8>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<u8>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<i16>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<u16>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<i32>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<u32>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<i64>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.34.0 ยท Sourceยง

impl Debug for Atomic<u64>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.3.0 ยท Sourceยง

impl Debug for Atomic<isize>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.3.0 ยท Sourceยง

impl Debug for Atomic<usize>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.3.0 ยท Sourceยง

impl Debug for Atomic<bool>

Available on target_has_atomic_load_store=8 only.
Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.3.0 ยท Sourceยง

impl<T> Debug for Atomic<*mut T>

Available on target_has_atomic_load_store=ptr only.
Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.0.0 ยท Sourceยง

impl Default for Atomic<bool>

Available on target_has_atomic_load_store=8 only.
Sourceยง

fn default() -> Atomic<bool>

Creates an AtomicBool initialized to false.

1.0.0 ยท Sourceยง

impl<T> Default for Atomic<*mut T>

Available on target_has_atomic_load_store=ptr only.
Sourceยง

fn default() -> Atomic<*mut T>

Creates a null AtomicPtr<T>.

1.34.0 ยท Sourceยง

impl Default for Atomic<i8>

Sourceยง

fn default() -> Atomic<i8>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<u8>

Sourceยง

fn default() -> Atomic<u8>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<i16>

Sourceยง

fn default() -> Atomic<i16>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<u16>

Sourceยง

fn default() -> Atomic<u16>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<i32>

Sourceยง

fn default() -> Atomic<i32>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<u32>

Sourceยง

fn default() -> Atomic<u32>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<i64>

Sourceยง

fn default() -> Atomic<i64>

Returns the โ€œdefault valueโ€ for a type. Read more
1.34.0 ยท Sourceยง

impl Default for Atomic<u64>

Sourceยง

fn default() -> Atomic<u64>

Returns the โ€œdefault valueโ€ for a type. Read more
1.0.0 ยท Sourceยง

impl Default for Atomic<isize>

Sourceยง

fn default() -> Atomic<isize>

Returns the โ€œdefault valueโ€ for a type. Read more
1.0.0 ยท Sourceยง

impl Default for Atomic<usize>

Sourceยง

fn default() -> Atomic<usize>

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<bool>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<bool>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<i8>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<i8>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<i16>

Available on no_target_has_atomic or target_has_atomic=16 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<i16>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<i32>

Available on no_target_has_atomic or target_has_atomic=32 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<i32>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<isize>

Available on no_target_has_atomic or target_has_atomic=ptr only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<isize>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<u8>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<u8>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<u16>

Available on no_target_has_atomic or target_has_atomic=16 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<u16>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<u32>

Available on no_target_has_atomic or target_has_atomic=32 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<u32>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<usize>

Available on no_target_has_atomic or target_has_atomic=ptr only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<usize>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<i64>

Available on no_target_has_atomic or target_has_atomic=64 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<i64>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Sourceยง

impl<'de> Deserialize<'de> for Atomic<u64>

Available on no_target_has_atomic or target_has_atomic=64 only.
Sourceยง

fn deserialize<D>( deserializer: D, ) -> Result<Atomic<u64>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
1.23.0 (const: unstable) ยท Sourceยง

impl<T> From<*mut T> for Atomic<*mut T>

Available on target_has_atomic_load_store=ptr only.
Sourceยง

fn from(p: *mut T) -> Atomic<*mut T>

Converts a *mut T into an AtomicPtr<T>.

1.24.0 (const: unstable) ยท Sourceยง

impl From<bool> for Atomic<bool>

Available on target_has_atomic_load_store=8 only.
Sourceยง

fn from(b: bool) -> Atomic<bool>

Converts a bool into an AtomicBool.

ยงExamples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")
1.34.0 (const: unstable) ยท Sourceยง

impl From<i8> for Atomic<i8>

Sourceยง

fn from(v: i8) -> Atomic<i8>

Converts an i8 into an AtomicI8.

1.34.0 (const: unstable) ยท Sourceยง

impl From<i16> for Atomic<i16>

Sourceยง

fn from(v: i16) -> Atomic<i16>

Converts an i16 into an AtomicI16.

1.34.0 (const: unstable) ยท Sourceยง

impl From<i32> for Atomic<i32>

Sourceยง

fn from(v: i32) -> Atomic<i32>

Converts an i32 into an AtomicI32.

1.34.0 (const: unstable) ยท Sourceยง

impl From<i64> for Atomic<i64>

Sourceยง

fn from(v: i64) -> Atomic<i64>

Converts an i64 into an AtomicI64.

1.23.0 (const: unstable) ยท Sourceยง

impl From<isize> for Atomic<isize>

Sourceยง

fn from(v: isize) -> Atomic<isize>

Converts an isize into an AtomicIsize.

1.34.0 (const: unstable) ยท Sourceยง

impl From<u8> for Atomic<u8>

Sourceยง

fn from(v: u8) -> Atomic<u8>

Converts an u8 into an AtomicU8.

1.34.0 (const: unstable) ยท Sourceยง

impl From<u16> for Atomic<u16>

Sourceยง

fn from(v: u16) -> Atomic<u16>

Converts an u16 into an AtomicU16.

1.34.0 (const: unstable) ยท Sourceยง

impl From<u32> for Atomic<u32>

Sourceยง

fn from(v: u32) -> Atomic<u32>

Converts an u32 into an AtomicU32.

1.34.0 (const: unstable) ยท Sourceยง

impl From<u64> for Atomic<u64>

Sourceยง

fn from(v: u64) -> Atomic<u64>

Converts an u64 into an AtomicU64.

1.23.0 (const: unstable) ยท Sourceยง

impl From<usize> for Atomic<usize>

Sourceยง

fn from(v: usize) -> Atomic<usize>

Converts an usize into an AtomicUsize.

ยง

impl FromBytes for Atomic<u8>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<i8>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<u16>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<i16>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<u32>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<i32>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<u64>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<i64>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<usize>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromBytes for Atomic<isize>

ยง

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
ยง

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
ยง

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
ยง

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
ยง

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
ยง

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
ยง

fn ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &Self with a DST length equal to count. Read more
ยง

fn ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the prefix of the given source as a DST &Self with length equal to count. Read more
ยง

fn ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, !>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the suffix of the given source as a DST &Self with length equal to count. Read more
ยง

fn mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize> + Immutable,

Interprets the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the prefix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, !>>
where Self: IntoBytes + KnownLayout<PointerMetadata = usize>,

Interprets the suffix of the given source as a &mut Self with DST length equal to count. Read more
ยง

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
ยง

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
ยง

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
ยง

impl FromZeros for Atomic<u8>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<i8>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<bool>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<u16>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<i16>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<u32>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<i32>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<u64>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<i64>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<usize>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl FromZeros for Atomic<isize>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl<T> FromZeros for Atomic<*mut T>

ยง

fn zero(&mut self)

Overwrites self with zeros. Read more
ยง

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
ยง

impl IntoBytes for Atomic<u8>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<i8>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<bool>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<u16>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<i16>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<u32>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<i32>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<u64>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<i64>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<usize>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl IntoBytes for Atomic<isize>

ยง

fn as_bytes(&self) -> &[u8] โ“˜
where Self: Immutable,

Gets the bytes of this value. Read more
ยง

fn as_mut_bytes(&mut self) -> &mut [u8] โ“˜
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
ยง

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
ยง

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
ยง

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
ยง

impl KnownLayout for Atomic<u8>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<i8>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<bool>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<u16>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<i16>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<u32>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<i32>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<u64>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<i64>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<usize>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl KnownLayout for Atomic<isize>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
ยง

impl<T> KnownLayout for Atomic<*mut T>

ยง

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
ยง

fn size_for_metadata(meta: Self::PointerMetadata) -> Option<usize>

Computes the size of an object of type Self with the given pointer metadata. Read more
1.24.0 ยท Sourceยง

impl<T> Pointer for Atomic<*mut T>

Available on target_has_atomic_load_store=ptr only.
Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
ยง

impl Radium for Atomic<i8>

ยง

type Item = i8

ยง

fn new(value: i8) -> Atomic<i8>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut i8

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> i8

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> i8

Load a value from this object. Read more
ยง

fn store(&self, value: i8, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: i8, order: Ordering) -> i8

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: i8, new: i8, order: Ordering) -> i8

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: i8, new: i8, success: Ordering, failure: Ordering, ) -> Result<i8, i8>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: i8, new: i8, success: Ordering, failure: Ordering, ) -> Result<i8, i8>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i8, i8>
where F: FnMut(i8) -> Option<i8>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: i8, order: Ordering) -> i8

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: i8, order: Ordering) -> i8

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: i8, order: Ordering) -> i8

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: i8, order: Ordering) -> i8

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: i8, order: Ordering) -> i8

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: i8, order: Ordering) -> i8

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<u8>

ยง

type Item = u8

ยง

fn new(value: u8) -> Atomic<u8>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut u8

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> u8

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> u8

Load a value from this object. Read more
ยง

fn store(&self, value: u8, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: u8, order: Ordering) -> u8

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: u8, new: u8, order: Ordering) -> u8

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: u8, new: u8, success: Ordering, failure: Ordering, ) -> Result<u8, u8>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: u8, new: u8, success: Ordering, failure: Ordering, ) -> Result<u8, u8>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u8, u8>
where F: FnMut(u8) -> Option<u8>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: u8, order: Ordering) -> u8

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: u8, order: Ordering) -> u8

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: u8, order: Ordering) -> u8

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: u8, order: Ordering) -> u8

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: u8, order: Ordering) -> u8

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: u8, order: Ordering) -> u8

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<i16>

ยง

type Item = i16

ยง

fn new(value: i16) -> Atomic<i16>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut i16

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> i16

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> i16

Load a value from this object. Read more
ยง

fn store(&self, value: i16, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: i16, order: Ordering) -> i16

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: i16, new: i16, order: Ordering) -> i16

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: i16, new: i16, success: Ordering, failure: Ordering, ) -> Result<i16, i16>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: i16, new: i16, success: Ordering, failure: Ordering, ) -> Result<i16, i16>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i16, i16>
where F: FnMut(i16) -> Option<i16>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: i16, order: Ordering) -> i16

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: i16, order: Ordering) -> i16

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: i16, order: Ordering) -> i16

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: i16, order: Ordering) -> i16

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: i16, order: Ordering) -> i16

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: i16, order: Ordering) -> i16

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<u16>

ยง

type Item = u16

ยง

fn new(value: u16) -> Atomic<u16>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut u16

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> u16

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> u16

Load a value from this object. Read more
ยง

fn store(&self, value: u16, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: u16, order: Ordering) -> u16

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: u16, new: u16, order: Ordering) -> u16

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: u16, new: u16, success: Ordering, failure: Ordering, ) -> Result<u16, u16>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: u16, new: u16, success: Ordering, failure: Ordering, ) -> Result<u16, u16>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u16, u16>
where F: FnMut(u16) -> Option<u16>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: u16, order: Ordering) -> u16

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: u16, order: Ordering) -> u16

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: u16, order: Ordering) -> u16

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: u16, order: Ordering) -> u16

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: u16, order: Ordering) -> u16

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: u16, order: Ordering) -> u16

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<i32>

ยง

type Item = i32

ยง

fn new(value: i32) -> Atomic<i32>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut i32

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> i32

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> i32

Load a value from this object. Read more
ยง

fn store(&self, value: i32, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: i32, order: Ordering) -> i32

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: i32, new: i32, order: Ordering) -> i32

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: i32, new: i32, success: Ordering, failure: Ordering, ) -> Result<i32, i32>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: i32, new: i32, success: Ordering, failure: Ordering, ) -> Result<i32, i32>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i32, i32>
where F: FnMut(i32) -> Option<i32>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: i32, order: Ordering) -> i32

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: i32, order: Ordering) -> i32

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: i32, order: Ordering) -> i32

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: i32, order: Ordering) -> i32

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: i32, order: Ordering) -> i32

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: i32, order: Ordering) -> i32

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<u32>

ยง

type Item = u32

ยง

fn new(value: u32) -> Atomic<u32>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut u32

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> u32

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> u32

Load a value from this object. Read more
ยง

fn store(&self, value: u32, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: u32, order: Ordering) -> u32

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: u32, new: u32, order: Ordering) -> u32

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: u32, new: u32, success: Ordering, failure: Ordering, ) -> Result<u32, u32>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: u32, new: u32, success: Ordering, failure: Ordering, ) -> Result<u32, u32>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u32, u32>
where F: FnMut(u32) -> Option<u32>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: u32, order: Ordering) -> u32

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: u32, order: Ordering) -> u32

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: u32, order: Ordering) -> u32

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: u32, order: Ordering) -> u32

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: u32, order: Ordering) -> u32

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: u32, order: Ordering) -> u32

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<i64>

ยง

type Item = i64

ยง

fn new(value: i64) -> Atomic<i64>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut i64

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> i64

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> i64

Load a value from this object. Read more
ยง

fn store(&self, value: i64, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: i64, order: Ordering) -> i64

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: i64, new: i64, order: Ordering) -> i64

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: i64, new: i64, success: Ordering, failure: Ordering, ) -> Result<i64, i64>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: i64, new: i64, success: Ordering, failure: Ordering, ) -> Result<i64, i64>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<i64, i64>
where F: FnMut(i64) -> Option<i64>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: i64, order: Ordering) -> i64

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: i64, order: Ordering) -> i64

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: i64, order: Ordering) -> i64

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: i64, order: Ordering) -> i64

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: i64, order: Ordering) -> i64

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: i64, order: Ordering) -> i64

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<u64>

ยง

type Item = u64

ยง

fn new(value: u64) -> Atomic<u64>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut u64

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> u64

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> u64

Load a value from this object. Read more
ยง

fn store(&self, value: u64, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: u64, order: Ordering) -> u64

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: u64, new: u64, order: Ordering) -> u64

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: u64, new: u64, success: Ordering, failure: Ordering, ) -> Result<u64, u64>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: u64, new: u64, success: Ordering, failure: Ordering, ) -> Result<u64, u64>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<u64, u64>
where F: FnMut(u64) -> Option<u64>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: u64, order: Ordering) -> u64

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: u64, order: Ordering) -> u64

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: u64, order: Ordering) -> u64

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: u64, order: Ordering) -> u64

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: u64, order: Ordering) -> u64

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: u64, order: Ordering) -> u64

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<isize>

ยง

type Item = isize

ยง

fn new(value: isize) -> Atomic<isize>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut isize

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> isize

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> isize

Load a value from this object. Read more
ยง

fn store(&self, value: isize, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: isize, order: Ordering) -> isize

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: isize, new: isize, order: Ordering) -> isize

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: isize, new: isize, success: Ordering, failure: Ordering, ) -> Result<isize, isize>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: isize, new: isize, success: Ordering, failure: Ordering, ) -> Result<isize, isize>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<isize, isize>
where F: FnMut(isize) -> Option<isize>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: isize, order: Ordering) -> isize

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: isize, order: Ordering) -> isize

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: isize, order: Ordering) -> isize

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: isize, order: Ordering) -> isize

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: isize, order: Ordering) -> isize

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: isize, order: Ordering) -> isize

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<usize>

ยง

type Item = usize

ยง

fn new(value: usize) -> Atomic<usize>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut usize

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> usize

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> usize

Load a value from this object. Read more
ยง

fn store(&self, value: usize, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: usize, order: Ordering) -> usize

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: usize, new: usize, order: Ordering) -> usize

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: usize, new: usize, success: Ordering, failure: Ordering, ) -> Result<usize, usize>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: usize, new: usize, success: Ordering, failure: Ordering, ) -> Result<usize, usize>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<usize, usize>
where F: FnMut(usize) -> Option<usize>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: usize, order: Ordering) -> usize

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: usize, order: Ordering) -> usize

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: usize, order: Ordering) -> usize

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: usize, order: Ordering) -> usize

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: usize, order: Ordering) -> usize

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: usize, order: Ordering) -> usize

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl Radium for Atomic<bool>

ยง

type Item = bool

ยง

fn new(value: bool) -> Atomic<bool>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut bool

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> bool

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> bool

Load a value from this object. Read more
ยง

fn store(&self, value: bool, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: bool, order: Ordering) -> bool

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: bool, new: bool, success: Ordering, failure: Ordering, ) -> Result<bool, bool>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<bool, bool>
where F: FnMut(bool) -> Option<bool>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: bool, order: Ordering) -> bool

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: bool, order: Ordering) -> bool

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: bool, order: Ordering) -> bool

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: bool, order: Ordering) -> bool

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: NumericOps,

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: NumericOps,

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
ยง

impl<T> Radium for Atomic<*mut T>

ยง

type Item = *mut T

ยง

fn new(value: *mut T) -> Atomic<*mut T>

Creates a new value of this type.
ยง

fn fence(order: Ordering)

If the underlying value is atomic, calls fence with the given Ordering. Otherwise, does nothing.
ยง

fn get_mut(&mut self) -> &mut *mut T

Returns a mutable reference to the underlying value. Read more
ยง

fn into_inner(self) -> *mut T

Consumes the wrapper and returns the contained value. Read more
ยง

fn load(&self, order: Ordering) -> *mut T

Load a value from this object. Read more
ยง

fn store(&self, value: *mut T, order: Ordering)

Store a value in this object. Read more
ยง

fn swap(&self, value: *mut T, order: Ordering) -> *mut T

Swap with the value stored in this object. Read more
ยง

fn compare_and_swap( &self, current: *mut T, new: *mut T, order: Ordering, ) -> *mut T

๐Ÿ‘ŽDeprecated:

Use compare_exchange or compare_exchange_weak instead

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange( &self, current: *mut T, new: *mut T, success: Ordering, failure: Ordering, ) -> Result<*mut T, *mut T>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn compare_exchange_weak( &self, current: *mut T, new: *mut T, success: Ordering, failure: Ordering, ) -> Result<*mut T, *mut T>

Stores a value into this object if the currently-stored value is the same as the current value. Read more
ยง

fn fetch_update<F>( &self, set_order: Ordering, fetch_order: Ordering, f: F, ) -> Result<*mut T, *mut T>
where F: FnMut(*mut T) -> Option<*mut T>,

Fetches the value, and applies a function to it that returns an optional new value. Read more
ยง

fn fetch_and(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: BitOps,

Performs a bitwise โ€œandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_nand(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: BitOps,

Performs a bitwise โ€œnandโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_or(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: BitOps,

Performs a bitwise โ€œorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_xor(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: BitOps,

Performs a bitwise โ€œxorโ€ on the currently-stored value and the argument value, and stores the result in self. Read more
ยง

fn fetch_add(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: NumericOps,

Adds value to the currently-stored value, wrapping on overflow, and stores the result in self. Read more
ยง

fn fetch_sub(&self, value: Self::Item, order: Ordering) -> Self::Item
where Self::Item: NumericOps,

Subtracts value from the currently-stored value, wrapping on underflow, and stores the result in self. Read more
1.14.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<isize>

Available on target_has_atomic_load_store=ptr only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<i8>

Available on target_has_atomic_load_store=8 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<i16>

Available on target_has_atomic_load_store=16 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<i32>

Available on target_has_atomic_load_store=32 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<i64>

Available on target_has_atomic_load_store=64 only.
1.14.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<usize>

Available on target_has_atomic_load_store=ptr only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<u8>

Available on target_has_atomic_load_store=8 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<u16>

Available on target_has_atomic_load_store=16 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<u32>

Available on target_has_atomic_load_store=32 only.
1.34.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<u64>

Available on target_has_atomic_load_store=64 only.
1.14.0 ยท Sourceยง

impl RefUnwindSafe for Atomic<bool>

Available on target_has_atomic_load_store=8 only.
1.14.0 ยท Sourceยง

impl<T> RefUnwindSafe for Atomic<*mut T>

Available on target_has_atomic_load_store=ptr only.
1.0.0 ยท Sourceยง

impl<T> Send for Atomic<T>
where T: AtomicPrimitive,

Sourceยง

impl Serialize for Atomic<bool>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<i8>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<i16>

Available on no_target_has_atomic or target_has_atomic=16 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<i32>

Available on no_target_has_atomic or target_has_atomic=32 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<isize>

Available on no_target_has_atomic or target_has_atomic=ptr only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<u8>

Available on no_target_has_atomic or target_has_atomic=8 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<u16>

Available on no_target_has_atomic or target_has_atomic=16 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<u32>

Available on no_target_has_atomic or target_has_atomic=32 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<usize>

Available on no_target_has_atomic or target_has_atomic=ptr only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<i64>

Available on no_target_has_atomic or target_has_atomic=64 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Sourceยง

impl Serialize for Atomic<u64>

Available on no_target_has_atomic or target_has_atomic=64 only.
Sourceยง

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
1.0.0 ยท Sourceยง

impl<T> Sync for Atomic<T>
where T: AtomicPrimitive,

ยง

impl TryFromBytes for Atomic<u8>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<i8>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<bool>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<u16>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<i16>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<u32>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<i32>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<u64>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<i64>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<usize>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl TryFromBytes for Atomic<isize>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl<T> TryFromBytes for Atomic<*mut T>

ยง

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
ยง

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
ยง

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
ยง

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the given source as a &mut Self without copying. Read more
ยง

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
ยง

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
ยง

fn try_ref_from_bytes_with_elems( source: &[u8], count: usize, ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_prefix_with_elems( source: &[u8], count: usize, ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the prefix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_ref_from_suffix_with_elems( source: &[u8], count: usize, ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + Immutable,

Attempts to interpret the suffix of the given source as a &Self with a DST length equal to count. Read more
ยง

fn try_mut_from_bytes_with_elems( source: &mut [u8], count: usize, ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_prefix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the prefix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_mut_from_suffix_with_elems( source: &mut [u8], count: usize, ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout<PointerMetadata = usize> + IntoBytes,

Attempts to interpret the suffix of the given source as a &mut Self with a DST length equal to count. Read more
ยง

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
ยง

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
ยง

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
ยง

impl Unaligned for Atomic<bool>

ยง

impl Unaligned for Atomic<u8>

ยง

impl Unaligned for Atomic<i8>

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
ยง

impl<A> BitAccess for A
where A: Radium, <A as Radium>::Item: BitRegister,

ยง

fn clear_bits(&self, mask: BitMask<Self::Item>) -> Self::Item

Clears bits within a memory element to 0. Read more
ยง

fn set_bits(&self, mask: BitMask<Self::Item>) -> Self::Item

Sets bits within a memory element to 1. Read more
ยง

fn invert_bits(&self, mask: BitMask<Self::Item>) -> Self::Item

Inverts bits within a memory element. Read more
ยง

fn write_bit<O>(&self, index: BitIdx<Self::Item>, value: bool) -> bool
where O: BitOrder,

Writes a value to one bit in a memory element, returning the previous value. Read more
ยง

fn get_writers( value: bool, ) -> for<'a> fn(&'a Self, BitMask<Self::Item>) -> Self::Item

Gets the function that will write value into all bits under a mask. Read more
ยง

impl<T> BitView for T
where T: BitStore,

ยง

type Store = T

The underlying element type.
ยง

fn view_bits<O>(&self) -> &BitSlice<T, O> โ“˜
where O: BitOrder,

Views a memory region as an immutable bit-slice.
ยง

fn try_view_bits<O>(&self) -> Result<&BitSlice<T, O>, BitSpanError<T>>
where O: BitOrder,

Attempts to view a memory region as an immutable bit-slice. Read more
ยง

fn view_bits_mut<O>(&mut self) -> &mut BitSlice<T, O> โ“˜
where O: BitOrder,

Views a memory region as a mutable bit-slice.
ยง

fn try_view_bits_mut<O>( &mut self, ) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>
where O: BitOrder,

Attempts to view a memory region as a mutable bit-slice. Read more
ยง

impl<T> BitViewSized for T
where T: BitStore,

ยง

const ZERO: T = <T as BitStore>::ZERO

The zero constant.
ยง

fn as_raw_slice(&self) -> &[<T as BitView>::Store]

Views the type as a slice of its elements.
ยง

fn as_raw_mut_slice(&mut self) -> &mut [<T as BitView>::Store]

Views the type as a mutable slice of its elements.
ยง

fn into_bitarray<O>(self) -> BitArray<Self, O>
where O: BitOrder,

Wraps self in a BitArray.
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
ยง

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

ยง

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

ยง

impl<T> Conv for T

ยง

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Sourceยง

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

ยง

impl<T> FmtForward for T

ยง

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
ยง

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
ยง

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
ยง

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
ยง

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
ยง

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
ยง

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
ยง

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
ยง

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

ยง

impl<T> Instrument for T

ยง

fn instrument(self, span: Span) -> Instrumented<Self> โ“˜

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
ยง

fn in_current_span(self) -> Instrumented<Self> โ“˜

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self> โ“˜

Converts 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 more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ“˜
where F: FnOnce(&Self) -> bool,

Converts 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 more
ยง

impl<T> MaybeSerde for T
where T: Serialize + for<'de> Deserialize<'de>,

ยง

impl<T> NonStaticAny for T
where T: ?Sized,

ยง

fn type_id(&self) -> TypeId

Returns the lifetime-erased type ID of self.
ยง

impl<D> OwoColorize for D

ยง

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
ยง

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
ยง

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
ยง

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
ยง

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
ยง

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
ยง

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
ยง

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
ยง

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
ยง

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
ยง

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
ยง

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
ยง

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
ยง

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
ยง

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
ยง

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
ยง

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
ยง

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
ยง

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
ยง

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
ยง

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
ยง

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
ยง

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
ยง

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
ยง

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
ยง

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
ยง

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
ยง

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
ยง

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
ยง

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
ยง

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
ยง

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
ยง

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
ยง

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
ยง

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
ยง

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
ยง

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
ยง

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
ยง

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
ยง

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
ยง

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
ยง

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
ยง

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
ยง

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
ยง

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
ยง

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
ยง

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
ยง

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either [OwoColorize::fg] or a color-specific method, such as [OwoColorize::green], Read more
ยง

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either [OwoColorize::bg] or a color-specific method, such as [OwoColorize::on_yellow], Read more
ยง

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
ยง

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
ยง

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
ยง

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
ยง

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
ยง

impl<T> Pipe for T
where T: ?Sized,

ยง

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
ยง

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
ยง

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
ยง

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
ยง

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
ยง

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
ยง

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
ยง

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
ยง

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
ยง

impl<T> Pointable for T

ยง

const ALIGN: usize

The alignment of pointer.
ยง

type Init = T

The type for initializers.
ยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
ยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
ยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
ยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
ยง

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
ยง

impl<T> Tap for T

ยง

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
ยง

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
ยง

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
ยง

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
ยง

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
ยง

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
ยง

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
ยง

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
ยง

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
ยง

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
ยง

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
ยง

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
ยง

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
ยง

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
ยง

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
ยง

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
ยง

impl<T> TryConv for T

ยง

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = !

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
ยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

ยง

fn vzip(self) -> V

ยง

impl<T> WithSubscriber for T

ยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> โ“˜
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
ยง

fn with_current_subscriber(self) -> WithDispatch<Self> โ“˜

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more

Layoutยง

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.