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
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
Implementations§
source§impl<'a, A> Effect<'a, A>
impl<'a, A> Effect<'a, A>
sourcepub fn ready(value: A) -> Selfwhere
A: 'a,
pub fn ready(value: A) -> Selfwhere
A: 'a,
Construct an effect that resolves immediately to the given value.
sourcepub fn select<B>(
eff1: Effect<'a, A>,
eff2: Effect<'a, B>
) -> Effect<'a, Either<(A, Effect<'a, B>), (B, Effect<'a, A>)>>where
A: 'a,
B: 'a,
pub fn select<B>(
eff1: Effect<'a, A>,
eff2: Effect<'a, B>
) -> Effect<'a, Either<(A, Effect<'a, B>), (B, Effect<'a, A>)>>where
A: 'a,
B: 'a,
Runs two effects in parallel, returning the result of whichever finishes first.
sourcepub fn select_all<I>(iter: I) -> Effect<'a, (A, usize, Vec<Effect<'a, A>>)>where
A: 'a,
I: IntoIterator<Item = Effect<'a, A>> + 'a,
pub fn select_all<I>(iter: I) -> Effect<'a, (A, usize, Vec<Effect<'a, A>>)>where
A: 'a,
I: IntoIterator<Item = Effect<'a, A>> + 'a,
Runs a list of effects in parallel, returning the result of whichever finishes first.
sourcepub fn join<B>(eff1: Effect<'a, A>, eff2: Effect<'a, B>) -> Effect<'a, (A, B)>where
A: 'a,
B: 'a,
pub fn join<B>(eff1: Effect<'a, A>, eff2: Effect<'a, B>) -> Effect<'a, (A, B)>where
A: 'a,
B: 'a,
Runs two effects in parallel, returning the results of both.
sourcepub fn join3<B, C>(
eff1: Effect<'a, A>,
eff2: Effect<'a, B>,
eff3: Effect<'a, C>
) -> Effect<'a, (A, B, C)>where
A: 'a,
B: 'a,
C: 'a,
pub fn join3<B, C>(
eff1: Effect<'a, A>,
eff2: Effect<'a, B>,
eff3: Effect<'a, C>
) -> Effect<'a, (A, B, C)>where
A: 'a,
B: 'a,
C: 'a,
Runs three effects in parallel, returning the results of all three.
Trait Implementations§
source§impl<'a, A> Apply<'a, A> for Effect<'a, A>where
A: Clone + 'a,
impl<'a, A> Apply<'a, A> for Effect<'a, A>where
A: Clone + 'a,
source§fn apply<B>(self, f: Self::Target<ApplyFn<'a, A, B>>) -> Self::Target<B>where
B: 'a,
fn apply<B>(self, f: Self::Target<ApplyFn<'a, A, B>>) -> Self::Target<B>where
B: 'a,
Apply an
F
of functions from A
to B
to an F
of A
,
producing an F
of B
.fn apply_first<B>(self, b: Self::Target<B>) -> Selfwhere
B: 'a,
Self: Sized,
A: Clone,
Self::Target<B>: Apply<'a, B, Target<ApplyFn<'a, B, A>> = Self::Target<ApplyFn<'a, B, A>>>,
fn apply_second<B>(self, b: Self::Target<B>) -> Self::Target<B>where
B: 'a + Clone,
Self: Sized,
Self::Target<B>: Apply<'a, B, Target<ApplyFn<'a, A, B>> = Self::Target<ApplyFn<'a, A, B>>>,
source§impl<'a, A: 'a> Functor<'a, A> for Effect<'a, A>
impl<'a, A: 'a> Functor<'a, A> for Effect<'a, A>
type Target<T: 'a> = Effect<'a, T>
source§fn fmap<B, F>(self, f: F) -> Self::Target<B>where
B: 'a,
F: Fn(A) -> B + 'a,
fn fmap<B, F>(self, f: F) -> Self::Target<B>where
B: 'a,
F: Fn(A) -> B + 'a,
Map a functor of
A
to a functor of B
using a function from A
to B
.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