Trait cucumber::codegen::ToWorldFuture

source ·
pub trait ToWorldFuture {
    type Future: Future;

    // Required method
    fn to_world_future(&self) -> Self::Future;
}
Available on crate feature macros only.
Expand description

Return-type polymorphism over asyncness for a #[world(init)] attribute of a #[derive(World)] macro.

It allows to accept both sync and async functions as an attribute’s argument, by automatically wrapping sync functions in a future::Ready.

impl cucumber::World for World {
    type Error = anyhow::Error;

    async fn new() -> Result<Self, Self::Error> {
        use cucumber::codegen::{
            IntoWorldResult as _, ToWorldFuture as _,
        };

        fn as_fn_ptr<T>(v: fn() -> T) -> fn() -> T {
            v
        }

        //           `#[world(init)]`'s value
        //          ⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄⌄
        (&as_fn_ptr(<Self as Default>::default))
            .to_world_future() // maybe wraps into `future::Ready`
            .await
            .into_world_result()
            .map_err(Into::into)
    }
}

Required Associated Types§

source

type Future: Future

Future returned by this World constructor.

Set to future::Ready in case construction is sync.

Required Methods§

source

fn to_world_future(&self) -> Self::Future

Resolves this Future for constructing a newWorld using autoderef-based specialization.

Implementations on Foreign Types§

source§

impl<Fut: Future> ToWorldFuture for &fn() -> Fut
where Fut::Output: IntoWorldResult,

§

type Future = Fut

source§

fn to_world_future(&self) -> Self::Future

source§

impl<R: IntoWorldResult> ToWorldFuture for fn() -> R

§

type Future = Ready<R>

source§

fn to_world_future(&self) -> Self::Future

Implementors§