Struct 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§

Source§

impl<'a, A> Apply<'a, A> for Effect<'a, A>
where A: 'a,

Source§

type Target<T> = Effect<'a, T> where T: 'a

Source§

fn apply<B>( self, f: <Self as Apply<'a, A>>::Target<ApplyFn<'a, A, B>>, ) -> <Self as Apply<'a, A>>::Target<B>
where B: 'a,

Source§

impl<'a, A> Bind<'a, A> for Effect<'a, A>
where A: 'a,

Source§

type Target<T> = Effect<'a, T>

Source§

fn bind<B, F>(self, f: F) -> Self::Target<B>
where F: Fn(A) -> Self::Target<B> + 'a,

Source§

impl<'a, A> Debug for Effect<'a, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, A, F> From<F> for Effect<'a, A>
where F: Future<Output = A> + 'a,

Source§

fn from(future: F) -> Self

Converts to this type from the input type.
Source§

impl<'a, A> Functor<'a, A> for Effect<'a, A>
where A: 'a,

Source§

type Target<T> = Effect<'a, T>

Source§

fn fmap<B, F>(self, f: F) -> Self::Target<B>
where F: Fn(A) -> B + 'a,

Source§

impl<'a, A> IntoFuture for Effect<'a, A>

Source§

type Output = A

The output that the future will produce on completion.
Source§

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

Creates a future from a value. Read more
Source§

impl<'a, A> Pure<A> for Effect<'a, A>
where A: 'a,

Source§

fn pure(value: A) -> Self

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<'a, M, A> Applicative<'a, A> for M
where M: Functor<'a, A> + Apply<'a, A> + Pure<A>,

Source§

impl<'a, M, A> Monad<'a, A> for M
where M: Bind<'a, A> + Applicative<'a, A>,