pub struct AsyncEvents<K, T> { /* private fields */ }
Expand description

Allows to create futures which will not complete until an associated event id is resolved. This is useful for creating futures waiting for completion on external events which are driven to completion outside of the current process.

use std::time::Duration;
use async_events::AsyncEvents;

async fn foo(events: &AsyncEvents<u32, &'static str>) {
    // This is a future waiting for an event with key `1` to resover its output.
    let observer = events.output_of(1);
    // We can have multiple observers for the same event, if we want to.
    let another_observer = events.output_of(1);

    // This will block until the event is resolved
    let result = observer.await;
    // Do something awesome with result
    println!("{result}");
}

async fn bar(events: &AsyncEvents<u32, &'static str>) {
    // All observers waiting for `1` wake up and their threads may continue. You could resolve
    // multiple events at once with the same result. This wakes up every observer associated
    // with the event.
    events.resolve_all_with(&[1], "Hello, World");
}

Implementations§

source§

impl<K, T> AsyncEvents<K, T>

source

pub fn new() -> Self

source§

impl<K, V> AsyncEvents<K, V>where K: Eq,

source

pub fn wait_for_output(&self, event_id: K) -> Observer<V>

👎Deprecated: Please use output_of instead

A future associated with a peer, which can be resolved using resolve_with. You can call this method repeatedly to create multiple observers waiting for the same event.

source

pub fn output_of(&self, event_id: K) -> Observer<V>

A future associated with a peer, which can be resolved using resolve_with. You can call this method repeatedly to create multiple observers waiting for the same event.

Events are created implicitly by creating futures waiting for them. They are removed then it is resolved. Waiting on an already resolved event will hang forever.

use async_events::AsyncEvents;

let events = AsyncEvents::<u32, u32>::new();

// Event occurs before we created the observer
events.resolve_all_with(&[1], 42);

// Oh no, event `1` has already been resolved. This is likely to wait forever.
let answer = events.output_of(1).await;
source

pub fn resolve_all_with(&self, event_ids: &[K], output: V)where V: Clone,

Resolves all the pending Observers associated with the given ids.

  • event_ids: Observers associated with these ids are resolved. It would be typical to call this method with only one element in event_ids. However in some error code paths it is not unusual that you can rule provide a result (usually Err) for many events at once.
  • output: The result these Observers will return in their .await call
source

pub fn resolve_all_if(&self, f: impl Fn(&K) -> Option<V>)where V: Clone,

Resolves all the pending Observers. This resolves all events independent of their id. This might come in useful e.g. during application shutdown.

  • output: The result these Observers will return in their .await call
  • f: Function acting as a filter for event ids which are to be resolved, and as a factory for their results. If f returns None observers associated with the event id are not resolved. If Some all observers with this Id are resolved.
source

pub fn resolve_one(&self, event_id: K, output: V)

Resolves one pending Observer associated with the given event id. If no observer with such an id exists, nothing happens.

  • event_id: One Observer associated with this ids is resolved.
  • output: The result the Observer will return in its .await call

Trait Implementations§

source§

impl<K, T> Default for AsyncEvents<K, T>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, T> RefUnwindSafe for AsyncEvents<K, T>

§

impl<K, T> Send for AsyncEvents<K, T>where K: Send, T: Send,

§

impl<K, T> Sync for AsyncEvents<K, T>where K: Send, T: Send,

§

impl<K, T> Unpin for AsyncEvents<K, T>where K: Unpin,

§

impl<K, T> UnwindSafe for AsyncEvents<K, T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.