Struct higher::effect::Effect

source ·
pub struct Effect<'a, A> { /* private fields */ }
Expand description

An effect monad.

This is essentially just a newtype for a boxed Future that implements Monad, so that you can treat your Futures like they’re Monads with the run! macro and all the category theory you like.

To turn a Future into an Effect monad, use From and Into:

let my_future = async { "Hello Joe!" };
let my_effect = Effect::from(my_future);

Effects can be awaited like they’re futures, because they implement IntoFuture:

assert_eq!(my_effect.await, "Hello Joe!");

You can compose effects using the run! macro:

run! {
    // Lift the value 1 into the Effect monad
    x <= Effect::pure(1);
    // Create an effect from an async block returning the value 2
    y <= Effect::from(async { 2 });
    // Perform a computation in an async block using the previously bound values
    z <= Effect::from(async move { x + y });
    // Compute the result and await it
    yield x + y + z
}.await

Trait Implementations§

Formats the value using the given formatter. Read more
Converts to this type from the input type.
The output that the future will produce on completion.
Which kind of future are we turning this into?
Creates a future from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.