AtomicBool

Struct AtomicBool 

Source
pub struct AtomicBool(AtomicBool);
Expand description

A boolean type which can be safely shared between threads.

This type has the same size, alignment, and bit validity as a [bool].

Tuple Fields§

§0: AtomicBool

Implementations§

Source§

impl AtomicBool

Source

pub const fn new(v: bool) -> AtomicBool

Creates a new AtomicBool.

§Examples
use KeOS::sync::atomic::AtomicBool;

let atomic_true = AtomicBool::new(true);
let atomic_false = AtomicBool::new(false);
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 keos::atomic::AtomicBool;

let mut some_bool = AtomicBool::new(true);
assert_eq!(*some_bool.get_mut(), true);
*some_bool.get_mut() = false;
assert_eq!(some_bool.load(), false);
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 keos::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);
assert_eq!(some_bool.into_inner(), true);
Source

pub fn load(&self) -> bool

Loads a value from the bool.

§Examples
use keos::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.load(), true);
Source

pub fn store(&self, val: bool)

Stores a value into the bool.

§Examples
use keos::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);

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

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

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

§Examples
use keos::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.swap(false), true);
assert_eq!(some_bool.load(), false);
Source

pub fn compare_exchange(&self, current: bool, new: bool) -> 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.

§Examples
use std::sync::atomic::AtomicBool;

let some_bool = AtomicBool::new(true);

assert_eq!(some_bool.compare_exchange(true, false), Ok(true));
assert_eq!(some_bool.load(), false);

assert_eq!(some_bool.compare_exchange(true, true), Err(false));
assert_eq!(some_bool.load(), false);
Source

pub fn fetch_and(&self, val: bool) -> 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.

§Examples
use keos::atomic::AtomicBool;

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

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

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

pub fn fetch_nand(&self, val: bool) -> 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.

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

§Examples
use keos::atomic::{AtomicBool, Ordering};

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

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

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

pub fn fetch_or(&self, val: bool) -> 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.

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

§Examples
use keos::atomic::{AtomicBool, Ordering};

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

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

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

pub fn fetch_xor(&self, val: bool) -> 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.

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

§Examples
use keos::atomic::{AtomicBool, Ordering};

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

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

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

pub fn fetch_not(&self) -> 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.

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

§Examples
use keos::atomic::AtomicBool;

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

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

pub fn fetch_update<F>(&self, 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. Returns a Result of Ok(previous_value) if the function returned Some(_), else Err(previous_value).

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.

§Examples
use keos::atomic::AtomicBool;

let x = AtomicBool::new(false);
assert_eq!(x.fetch_update(|_| None), Err(false));
assert_eq!(x.fetch_update(|x| Some(!x)), Ok(false));
assert_eq!(x.fetch_update(|x| Some(!x)), Ok(true));
assert_eq!(x.load(), false);

Trait Implementations§

Source§

impl Default for AtomicBool

Source§

fn default() -> AtomicBool

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

impl From<bool> for AtomicBool

Source§

fn from(b: bool) -> Self

Converts a bool into an AtomicBool.

§Examples
use keos::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")

Auto Trait Implementations§

§

impl !Freeze for AtomicBool

§

impl RefUnwindSafe for AtomicBool

§

impl Send for AtomicBool

§

impl Sync for AtomicBool

§

impl Unpin for AtomicBool

§

impl UnwindSafe for AtomicBool

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.