Skip to main content

IntoWorldResult

Trait IntoWorldResult 

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

    // Required method
    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>.

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§

Source

type World: World

World type itself.

Source

type Error

Error returned by this World constructor.

Set to Infallible in case construction is infallible.

Required Methods§

Source

fn into_world_result(self) -> Result<Self::World, Self::Error>

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

§Errors

In case the World construction errors.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<W: World, E> IntoWorldResult for Result<W, E>

Source§

type World = W

Source§

type Error = E

Source§

fn into_world_result(self) -> Self

Implementors§