[−][src]Struct event_listener::Event
A synchronization primitive for notifying async tasks and threads.
Listeners can be registered using Event::listen()
. There are threifour ways of notifying
listeners:
Event::notify_one()
notifies one listener.Event::notify_all()
notifies all listeners.Event::notify()
notifies an arbitrary number of listeners.Event::notify_additional()
notifies an arbitrary number of unnotified listeners.
If there are no active listeners at the time a notification is sent, it simply gets lost.
Note that Event::notify_one()
does not notify one additional listener - it only makes
sure at least one listener among the active ones is notified. Similarly, Event::notify()
makes sure a number of active listeners are notified. If you need to notify additional
listeners, use Event::notify_additional()
.
There are two ways for a listener to wait for a notification:
- In an asynchronous manner using
.await
. - In a blocking manner by calling
EventListener::wait()
on it.
If a notified listener is dropped without receiving a notification, dropping will notify another active listener.
Listeners are registered and notified in the first-in first-out fashion, ensuring fairness.
Implementations
impl Event
[src]
pub fn new() -> Event
[src]
pub fn listen(&self) -> EventListener
[src]
Returns a guard listening for a notification.
Examples
use event_listener::Event; let event = Event::new(); let listener = event.listen();
pub fn notify(&self, n: usize)
[src]
Notifies a number of active listeners.
The number is allowed to be zero or exceed the current number of listeners.
Note that this does not notify n
additional listeners - it only makes sure at least
n
listeners among the active ones are notified.
Examples
use event_listener::Event; let event = Event::new(); // This notification gets lost because there are no listeners. event.notify(1); let listener1 = event.listen(); let listener2 = event.listen(); let listener3 = event.listen(); // Notifies two listeners. // // Listener queueing is fair, which means `listener1` and `listener2` // get notified here since they start listening before `listener3`. event.notify_one();
pub fn notify_additional(&self, n: usize)
[src]
Notifies a number of active and still unnotified listeners.
The number is allowed to be zero or exceed the current number of listeners.
In contrast to Event::notify()
, this method will notify n
additional listeners that
were previously unnotified.
Examples
use event_listener::Event; let event = Event::new(); // This notification gets lost because there are no listeners. event.notify(1); let listener1 = event.listen(); let listener2 = event.listen(); let listener3 = event.listen(); // Notifies two listeners. // // Listener queueing is fair, which means `listener1` and `listener2` // get notified here since they start listening before `listener3`. event.notify_one();
pub fn notify_one(&self)
[src]
Notifies a single active listener.
Note that this does not notify one additional listener - it only makes sure at least one listener among the active ones is notified.
This method is equivalent to self.notify(1)
.
Examples
use event_listener::Event; let event = Event::new(); // This notification gets lost because there are no listeners. event.notify_one(); let listener1 = event.listen(); let listener2 = event.listen(); // Notifies just one of `listener1` and `listener2`. // // Listener queueing is fair, which means `listener1` gets notified // here since it was the first to start listening. event.notify_one();
pub fn notify_all(&self)
[src]
Notifies all active listeners.
This method is equivalent to self.notify(usize::MAX)
.
Examples
use event_listener::Event; let event = Event::new(); // This notification gets lost because there are no listeners. event.notify_all(); let listener1 = event.listen(); let listener2 = event.listen(); // Both `listener1` and `listener2` get notified. event.notify_all();
Trait Implementations
impl Debug for Event
[src]
impl Default for Event
[src]
impl Drop for Event
[src]
impl Send for Event
[src]
impl Sync for Event
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,