Trait event_listener_strategy::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.

Object Safety§

This trait is not object safe.

Implementors§

source§

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

source§

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

§

type Context = ()

§

type Future = Ready