Skip to main content

IO

Struct IO 

Source
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 via anyhow::Error and surfaced at .run() boundaries. For typed errors, use IO<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,

Source

pub fn succeed(a: A) -> IO<A>

Creates an IO that succeeds with the given value.

Source

pub fn fail(e: impl Into<Error> + Send + 'static) -> IO<A>

Creates an IO that fails with the given error.

Source

pub fn effect(f: impl FnOnce() -> A + Send + 'static) -> IO<A>

Creates an IO from an infallible side effect.

Source

pub fn effect_result( f: impl FnOnce() -> Result<A, Error> + Send + 'static, ) -> IO<A>

Creates an IO from a fallible side effect (primary constructor).

Source

pub fn from_result( result: Result<A, impl Into<Error> + Send + 'static>, ) -> IO<A>

Lifts a Result into an IO.

Source

pub fn from_option( option: Option<A>, on_none: impl FnOnce() -> Error + Send + 'static, ) -> IO<A>

Lifts an Option into an IO, using the provided closure for the None case.

Source

pub fn from_either( either: Either<impl Into<Error> + Send + 'static, A>, ) -> IO<A>

Lifts an Either into an IO. Left becomes the error, Right becomes the value.

Source§

impl<A> IO<A>

Source

pub fn run(self) -> Result<A, Error>

Consumes and executes this IO, returning the result.

Source

pub fn run_or_panic(self) -> A
where A: Debug,

Executes this IO, panicking on error.

Source§

impl<A> IO<A>
where A: Send + 'static,

Source

pub fn map<B>(self, f: impl FnOnce(A) -> B + Send + 'static) -> IO<B>
where B: Send + 'static,

Transforms the success value.

Source

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.

Source

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).

Source

pub fn zip<B>(self, other: IO<B>) -> IO<(A, B)>
where B: Send + 'static,

Combines two IOs into a tuple.

Source

pub fn zip_with<B, C>( self, other: IO<B>, f: impl FnOnce(A, B) -> C + Send + 'static, ) -> IO<C>
where B: Send + 'static, C: Send + 'static,

Combines two IOs with a function.

Source

pub fn then<B>(self, other: IO<B>) -> IO<B>
where B: Send + 'static,

Sequences two IOs, discarding the first result.

Source

pub fn tap(self, f: impl FnOnce(&A) + Send + 'static) -> IO<A>

Runs a side effect on the success value, returning the original value.

Source§

impl<A> IO<A>
where A: Send + 'static,

Source

pub fn map_error(self, f: impl FnOnce(Error) -> Error + Send + 'static) -> IO<A>

Transforms the error.

Source

pub fn catch(self, f: impl FnOnce(Error) -> IO<A> + Send + 'static) -> IO<A>

Recovers from an error by producing a new IO.

Source

pub fn or_else(self, other: IO<A>) -> IO<A>

Falls back to another IO on error.

Source

pub fn either(self) -> IO<Either<Error, A>>

Makes this IO infallible by wrapping the result in Either. Errors become Left, successes become Right.

Source

pub fn retry(factory: impl Fn() -> IO<A> + Send + 'static, n: usize) -> IO<A>

Retries a factory function up to n times on failure.

This is an associated function (not a method) because IO is FnOnce — the factory creates a fresh IO on each attempt.

Source§

impl IO<()>

Source

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>
where R: Send + 'static, B: Send + 'static,

Acquires a resource, uses it, and guarantees release even on failure.

  • acquire: IO that produces the resource
  • use_fn: Function that uses the resource (receives a reference)
  • release: Cleanup function that always runs after use_fn (on both success and error)

If acquire fails, neither use_fn nor release run.

Source§

impl<A> IO<A>
where A: Send + 'static,

Source

pub fn ensuring(self, finalizer: impl FnOnce() + Send + 'static) -> IO<A>

Guarantees that the finalizer runs after this IO, regardless of success or failure.

Source§

impl<A> IO<A>
where A: Send + 'static,

Source

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).

Source

pub async fn to_future(self) -> Result<A, Error>

Converts this IO into a future.

Trait Implementations§

Source§

impl<A> Debug for IO<A>
where A: Debug,

Source§

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

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

impl<A> Pure<A> for IO<A>
where A: Send + 'static,

Source§

fn pure(a: A) -> IO<A>

Auto Trait Implementations§

§

impl<A> Freeze for IO<A>

§

impl<A> !RefUnwindSafe for IO<A>

§

impl<A> Send for IO<A>

§

impl<A> !Sync for IO<A>

§

impl<A> Unpin for IO<A>

§

impl<A> UnsafeUnpin for IO<A>

§

impl<A> !UnwindSafe for IO<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.