Trait event_listener::Listener

source ·
pub trait Listener<T = ()>: Future<Output = T> + Sealed {
    // Required methods
    fn wait(self) -> T;
    fn wait_timeout(self, timeout: Duration) -> Option<T>;
    fn wait_deadline(self, deadline: Instant) -> Option<T>;
    fn discard(self) -> bool;
    fn listens_to(&self, event: &Event<T>) -> bool;
    fn same_event(&self, other: &Self) -> bool;
}
Expand description

A handle that is listening to an Event.

This trait represents a type waiting for a notification from an Event. See the EventListener type for more documentation on this trait’s usage.

Required Methods§

source

fn wait(self) -> T

Blocks until a notification is received.

§Examples
use event_listener::{Event, Listener};

let event = Event::new();
let mut listener = event.listen();

// Notify `listener`.
event.notify(1);

// Receive the notification.
listener.wait();
source

fn wait_timeout(self, timeout: Duration) -> Option<T>

Blocks until a notification is received or a timeout is reached.

Returns true if a notification was received.

§Examples
use std::time::Duration;
use event_listener::{Event, Listener};

let event = Event::new();
let mut listener = event.listen();

// There are no notification so this times out.
assert!(listener.wait_timeout(Duration::from_secs(1)).is_none());
source

fn wait_deadline(self, deadline: Instant) -> Option<T>

Blocks until a notification is received or a deadline is reached.

Returns true if a notification was received.

§Examples
use std::time::{Duration, Instant};
use event_listener::{Event, Listener};

let event = Event::new();
let mut listener = event.listen();

// There are no notification so this times out.
assert!(listener.wait_deadline(Instant::now() + Duration::from_secs(1)).is_none());
source

fn discard(self) -> bool

Drops this listener and discards its notification (if any) without notifying another active listener.

Returns true if a notification was discarded.

§Examples
use event_listener::{Event, Listener};

let event = Event::new();
let mut listener1 = event.listen();
let mut listener2 = event.listen();

event.notify(1);

assert!(listener1.discard());
assert!(!listener2.discard());
source

fn listens_to(&self, event: &Event<T>) -> bool

Returns true if this listener listens to the given Event.

§Examples
use event_listener::{Event, Listener};

let event = Event::new();
let listener = event.listen();

assert!(listener.listens_to(&event));
source

fn same_event(&self, other: &Self) -> bool

Returns true if both listeners listen to the same Event.

§Examples
use event_listener::{Event, Listener};

let event = Event::new();
let listener1 = event.listen();
let listener2 = event.listen();

assert!(listener1.same_event(&listener2));

Object Safety§

This trait is not object safe.

Implementors§