Enum higher::io::IO

source ·
pub enum IO<'a, A, E> {
    Future(LocalBoxFuture<'a, Result<A, E>>),
    Error(E),
}
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_io function. Naturally, we also provide a version of Haskell’s putStrLn so that we can implement the canonical hello world in monadic Rust:

run_io(putstrln("Hello Simon!"));

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

run_io(run! {
    putstrln("I have the power");
    putstrln("of");
    putstrln("HASKELL")
});

Variants§

§

Future(LocalBoxFuture<'a, Result<A, E>>)

§

Error(E)

Implementations§

Trait Implementations§

Formats the value using the given formatter. Read more
Converts to this type from the input type.
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.