AwaitableBool

Struct AwaitableBool 

Source
pub struct AwaitableBool { /* private fields */ }
Expand description

A bool whose value changes can be waited on.

Internally, it uses AtomicBool and tokio::sync::Notify.

Because of that, most methods only need a shared reference (& as opposed to &mut) to self, so sharing between tasks or threads should be cheap and easy. This struct doesn’t implement Clone, so place it in an Arc (no lock protecting it necessary per the previous sentence) if cloning is needed.

Implementations§

Source§

impl AwaitableBool

Source

pub fn new<IntoAtomicBool: Into<AtomicBool>>(value: IntoAtomicBool) -> Self

Creates a new AwaitableBool.

§Examples
§Specify an initial value
use awaitable_bool::AwaitableBool;

let initially_true = AwaitableBool::new(true);
let initially_false = AwaitableBool::new(false);
§Use an existing AtomicBool to make an AwaitableBool
use std::sync::atomic::AtomicBool;
use awaitable_bool::AwaitableBool;

let atomic_bool = AtomicBool::new(false);
let awaitable_bool = AwaitableBool::new(atomic_bool);
Source

pub fn set_true(&self)

Set the AwaitableBool to true (with Release ordering if not already true and Relaxed ordering if it is).

This wakes all tasks waiting for wait_true. It also wakes those waiting for wait if the value wasn’t already true.

Source

pub fn set_false(&self)

Set the AwaitableBool to false (with Release ordering if not already false and Relaxed ordering if it is).

This wakes all tasks waiting for wait_false. It also wakes those waiting for wait if the value wasn’t already false.

Source

pub fn toggle(&self)

Set the AwaitableBool to the inverse of its current value (i.e. false if true or true if false) (with Release ordering).

This wakes all tasks waiting for wait. It also wakes those waiting for wait_true if the value was just changed from false to true, or those waiting for wait_false if the value was just changed from true to false.

Source

pub fn load(&self) -> bool

Get the current value of the AwaitableBool (with Acquire ordering).

Source

pub fn is_true(&self) -> bool

Check if the AwaitableBool’s value is currently true (with Acquire ordering).

Source

pub fn is_false(&self) -> bool

Check if the AwaitableBool’s value is currently false (with Acquire ordering).

Source

pub async fn wait(&self)

Wait for this AwaitableBool’s value to change.

Use load after to know what it changed to.

Source

pub async fn wait_true(&self)

Wait for this AwaitableBool’s value to become true. This returns immediately if it’s already true.

Source

pub async fn wait_false(&self)

Wait for this AwaitableBool’s value to become false. This returns immediately if it’s already false.

Source

pub const fn into_inner(self) -> AtomicBool

Consume this AwaitableBool to get the contained AtomicBool.

AtomicBool also has an into_inner method to get its contained bool.

Trait Implementations§

Source§

impl Debug for AwaitableBool

Source§

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

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

impl Default for AwaitableBool

Source§

fn default() -> AwaitableBool

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

impl<T: Into<AtomicBool>> From<T> for AwaitableBool

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

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
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 = Infallible

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

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

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.