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§
Implementations§
Trait Implementations§
source§impl<'a, A, E: 'a> IntoFuture for IO<'a, A, E>
impl<'a, A, E: 'a> IntoFuture for IO<'a, A, E>
§type IntoFuture = Pin<Box<dyn Future<Output = Result<A, E>> + 'a, Global>>
type IntoFuture = Pin<Box<dyn Future<Output = Result<A, E>> + '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