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
Future
s like they’re Monad
s 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§
source§impl<'a, A> IntoFuture for Effect<'a, A>
impl<'a, A> IntoFuture for Effect<'a, A>
§type IntoFuture = Pin<Box<dyn Future<Output = A> + 'a, Global>>
type IntoFuture = Pin<Box<dyn Future<Output = A> + 'a, Global>>
Which kind of future are we turning this into?
source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Creates a future from a value. Read more