Struct async_events::AsyncEvents
source · 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>
impl<K, T> AsyncEvents<K, T>
source§impl<K, V> AsyncEvents<K, V>where
K: Eq,
impl<K, V> AsyncEvents<K, V>where K: Eq,
sourcepub fn wait_for_output(&self, event_id: K) -> Observer<V> ⓘ
👎Deprecated: Please use output_of instead
pub fn wait_for_output(&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.
sourcepub fn output_of(&self, event_id: K) -> Observer<V> ⓘ
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;
sourcepub fn resolve_all_with(&self, event_ids: &[K], output: V)where
V: Clone,
pub fn resolve_all_with(&self, event_ids: &[K], output: V)where V: Clone,
Resolves all the pending Observer
s 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 inevent_ids
. However in some error code paths it is not unusual that you can rule provide a result (usuallyErr
) for many events at once.output
: The result theseObserver
s will return in their.await
call
sourcepub fn resolve_all_if(&self, f: impl Fn(&K) -> Option<V>)where
V: Clone,
pub fn resolve_all_if(&self, f: impl Fn(&K) -> Option<V>)where V: Clone,
Resolves all the pending Observer
s. This resolves all events independent of their id.
This might come in useful e.g. during application shutdown.
output
: The result theseObserver
s will return in their.await
callf
: Function acting as a filter for event ids which are to be resolved, and as a factory for their results. Iff
returnsNone
observers associated with the event id are not resolved. IfSome
all observers with this Id are resolved.