Macro event_listener_strategy::easy_wrapper

source ·
macro_rules! easy_wrapper {
    (
        $(#[$meta:meta])*
        $vis:vis struct $name:ident

        $(<
            $( $lifetime:lifetime $(: $lifetime_bound:lifetime)? ),* $(,)?
            $( $generics:ident
                $(: $generics_bound:path)?
                $(: ?$generics_unsized_bound:path)?
                $(: $generics_lifetime_bound:lifetime)?
                $(= $generics_default:ty)?
            ),* $(,)?
        >)?

        ($inner:ty => $output:ty)

        $(where
            $( $where_clause_ty:ty
                $(: $where_clause_bound:path)?
                $(: ?$where_clause_unsized_bound:path)?
                $(: $where_clause_lifetime_bound:lifetime)?
            ),* $(,)?
        )?

        ;

        $(#[$wait_meta:meta])*
        $wait_vis: vis wait();
    ) => { ... };
}
Expand description

A wrapper around an EventListenerFuture that can be easily exported for use.

This type implements Future, has a _new() constructor, and a wait() method that uses the Blocking strategy to poll the future until it is ready.

§Examples

mod my_future {
    use event_listener_strategy::{easy_wrapper, EventListenerFuture, Strategy};
    use std::pin::Pin;
    use std::task::Poll;

    struct MyFuture;

    impl EventListenerFuture for MyFuture {
        type Output = ();

        fn poll_with_strategy<'a, S: Strategy<'a>>(
            self: Pin<&mut Self>,
            strategy: &mut S,
            context: &mut S::Context,
        ) -> Poll<Self::Output> {
            /* ... */
        }
    }

    easy_wrapper! {
        /// A future that does something.
        pub struct MyFutureWrapper(MyFuture => ());
        /// Wait for it.
        pub wait();
    }

    impl MyFutureWrapper {
        /// Create a new instance of the future.
        pub fn new() -> Self {
            Self::_new(MyFuture)
        }
    }
}

use my_future::MyFutureWrapper;

// Use the future in a blocking context.
let future = MyFutureWrapper::new();
future.wait();

// Use the future in a non-blocking context.
futures_lite::future::block_on(async {
    let future = MyFutureWrapper::new();
    future.await;
});