pub struct AtomicFlags<F>{ /* private fields */ }Expand description
A flag set stored in an atomic integer.
Implementations§
Source§impl<F> AtomicFlags<F>
impl<F> AtomicFlags<F>
pub fn new(flags: F) -> Self
Sourcepub const fn from_storage(storage: <F::Bits as AtomicBits>::Storage) -> Self
pub const fn from_storage(storage: <F::Bits as AtomicBits>::Storage) -> Self
Const constructor from an already-built atomic.
Less ergonomic than new but allow to build this struct from a const context
(i.e. static flags).
use std::sync::atomic::AtomicU8;
use atomic_bitflags::AtomicFlags;
use bitflags::bitflags;
bitflags! {
#[derive(Clone, Copy)]
struct Features: u8 {
const CACHE = 1 << 0;
}
}
static FEATURES: AtomicFlags<Features> =
AtomicFlags::from_storage(AtomicU8::new(Features::CACHE.bits()));pub fn store(&self, flags: F, ordering: Ordering)
pub fn load(&self, ordering: Ordering) -> F
Sourcepub fn fetch_and(&self, flags: F, ordering: Ordering) -> F
pub fn fetch_and(&self, flags: F, ordering: Ordering) -> F
Atomically replaces the value with current & flags returning the previous value.
Sourcepub fn fetch_or(&self, flags: F, ordering: Ordering) -> F
pub fn fetch_or(&self, flags: F, ordering: Ordering) -> F
Atomically replaces the value with current | flags, returning the
previous value.
Sourcepub fn fetch_xor(&self, flags: F, ordering: Ordering) -> F
pub fn fetch_xor(&self, flags: F, ordering: Ordering) -> F
Atomically toggles flags, returning the previous value.
Sourcepub fn fetch_nand(&self, flags: F, ordering: Ordering) -> F
pub fn fetch_nand(&self, flags: F, ordering: Ordering) -> F
Atomically replaces the value with !(current & flags), returning the
previous value.
This follows the standard atomic fetch_nand: the result is not
restricted to flags, so bits outside the mask are set.
Sourcepub fn swap(&self, flags: F, ordering: Ordering) -> F
pub fn swap(&self, flags: F, ordering: Ordering) -> F
Atomically replaces the value with flags, returning the previous value.
Sourcepub fn compare_exchange(
&self,
current: F,
new: F,
success: Ordering,
failure: Ordering,
) -> Result<F, F>
pub fn compare_exchange( &self, current: F, new: F, success: Ordering, failure: Ordering, ) -> Result<F, F>
Atomically stores new if the current value is current.
Returns Ok(previous) on success and Err(actual) on failure.
§Panics
Panics if failure is stronger than success.
Sourcepub fn compare_exchange_weak(
&self,
current: F,
new: F,
success: Ordering,
failure: Ordering,
) -> Result<F, F>
pub fn compare_exchange_weak( &self, current: F, new: F, success: Ordering, failure: Ordering, ) -> Result<F, F>
Like compare_exchange, but may fail
spuriously.
Use it inside a retry loop where a spurious failure is harmless; prefer
compare_exchange otherwise.
§Panics
Panics if failure is stronger than success.
Sourcepub fn try_update<Op>(
&self,
set_order: Ordering,
fetch_order: Ordering,
f: Op,
) -> Result<F, F>
pub fn try_update<Op>( &self, set_order: Ordering, fetch_order: Ordering, f: Op, ) -> Result<F, F>
Atomically updates the value using f, returning the previous value.
f receives the current value and returns the new one, or None to
abort. It may run more than once if another thread changes the value.
Returns Ok(previous) on success and Err(current) if f returns
None.
§Panics
Panics if fetch_order is Release or AcqRel, or stronger than
set_order.
Sourcepub fn take(&self, flags: F, ordering: Ordering) -> F
pub fn take(&self, flags: F, ordering: Ordering) -> F
Atomically clears flags and returns the subset that was set.
Returns an empty set if none of flags were set. Other bits are left
untouched.
Sourcepub fn peek(&self, flags: F, ordering: Ordering) -> F
pub fn peek(&self, flags: F, ordering: Ordering) -> F
Returns the subset of flags that is currently set.
Does not modify the stored value. The result is a snapshot from a single
load; use take to claim flags atomically.
Sourcepub fn into_inner(self) -> F
pub fn into_inner(self) -> F
Consumes the atomic and returns the flags.