Skip to main content

AtomicFlags

Struct AtomicFlags 

Source
pub struct AtomicFlags<F>
where F: Flags + Copy, F::Bits: AtomicBits,
{ /* private fields */ }
Expand description

A flag set stored in an atomic integer.

Implementations§

Source§

impl<F> AtomicFlags<F>
where F: Flags + Copy, F::Bits: AtomicBits,

Source

pub fn new(flags: F) -> Self

Source

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()));
Source

pub fn store(&self, flags: F, ordering: Ordering)

Source

pub fn load(&self, ordering: Ordering) -> F

Source

pub fn fetch_and(&self, flags: F, ordering: Ordering) -> F

Atomically replaces the value with current & flags returning the previous value.

Source

pub fn fetch_or(&self, flags: F, ordering: Ordering) -> F

Atomically replaces the value with current | flags, returning the previous value.

Source

pub fn fetch_xor(&self, flags: F, ordering: Ordering) -> F

Atomically toggles flags, returning the previous value.

Source

pub fn toggle(&self, flags: F, ordering: Ordering) -> F

Synonymous with fetch_xor.

Source

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.

Source

pub fn swap(&self, flags: F, ordering: Ordering) -> F

Atomically replaces the value with flags, returning the previous value.

Source

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.

Source

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.

Source

pub fn try_update<Op>( &self, set_order: Ordering, fetch_order: Ordering, f: Op, ) -> Result<F, F>
where Op: FnMut(F) -> Option<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.

Source

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.

Source

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.

Source

pub fn clear(&self, ordering: Ordering)

Clears all flags.

Source

pub fn into_inner(self) -> F

Consumes the atomic and returns the flags.

Source

pub fn set(&self, flags: F, ordering: Ordering) -> F

Atomically sets the bits in flags, returning the previous value.

Source

pub fn unset(&self, flags: F, ordering: Ordering) -> F

Atomically clears the bits in flags, returning the previous value.

Trait Implementations§

Source§

impl<F> Debug for AtomicFlags<F>
where F: Flags + Copy + Debug, F::Bits: AtomicBits,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F> Default for AtomicFlags<F>
where F: Flags + Copy + Default, F::Bits: AtomicBits,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<F> Freeze for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: Freeze,

§

impl<F> RefUnwindSafe for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: RefUnwindSafe,

§

impl<F> Send for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: Send,

§

impl<F> Sync for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: Sync,

§

impl<F> Unpin for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: Unpin,

§

impl<F> UnsafeUnpin for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: UnsafeUnpin,

§

impl<F> UnwindSafe for AtomicFlags<F>
where <F as Flags>::Bits: Sized, <<F as Flags>::Bits as AtomicBits>::Storage: UnwindSafe,

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
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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.