Struct higher_effect::IO

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

An IO monad.

This wraps a Future similarly to the Effect monad, but the wrapped effect must return a Result. Just as with Result’s Bind implementation, this also short circuits a chain of computations if a step produces an error, resolving immediately to that error.

You can construct an IO monad from a Future which returns a Result:

let my_io_monad = IO::<&str, &str>::from(async { Ok("Hello Joe!") });

You can .await an IO monad and get a Result back:

assert_eq!(my_io_monad.await, Ok("Hello Joe!"));

You can run your IO monads on a thread local executor using the run() method. Naturally, we also provide a version of Haskell’s putStrLn so that we can implement the canonical hello world in monadic Rust:

put_str_ln("Hello Simon!").run();

Because IO implements Bind, you can chain async IO operations together using the run! macro:

run! {
    put_str("What is your name? ");
    name <= get_line();
    put_str_ln(format!("Hello {}!", name))
}.run();

Implementations§

Run the effect to completion, blocking the current thread.

Transform the effect’s success value using the given function.

If the effect fails, the function is not applied.

This is identical to Functor::fmap, except it accepts a FnOnce instead of a Fn.

Transform the effect’s error value using the given function.

If the effect succeeds, the function is not applied.

This is identical to Bifunctor::rmap, except it accepts a FnOnce instead of a Fn.

Test whether this effect is known to have already failed.

Run two effects in parallel, returning the result of whichever finishes first, or the error from whichever fails first.

Run a list of effects in parallel, returning the result of whichever finishes first, or the first error to occur.

Run two effects in parallel, returning the results of both, or the error from whichever fails first.

Run three effects in parallel, returning the results of all three, or the error from whichever fails first.

Run four effects in parallel, returning the results of all four, or the error from whichever fails first.

Run a list of effects in parallel, returning a list of their results, or the error from whichever fails first.

Trait Implementations§

Throw an error. Read more
Handle an error. Read more
Handle an error. Read more
Apply an F of functions from A to B to an F of A, producing an F of B.
Map a Bifunctor<A, B> to a Bifunctor<C, D> using a function from A to C and a function from B to D.
Map only the left hand side of the bifunctor from A to C.
Map only the right hand side of the bifunctor from B to D.
Apply the function f to the A or As inside the M<A> to turn it into an M<B>. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Map a functor of A to a functor of B using a function from A to B.
Map the functor to the provided constant value.
Map the functor to the unit value ().
Turn the functor into an iterator. Read more
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.