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

    // Required methods
    fn poll<T>(
        &mut self,
        event_listener: Pin<&mut EventListener<T>>,
        context: &mut Self::Context
    ) -> Poll<T>;
    fn wait(&mut self, evl: Pin<&'a mut 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::{Event, EventListener};
use event_listener_strategy::{EventListenerFuture, Strategy, Blocking, NonBlocking};
use std::pin::Pin;

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

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

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

// Poll the future.
listener.as_mut().listen(&ev);
ev.notify(1);

wait_on(listener.as_mut(), &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>( &mut self, event_listener: Pin<&mut EventListener<T>>, context: &mut Self::Context ) -> Poll<T>

Poll the event listener until it is ready.

source

fn wait(&mut self, evl: Pin<&'a mut EventListener>) -> Self::Future

Wait for the event listener to become ready.

Object Safety§

This trait is not object safe.

Implementors§

source§

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

§

type Context = Context<'a>

§

type Future = Pin<&'evl mut EventListener>

source§

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

§

type Context = ()

§

type Future = Ready