Struct Notify

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

Notify a single task to wake up.

Notify provides a basic mechanism to notify a single task of an event. Notify itself does not carry any data. Instead, it is to be used to signal another task to perform an operation.

If notify() is called before notified().await, then the next call to notified().await will complete immediately, consuming the permit. Any subsequent calls to notified().await will wait for a new permit.

If notify() is called multiple times before notified().await, only a single permit is stored. The next call to notified().await will complete immediately, but the one after will wait for a new permit.

§Examples

Basic usage.

use std::sync::Arc;
use async_notify::Notify;

async_global_executor::block_on(async {
   let notify = Arc::new(Notify::new());
   let notify2 = notify.clone();

   async_global_executor::spawn(async move {
       notify2.notify();
       println!("sent notification");
   })
   .detach();

   println!("received notification");
   notify.notified().await;
})

Implementations§

Source§

impl Notify

Like tokio Notify, this is a runtime independent Notify.

Source

pub fn new() -> Self

Source

pub fn notify(&self)

Notifies a waiting task

If a task is currently waiting, that task is notified. Otherwise, a permit is stored in this Notify value and the next call to notified().await will complete immediately consuming the permit made available by this call to notify().

At most one permit may be stored by Notify. Many sequential calls to notify will result in a single permit being stored. The next call to notified().await will complete immediately, but the one after that will wait.

§Examples
use std::sync::Arc;
use async_notify::Notify;

async_global_executor::block_on(async {
   let notify = Arc::new(Notify::new());
   let notify2 = notify.clone();

   async_global_executor::spawn(async move {
       notify2.notify();
       println!("sent notification");
   })
   .detach();

   println!("received notification");
   notify.notified().await;
})
Source

pub async fn notified(&self)

Wait for a notification.

Each Notify value holds a single permit. If a permit is available from an earlier call to notify(), then notified().await will complete immediately, consuming that permit. Otherwise, notified().await waits for a permit to be made available by the next call to notify().

This method is cancel safety.

Trait Implementations§

Source§

impl<T: Deref<Target = Notify>> AsRef<Notify> for NotifyStream<T>

Source§

fn as_ref(&self) -> &Notify

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Notify

Source§

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

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

impl Default for Notify

Source§

fn default() -> Notify

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

Auto Trait Implementations§

§

impl !Freeze for Notify

§

impl RefUnwindSafe for Notify

§

impl Send for Notify

§

impl Sync for Notify

§

impl Unpin for Notify

§

impl UnwindSafe for Notify

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.