event_listener_strategy

Trait Strategy

Source
pub trait Strategy<'a> {
    type Context: ?Sized;
    type Future: Future + 'a;

    // Required methods
    fn poll<T, L: Listener<T> + Unpin>(
        &mut self,
        event_listener: &mut Option<L>,
        context: &mut Self::Context,
    ) -> Poll<T>;
    fn wait(&mut self, evl: EventListener) -> Self::Future;
}
Expand description

A strategy for polling an EventListenerFuture or an EventListener.

This trait is used by the EventListenerFuture::poll_with_strategy method to determine how to poll the future. It can also be used standalone, by calling the Strategy::wait method.

§Examples

use event_listener_strategy::{
   event_listener::{Event, EventListener},
   EventListenerFuture, Strategy, Blocking, NonBlocking
};
use std::pin::Pin;

async fn wait_on<'a, S: Strategy<'a>>(evl: EventListener, strategy: &mut S) {
    strategy.wait(evl).await;
}

// Block on the future.
let ev = Event::new();
let listener = ev.listen();
ev.notify(1);

wait_on(listener, &mut Blocking::default()).await;

// Poll the future.
let listener = ev.listen();
ev.notify(1);

wait_on(listener, &mut NonBlocking::default()).await;

Required Associated Types§

Source

type Context: ?Sized

The context needed to poll the future.

Source

type Future: Future + 'a

The future returned by the Strategy::wait method.

Required Methods§

Source

fn poll<T, L: Listener<T> + Unpin>( &mut self, event_listener: &mut Option<L>, context: &mut Self::Context, ) -> Poll<T>

Poll the event listener until it is ready.

Source

fn wait(&mut self, evl: EventListener) -> Self::Future

Wait for the event listener to become ready.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a, 'evl> Strategy<'evl> for NonBlocking<'a>

Source§

impl<'evl> Strategy<'evl> for Blocking

Source§

type Context = ()

Source§

type Future = Ready