logo
pub trait IntoWorldResult: Sized {
    type World: World;
    type Error;

    fn into_world_result(self) -> Result<Self::World, Self::Error>;
}
Available on crate feature macros only.
Expand description

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

It allows to accept both fallible (returning Result) and infallible functions as an attribute’s argument, by automatically wrapping infallible functions in a Result<World, Infallible>.

#[async_trait(?Send)]
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()
            .await
            .into_world_result() // maybe wraps into `Result<_, Infallible>`
            .map_err(Into::into)
    }
}

Required Associated Types

World type itself.

Error returned by this World constructor.

Set to Infallible in case construction is infallible.

Required Methods

Passes Result<World, Self::Error> as is, or wraps the plain World in a Result<World, Infallible>.

Errors

In case the World construction errors.

Implementations on Foreign Types

Implementors