pub struct NotifyAggregator { /* private fields */ }
Expand description
This struct is used to aggregate multiple Notify
instances into
effectively a single Notify
instance.
Calling NotifyAggregator::notified
will wait for a notification on any of the registered
Notify
instances.
Implementations§
Source§impl NotifyAggregator
impl NotifyAggregator
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new NotifyAggregator
.
§Examples
use tokio_notify_aggregator::NotifyAggregator;
let aggregator = NotifyAggregator::new();
Sourcepub fn from_vec(vec: &Vec<Arc<Notify>>) -> Self
pub fn from_vec(vec: &Vec<Arc<Notify>>) -> Self
Constructs a new NotifyAggregator
with the Arc<Notify>
instances from vec
.
§Examples
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let notifiers = vec![Arc::new(Notify::new()), Arc::new(Notify::new())];
let aggregator = NotifyAggregator::from_vec(¬ifiers);
Sourcepub fn add_notifier(&self, notifier: Arc<Notify>) -> &Self
pub fn add_notifier(&self, notifier: Arc<Notify>) -> &Self
Registers a new Notify
instance to be tracked.
§Examples
Simple usage:
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let aggregator = NotifyAggregator::new();
let notifier = Arc::new(Notify::new());
aggregator.add_notifier(notifier.clone());
Chained addition:
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let notifier1 = Arc::new(Notify::new());
let notifier2 = Arc::new(Notify::new());
let aggregator = NotifyAggregator::new()
.add_notifier(notifier1.clone())
.add_notifier(notifier2.clone());
Sourcepub fn notified(&self) -> Notified<'_>
pub fn notified(&self) -> Notified<'_>
Wait for a notification on any of the registered Notify
instances.
§Examples
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
#[tokio::main]
async fn main() {
let aggregator = NotifyAggregator::new();
let notifier1 = Arc::new(Notify::new());
let notifier2 = Arc::new(Notify::new());
aggregator.add_notifier(notifier1.clone());
aggregator.add_notifier(notifier2.clone());
tokio::spawn(async move {
loop {
aggregator.notified().await;
println!("Notified!");
}
});
notifier1.notify_waiters();
notifier2.notify_waiters();
notifier1.notify_waiters();
notifier2.notify_waiters();
}
Trait Implementations§
Source§impl Clone for NotifyAggregator
impl Clone for NotifyAggregator
Source§fn clone(&self) -> NotifyAggregator
fn clone(&self) -> NotifyAggregator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for NotifyAggregator
impl Debug for NotifyAggregator
Source§impl Default for NotifyAggregator
impl Default for NotifyAggregator
Source§fn default() -> NotifyAggregator
fn default() -> NotifyAggregator
Source§impl Drop for NotifyAggregator
impl Drop for NotifyAggregator
Source§fn drop(&mut self)
fn drop(&mut self)
Handles the cleanup logic for the NotifyAggregator
, by signaling all spawned tasks
(one per registered Notify
) to stop.
Source§impl<'a> From<&'a Vec<Arc<Notify>>> for NotifyAggregator
impl<'a> From<&'a Vec<Arc<Notify>>> for NotifyAggregator
Source§fn from(vec: &'a Vec<Arc<Notify>>) -> Self
fn from(vec: &'a Vec<Arc<Notify>>) -> Self
Constructs a new NotifyAggregator
with the Arc<Notify>
instances from vec
.
§Examples
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let notifiers = vec![Arc::new(Notify::new()), Arc::new(Notify::new())];
let aggregator = NotifyAggregator::from(¬ifiers);
Source§impl From<Vec<Arc<Notify>>> for NotifyAggregator
impl From<Vec<Arc<Notify>>> for NotifyAggregator
Source§fn from(vec: Vec<Arc<Notify>>) -> Self
fn from(vec: Vec<Arc<Notify>>) -> Self
Constructs a new NotifyAggregator
with the Arc<Notify>
instances from vec
.
§Examples
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let notifiers = vec![Arc::new(Notify::new()), Arc::new(Notify::new())];
let aggregator = NotifyAggregator::from(notifiers);
Source§impl FromIterator<Arc<Notify>> for NotifyAggregator
impl FromIterator<Arc<Notify>> for NotifyAggregator
Source§fn from_iter<T: IntoIterator<Item = Arc<Notify>>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = Arc<Notify>>>(iter: T) -> Self
Constructs a new NotifyAggregator
with the Arc<Notify>
instances from iter
.
§Examples
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_notify_aggregator::NotifyAggregator;
let notifiers = vec![Arc::new(Notify::new()), Arc::new(Notify::new())];
let aggregator = NotifyAggregator::from_iter(notifiers.into_iter());