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>
Source§type IntoFuture = Pin<Box<dyn Future<Output = A> + 'a>>
type IntoFuture = Pin<Box<dyn Future<Output = A> + 'a>>
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
Auto Trait Implementations§
impl<'a, A> Freeze for Effect<'a, A>
impl<'a, A> !RefUnwindSafe for Effect<'a, A>
impl<'a, A> !Send for Effect<'a, A>
impl<'a, A> !Sync for Effect<'a, A>
impl<'a, A> Unpin for Effect<'a, A>
impl<'a, A> !UnwindSafe for Effect<'a, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more