pub struct IO<A> { /* private fields */ }Expand description
A lazy, composable IO effect type.
IO<A> represents a computation that, when executed, performs side effects
and produces a value of type A (or fails with an error).
Nothing executes until .run() is called — this gives referential transparency,
deferred execution, and composable effect descriptions.
§Design
- One type parameter:
IO<A>keeps signatures clean. Errors are type-erased viaanyhow::Errorand surfaced at.run()boundaries. For typed errors, useIO<Result<A, E>>— opt-in, not mandatory. Send + 'static: Compatible with tokio and thread pools.FnOnce: Consumed exactly once.
§Examples
use functype_io::IO;
let io = IO::succeed(42);
assert_eq!(io.run().unwrap(), 42);
let io = IO::effect(|| 1 + 2).map(|x| x * 10);
assert_eq!(io.run().unwrap(), 30);Implementations§
Source§impl<A> IO<A>where
A: Send + 'static,
impl<A> IO<A>where
A: Send + 'static,
Sourcepub fn fail(e: impl Into<Error> + Send + 'static) -> IO<A>
pub fn fail(e: impl Into<Error> + Send + 'static) -> IO<A>
Creates an IO that fails with the given error.
Sourcepub fn effect(f: impl FnOnce() -> A + Send + 'static) -> IO<A>
pub fn effect(f: impl FnOnce() -> A + Send + 'static) -> IO<A>
Creates an IO from an infallible side effect.
Sourcepub fn effect_result(
f: impl FnOnce() -> Result<A, Error> + Send + 'static,
) -> IO<A>
pub fn effect_result( f: impl FnOnce() -> Result<A, Error> + Send + 'static, ) -> IO<A>
Creates an IO from a fallible side effect (primary constructor).
Sourcepub fn from_result(
result: Result<A, impl Into<Error> + Send + 'static>,
) -> IO<A>
pub fn from_result( result: Result<A, impl Into<Error> + Send + 'static>, ) -> IO<A>
Lifts a Result into an IO.
Source§impl<A> IO<A>where
A: Send + 'static,
impl<A> IO<A>where
A: Send + 'static,
Sourcepub fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> IO<B>where
B: Send + 'static,
pub fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> IO<B>where
B: Send + 'static,
Transforms the success value.
Sourcepub fn flat_map<B>(self, f: impl FnOnce(A) -> IO<B> + Send + 'static) -> IO<B>where
B: Send + 'static,
pub fn flat_map<B>(self, f: impl FnOnce(A) -> IO<B> + Send + 'static) -> IO<B>where
B: Send + 'static,
Chains a computation that depends on the success value.
Sourcepub fn and_then<B>(self, f: impl FnOnce(A) -> IO<B> + Send + 'static) -> IO<B>where
B: Send + 'static,
pub fn and_then<B>(self, f: impl FnOnce(A) -> IO<B> + Send + 'static) -> IO<B>where
B: Send + 'static,
Alias for flat_map (for fdo! macro compatibility).
Sourcepub fn zip<B>(self, other: IO<B>) -> IO<(A, B)>where
B: Send + 'static,
pub fn zip<B>(self, other: IO<B>) -> IO<(A, B)>where
B: Send + 'static,
Combines two IOs into a tuple.
Sourcepub fn zip_with<B, C>(
self,
other: IO<B>,
f: impl FnOnce(A, B) -> C + Send + 'static,
) -> IO<C>
pub fn zip_with<B, C>( self, other: IO<B>, f: impl FnOnce(A, B) -> C + Send + 'static, ) -> IO<C>
Combines two IOs with a function.
Source§impl<A> IO<A>where
A: Send + 'static,
impl<A> IO<A>where
A: Send + 'static,
Sourcepub fn map_error(self, f: impl FnOnce(Error) -> Error + Send + 'static) -> IO<A>
pub fn map_error(self, f: impl FnOnce(Error) -> Error + Send + 'static) -> IO<A>
Transforms the error.
Sourcepub fn catch(self, f: impl FnOnce(Error) -> IO<A> + Send + 'static) -> IO<A>
pub fn catch(self, f: impl FnOnce(Error) -> IO<A> + Send + 'static) -> IO<A>
Recovers from an error by producing a new IO.
Source§impl IO<()>
impl IO<()>
Sourcepub fn bracket<R, B>(
acquire: IO<R>,
use_fn: impl FnOnce(&R) -> IO<B> + Send + 'static,
release: impl FnOnce(&R) + Send + 'static,
) -> IO<B>
pub fn bracket<R, B>( acquire: IO<R>, use_fn: impl FnOnce(&R) -> IO<B> + Send + 'static, release: impl FnOnce(&R) + Send + 'static, ) -> IO<B>
Acquires a resource, uses it, and guarantees release even on failure.
acquire: IO that produces the resourceuse_fn: Function that uses the resource (receives a reference)release: Cleanup function that always runs afteruse_fn(on both success and error)
If acquire fails, neither use_fn nor release run.
Source§impl<A> IO<A>where
A: Send + 'static,
impl<A> IO<A>where
A: Send + 'static,
Sourcepub fn from_future(
future: impl Future<Output = Result<A, Error>> + Send + 'static,
) -> IO<A>
pub fn from_future( future: impl Future<Output = Result<A, Error>> + Send + 'static, ) -> IO<A>
Creates an IO from a future. Blocks on the current tokio runtime.
Must be called from within a tokio runtime context. Uses block_in_place
to avoid panicking when called from an async context (requires multi-thread runtime).