Skip to main content

TryThunk

Struct TryThunk 

Source
pub struct TryThunk<'a, A, E>(/* private fields */);
Expand description

A deferred computation that may fail with error type E.

This is Thunk<'a, Result<A, E>> with ergonomic combinators for error handling. Like Thunk, this is NOT memoized. Each TryThunk::evaluate re-executes. Unlike Thunk, the result is Result<A, E>.

§Type Parameters

  • 'a: The lifetime of the computation.
  • A: The type of the value produced by the computation on success.
  • E: The type of the error produced by the computation on failure.

§Higher-Kinded Type Representation

This type has multiple higher-kinded representations:

§When to Use

Use TryThunk for lightweight fallible deferred computation with full HKT support. It is not stack-safe for deep bind chains. For stack-safe fallible recursion, use TryTrampoline. For memoized fallible computation, use TryLazy.

§Algebraic Properties

TryThunk forms a monad over the success type A (with E fixed):

  • TryThunk::pure(a).bind(f).evaluate() == f(a).evaluate() (left identity).
  • thunk.bind(TryThunk::pure).evaluate() == thunk.evaluate() (right identity).
  • thunk.bind(f).bind(g).evaluate() == thunk.bind(|a| f(a).bind(g)).evaluate() (associativity).

On the error channel, bind short-circuits: if the computation produces Err(e), the continuation f is never called.

§Stack Safety

TryThunk::bind chains are not stack-safe. Each nested bind adds a frame to the call stack, so sufficiently deep chains will cause a stack overflow. For stack-safe fallible recursion, use TryTrampoline.

§Limitations

Cannot implement Traversable: TryThunk wraps a FnOnce closure, which cannot be cloned because FnOnce is consumed when called. The Traversable trait requires Clone bounds on the result type, making it fundamentally incompatible with TryThunk’s design. This mirrors the same limitation on Thunk.

Implementations§

Source§

impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
  • E: The type of the error value.
Source

pub fn new(f: impl FnOnce() -> Result<A, E> + 'a) -> Self

Creates a new TryThunk from a thunk.

§Type Signature

forall A E. (() -> Result A E) -> TryThunk A E

§Parameters
  • f: The thunk to wrap.
§Returns

A new TryThunk instance.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::new(|| Ok(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn pure(a: A) -> Self

Returns a pure value (already computed).

§Type Signature

forall A E. A -> TryThunk A E

§Parameters
  • a: The value to wrap.
§Returns

A new TryThunk instance containing the value.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn defer(f: impl FnOnce() -> TryThunk<'a, A, E> + 'a) -> Self

Defers a computation that returns a TryThunk.

§Type Signature

forall A E. (() -> TryThunk A E) -> TryThunk A E

§Parameters
  • f: The thunk that returns a TryThunk.
§Returns

A new TryThunk instance.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::defer(|| TryThunk::ok(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn ok(a: A) -> Self

Alias for pure, provided for readability.

Both TryThunk::ok(x) and TryThunk::pure(x) produce the same result: a deferred computation that succeeds with x. The ok variant mirrors the Result::Ok constructor name, making intent clearer when working directly with TryThunk values rather than through HKT abstractions.

§Type Signature

forall A E. A -> TryThunk A E

§Parameters
  • a: The value to wrap.
§Returns

A new TryThunk instance containing the value.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn err(e: E) -> Self

Returns a pure error.

§Type Signature

forall A E. E -> TryThunk A E

§Parameters
  • e: The error to wrap.
§Returns

A new TryThunk instance containing the error.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, &str> = TryThunk::err("error");
assert_eq!(try_thunk.evaluate(), Err("error"));
Source

pub fn bind<B: 'a>( self, f: impl FnOnce(A) -> TryThunk<'a, B, E> + 'a, ) -> TryThunk<'a, B, E>

Monadic bind: chains computations.

§Type Signature

forall A E B. (TryThunk A E, A -> TryThunk B E) -> TryThunk B E

§Type Parameters
  • B: The type of the result of the new computation.
§Parameters
  • self: The TryThunk instance.
  • f: The function to apply to the result of the computation.
§Returns

A new TryThunk instance representing the chained computation.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).bind(|x| TryThunk::ok(x * 2));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn map<B: 'a>(self, func: impl FnOnce(A) -> B + 'a) -> TryThunk<'a, B, E>

Functor map: transforms the result.

§Type Signature

forall A E B. (TryThunk A E, A -> B) -> TryThunk B E

§Type Parameters
  • B: The type of the result of the transformation.
§Parameters
  • self: The TryThunk instance.
  • func: The function to apply to the result of the computation.
§Returns

A new TryThunk instance with the transformed result.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::ok(21).map(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn map_err<E2: 'a>( self, f: impl FnOnce(E) -> E2 + 'a, ) -> TryThunk<'a, A, E2>

Map error: transforms the error.

§Type Signature

forall A E E2. (TryThunk A E, E -> E2) -> TryThunk A E2

§Type Parameters
  • E2: The type of the new error.
§Parameters
  • self: The TryThunk instance.
  • f: The function to apply to the error.
§Returns

A new TryThunk instance with the transformed error.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Err(42));
Source

pub fn catch(self, f: impl FnOnce(E) -> TryThunk<'a, A, E> + 'a) -> Self

Recovers from an error.

§Type Signature

forall A E. (TryThunk A E, E -> TryThunk A E) -> TryThunk A E

§Parameters
  • self: The TryThunk instance.
  • f: The function to apply to the error value.
§Returns

A new TryThunk that attempts to recover from failure.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, &str> = TryThunk::err("error").catch(|_| TryThunk::ok(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn catch_with<E2: 'a>( self, f: impl FnOnce(E) -> TryThunk<'a, A, E2> + 'a, ) -> TryThunk<'a, A, E2>

Recovers from an error using a fallible recovery function that may produce a different error type.

Unlike catch, catch_with allows the recovery function to return a TryThunk with a different error type E2. On success, the value is passed through unchanged. On failure, the recovery function is applied to the error value and its result is evaluated.

§Type Signature

forall A E E2. (TryThunk A E, E -> TryThunk A E2) -> TryThunk A E2

§Type Parameters
  • E2: The error type produced by the recovery computation.
§Parameters
  • self: The TryThunk instance.
  • f: The monadic recovery function applied to the error value.
§Returns

A new TryThunk that either passes through the success value or uses the result of the recovery computation.

§Examples
use fp_library::types::*;

let recovered: TryThunk<i32, i32> =
	TryThunk::<i32, &str>::err("error").catch_with(|_| TryThunk::err(42));
assert_eq!(recovered.evaluate(), Err(42));

let ok: TryThunk<i32, i32> = TryThunk::<i32, &str>::ok(1).catch_with(|_| TryThunk::err(42));
assert_eq!(ok.evaluate(), Ok(1));
Source

pub fn bimap<B: 'a, E2: 'a>( self, f: impl FnOnce(A) -> B + 'a, g: impl FnOnce(E) -> E2 + 'a, ) -> TryThunk<'a, B, E2>

Maps both the success and error values simultaneously.

§Type Signature

forall A E B E2. (TryThunk A E, A -> B, E -> E2) -> TryThunk B E2

§Type Parameters
  • B: The type of the new success value.
  • E2: The type of the new error value.
§Parameters
  • self: The TryThunk instance.
  • f: The function to apply to the success value.
  • g: The function to apply to the error value.
§Returns

A new TryThunk with both sides transformed.

§Examples
use fp_library::types::*;

let ok: TryThunk<i32, i32> = TryThunk::pure(5);
assert_eq!(ok.bimap(|x| x * 2, |e| e + 1).evaluate(), Ok(10));

let err: TryThunk<i32, i32> = TryThunk::err(5);
assert_eq!(err.bimap(|x| x * 2, |e| e + 1).evaluate(), Err(6));
Source

pub fn evaluate(self) -> Result<A, E>

Forces evaluation and returns the result.

§Type Signature

forall A E. TryThunk A E -> Result A E

§Returns

The result of the computation.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::ok(42);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn lift2<B: 'a, C: 'a>( self, other: TryThunk<'a, B, E>, f: impl FnOnce(A, B) -> C + 'a, ) -> TryThunk<'a, C, E>

Combines two TryThunks, running both and combining their results.

Short-circuits on error: if self fails, other is never evaluated.

§Type Signature

forall A E B C. (TryThunk A E, TryThunk B E, (A, B) -> C) -> TryThunk C E

§Type Parameters
  • B: The type of the second computation’s success value.
  • C: The type of the combined result.
§Parameters
  • self: The TryThunk instance.
  • other: The second computation.
  • f: The function to combine the results.
§Returns

A new TryThunk producing the combined result.

§Examples
use fp_library::types::*;

let t1: TryThunk<i32, String> = TryThunk::ok(10);
let t2: TryThunk<i32, String> = TryThunk::ok(20);
let t3 = t1.lift2(t2, |a, b| a + b);
assert_eq!(t3.evaluate(), Ok(30));

let t4: TryThunk<i32, String> = TryThunk::err("fail".to_string());
let t5: TryThunk<i32, String> = TryThunk::ok(20);
let t6 = t4.lift2(t5, |a, b| a + b);
assert_eq!(t6.evaluate(), Err("fail".to_string()));
Source

pub fn then<B: 'a>(self, other: TryThunk<'a, B, E>) -> TryThunk<'a, B, E>

Sequences two TryThunks, discarding the first result.

Short-circuits on error: if self fails, other is never evaluated.

§Type Signature

forall A E B. (TryThunk A E, TryThunk B E) -> TryThunk B E

§Type Parameters
  • B: The type of the second computation’s success value.
§Parameters
  • self: The TryThunk instance.
  • other: The second computation.
§Returns

A new TryThunk that runs both computations and returns the result of the second.

§Examples
use fp_library::types::*;

let t1: TryThunk<i32, String> = TryThunk::ok(10);
let t2: TryThunk<i32, String> = TryThunk::ok(20);
let t3 = t1.then(t2);
assert_eq!(t3.evaluate(), Ok(20));

let t4: TryThunk<i32, String> = TryThunk::err("fail".to_string());
let t5: TryThunk<i32, String> = TryThunk::ok(20);
let t6 = t4.then(t5);
assert_eq!(t6.evaluate(), Err("fail".to_string()));
Source

pub fn tail_rec_m<S>( f: impl Fn(S) -> TryThunk<'a, ControlFlow<A, S>, E> + 'a, initial: S, ) -> Self
where S: 'a,

Performs tail-recursive monadic computation with error handling.

The step function f is called in a loop, avoiding stack growth. Each iteration evaluates f(state) and inspects the resulting Result and ControlFlow: Ok(ControlFlow::Continue(next)) continues with next, Ok(ControlFlow::Break(a)) breaks out and returns Ok(a), and Err(e) short-circuits with Err(e).

Unlike the MonadRec implementation for TryThunkErrAppliedBrand<E>, this method does not require E: 'static, so it works with borrowed error types like &'a str.

§Step Function

The function f is bounded by Fn, so it is callable multiple times by shared reference. Each iteration of the loop calls f without consuming it.

§Type Signature

forall A E S. (S -> TryThunk (ControlFlow A S) E, S) -> TryThunk A E

§Type Parameters
  • S: The type of the loop state.
§Parameters
  • f: The step function that produces the next state, the final result, or an error.
  • initial: The initial state.
§Returns

A TryThunk that, when evaluated, runs the tail-recursive loop.

§Examples
use {
	core::ops::ControlFlow,
	fp_library::types::*,
};

let result: TryThunk<i32, &str> = TryThunk::tail_rec_m(
	|x| {
		TryThunk::ok(
			if x < 1000 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) },
		)
	},
	0,
);
assert_eq!(result.evaluate(), Ok(1000));
Source

pub fn arc_tail_rec_m<S>( f: impl Fn(S) -> TryThunk<'a, ControlFlow<A, S>, E> + Send + Sync + 'a, initial: S, ) -> Self
where S: 'a,

Arc-wrapped version of tail_rec_m for non-Clone closures.

Use this when your closure captures non-Clone state. The closure is wrapped in Arc internally, which provides the required Clone implementation. The step function must be Send + Sync because Arc requires these bounds.

§Type Signature

forall A E S. (S -> TryThunk (ControlFlow A S) E, S) -> TryThunk A E

§Type Parameters
  • S: The type of the loop state.
§Parameters
  • f: The step function that produces the next state, the final result, or an error.
  • initial: The initial state.
§Returns

A TryThunk that, when evaluated, runs the tail-recursive loop.

§Examples
use {
	core::ops::ControlFlow,
	fp_library::types::*,
	std::sync::{
		Arc,
		atomic::{
			AtomicUsize,
			Ordering,
		},
	},
};

let counter = Arc::new(AtomicUsize::new(0));
let counter_clone = Arc::clone(&counter);
let result: TryThunk<i32, ()> = TryThunk::arc_tail_rec_m(
	move |x| {
		counter_clone.fetch_add(1, Ordering::SeqCst);
		TryThunk::ok(if x < 100 { ControlFlow::Continue(x + 1) } else { ControlFlow::Break(x) })
	},
	0,
);
assert_eq!(result.evaluate(), Ok(100));
assert_eq!(counter.load(Ordering::SeqCst), 101);
Source

pub fn into_rc_try_lazy(self) -> RcTryLazy<'a, A, E>

Converts this TryThunk into a memoized RcTryLazy.

The resulting RcTryLazy will evaluate the computation on first access and cache the result for subsequent accesses.

§Type Signature

forall A E. TryThunk A E -> RcTryLazy A E

§Returns

A memoized RcTryLazy wrapping this computation.

§Examples
use fp_library::types::*;

let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
let lazy: RcTryLazy<i32, ()> = thunk.into_rc_try_lazy();
assert_eq!(lazy.evaluate(), Ok(&42));
Source

pub fn into_arc_try_lazy(self) -> ArcTryLazy<'a, A, E>
where A: Send + Sync + 'a, E: Send + Sync + 'a,

Evaluates this TryThunk and wraps the result in a thread-safe ArcTryLazy.

The thunk is evaluated eagerly because its inner closure is not Send. The result is stored in an ArcTryLazy for thread-safe sharing.

§Type Signature

forall A E. TryThunk A E -> ArcTryLazy A E

§Returns

A thread-safe memoized ArcTryLazy wrapping this computation.

§Examples
use fp_library::types::*;

let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
let lazy: ArcTryLazy<i32, ()> = thunk.into_arc_try_lazy();
assert_eq!(lazy.evaluate(), Ok(&42));
Source§

impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
  • E: The type of the error value.
Source

pub fn catch_unwind_with( f: impl FnOnce() -> A + UnwindSafe + 'a, handler: impl FnOnce(Box<dyn Any + Send>) -> E + 'a, ) -> Self

Creates a TryThunk that catches unwinds (panics), converting the panic payload using a custom conversion function.

The closure f is executed when the thunk is evaluated. If f panics, the panic payload is passed to handler to produce the error value. If f returns normally, the value is wrapped in Ok.

§Type Signature

forall A E. ((() -> A, UnwindSafe), Any -> E) -> TryThunk A E

§Parameters
  • f: The closure that might panic.
  • handler: The function that converts a panic payload into the error type.
§Returns

A new TryThunk instance where panics are converted to Err(E) via the handler.

§Examples
use fp_library::types::*;

let thunk = TryThunk::<i32, i32>::catch_unwind_with(
	|| {
		if true {
			panic!("oops")
		}
		42
	},
	|_payload| -1,
);
assert_eq!(thunk.evaluate(), Err(-1));
Source

pub fn into_inner(self) -> Thunk<'a, Result<A, E>>

Unwraps the newtype, returning the inner Thunk<'a, Result<A, E>>.

§Type Signature

forall A E. TryThunk A E -> Thunk (Result A E)

§Returns

The underlying Thunk that produces a Result.

§Examples
use fp_library::types::*;

let try_thunk: TryThunk<i32, ()> = TryThunk::pure(42);
let inner = try_thunk.into_inner();
assert_eq!(inner.evaluate(), Ok(42));
Source§

impl<'a, A: 'a> TryThunk<'a, A, String>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
Source

pub fn catch_unwind(f: impl FnOnce() -> A + UnwindSafe + 'a) -> Self

Creates a TryThunk that catches unwinds (panics).

The closure is executed when the thunk is evaluated. If the closure panics, the panic payload is converted to a String error. If the closure returns normally, the value is wrapped in Ok.

This is a convenience wrapper around catch_unwind_with that uses the default panic payload to string conversion.

§Type Signature

forall A. (() -> A, UnwindSafe) -> TryThunk A

§Parameters
  • f: The closure that might panic.
§Returns

A new TryThunk instance where panics are converted to Err(String).

§Examples
use fp_library::types::*;

let thunk = TryThunk::<i32, String>::catch_unwind(|| {
	if true {
		panic!("oops")
	}
	42
});
assert_eq!(thunk.evaluate(), Err("oops".to_string()));

Trait Implementations§

Source§

impl<'a, A, E> Debug for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

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

Formats the try-thunk without evaluating it.

§Type Signature

forall A E. (&TryThunk A E, &mut fmt) -> fmt

§Parameters
  • &self: The try-thunk to format.
  • f: The formatter.
§Returns

The formatting result.

§Examples
use fp_library::types::*;
let thunk = TryThunk::new(|| Ok::<i32, ()>(42));
assert_eq!(format!("{:?}", thunk), "TryThunk(<unevaluated>)");
Source§

impl<'a, A, E> Deferrable<'a> for TryThunk<'a, A, E>
where A: 'a, E: 'a,

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn defer(f: impl FnOnce() -> Self + 'a) -> Self
where Self: Sized,

Creates a TryThunk from a computation that produces it.

§Type Signature

forall A E. (() -> TryThunk A E) -> TryThunk A E

§Parameters
  • f: A thunk that produces the try thunk.
§Returns

The deferred try thunk.

§Examples
use fp_library::{
	brands::*,
	classes::Deferrable,
	functions::*,
	types::*,
};

let task: TryThunk<i32, ()> = Deferrable::defer(|| TryThunk::ok(42));
assert_eq!(task.evaluate(), Ok(42));
Source§

impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>
where A: Clone + 'a, E: 'a, Config: LazyConfig,

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
  • Config: The memoization configuration.
Source§

fn from(memo: Lazy<'a, A, Config>) -> Self

§Type Signature

forall A E Config. LazyConfig Config => Lazy A Config -> TryThunk A E Config

§Parameters
  • memo: The lazy value to convert.
§Returns

A new TryThunk instance that wraps the lazy value.

§Examples
use fp_library::types::*;
let lazy = Lazy::<_, RcLazyConfig>::pure(42);
let thunk: TryThunk<i32, ()> = TryThunk::from(lazy);
assert_eq!(thunk.evaluate(), Ok(42));
Source§

impl<'a, A: 'a, E: 'a> From<Result<A, E>> for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(result: Result<A, E>) -> Self

§Type Signature

forall A E. Result A E -> TryThunk A E

§Parameters
  • result: The result to convert.
§Returns

A new TryThunk instance that produces the result.

§Examples
use fp_library::types::*;
let ok_thunk: TryThunk<i32, String> = TryThunk::from(Ok(42));
assert_eq!(ok_thunk.evaluate(), Ok(42));

let err_thunk: TryThunk<i32, String> = TryThunk::from(Err("error".to_string()));
assert_eq!(err_thunk.evaluate(), Err("error".to_string()));
Source§

impl<'a, A: 'a, E: 'a> From<Thunk<'a, A>> for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(eval: Thunk<'a, A>) -> Self

§Type Signature

forall A E. Thunk A -> TryThunk A E

§Parameters
  • eval: The thunk to convert.
§Returns

A new TryThunk instance that wraps the thunk.

§Examples
use fp_library::types::*;
let thunk = Thunk::new(|| 42);
let try_thunk: TryThunk<i32, ()> = TryThunk::from(thunk);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source§

impl<'a, A, E, Config> From<TryLazy<'a, A, E, Config>> for TryThunk<'a, A, E>
where A: Clone + 'a, E: Clone + 'a, Config: LazyConfig,

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
  • Config: The memoization configuration.
Source§

fn from(memo: TryLazy<'a, A, E, Config>) -> Self

Converts a TryLazy value into a TryThunk by cloning the memoized result.

This conversion clones both the success and error values on each evaluation. The cost depends on the Clone implementations of A and E.

§Type Signature

forall A E Config. LazyConfig Config => TryLazy A E Config -> TryThunk A E Config

§Parameters
  • memo: The fallible lazy value to convert.
§Returns

A new TryThunk instance that wraps the fallible lazy value.

§Examples
use fp_library::types::*;
let lazy = TryLazy::<_, _, RcLazyConfig>::new(|| Ok::<i32, ()>(42));
let thunk = TryThunk::from(lazy);
assert_eq!(thunk.evaluate(), Ok(42));
Source§

impl<'a, A: 'a, E: 'a> From<TrySendThunk<'a, A, E>> for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(send_thunk: TrySendThunk<'a, A, E>) -> Self

Converts a TrySendThunk into a TryThunk by erasing the Send bound.

This delegates to the SendThunk to Thunk conversion, which is a zero-cost unsizing coercion: the inner Box<dyn FnOnce() -> Result<A, E> + Send + 'a> is coerced to Box<dyn FnOnce() -> Result<A, E> + 'a>.

§Type Signature

forall A E. TrySendThunk A E -> TryThunk A E

§Parameters
  • send_thunk: The send try-thunk to convert.
§Returns

A TryThunk wrapping the same deferred computation.

§Examples
use fp_library::types::*;
let send_thunk: TrySendThunk<i32, ()> = TrySendThunk::pure(42);
let thunk: TryThunk<i32, ()> = TryThunk::from(send_thunk);
assert_eq!(thunk.evaluate(), Ok(42));
Source§

impl<'a, A, E> From<TryThunk<'a, A, E>> for TryLazy<'a, A, E, RcLazyConfig>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
  • E: The type of the error.
Source§

fn from(eval: TryThunk<'a, A, E>) -> Self

§Type Signature

forall A E. TryThunk A E -> TryLazy A E

§Parameters
  • eval: The fallible thunk to convert.
§Returns

A new TryLazy instance that will evaluate the thunk on first access.

§Examples
use fp_library::types::*;
let thunk = TryThunk::new(|| Ok::<i32, ()>(42));
let memo: RcTryLazy<i32, ()> = TryLazy::from(thunk);
assert_eq!(memo.evaluate(), Ok(&42));
Source§

impl<'a, A, E> From<TryThunk<'a, A, E>> for TryLazy<'a, A, E, ArcLazyConfig>
where A: Send + Sync + 'a, E: Send + Sync + 'a,

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
  • E: The type of the error.
Source§

fn from(eval: TryThunk<'a, A, E>) -> Self

Converts a TryThunk into an ArcTryLazy by eagerly evaluating the thunk.

TryThunk is !Send, so the result must be computed immediately to cross into the thread-safe ArcTryLazy world.

§Type Signature

forall A E. TryThunk A E -> TryLazy A E

§Parameters
  • eval: The fallible thunk to convert.
§Returns

A new TryLazy instance containing the eagerly evaluated result.

§Examples
use fp_library::types::*;
let thunk = TryThunk::new(|| Ok::<i32, ()>(42));
let memo: ArcTryLazy<i32, ()> = ArcTryLazy::from(thunk);
assert_eq!(memo.evaluate(), Ok(&42));
Source§

impl<'a, A, E> From<TryThunk<'a, A, E>> for TrySendThunk<'a, A, E>
where A: Send + 'a, E: Send + 'a,

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(thunk: TryThunk<'a, A, E>) -> Self

Converts a TryThunk into a TrySendThunk.

The TryThunk closure is not Send, so the conversion eagerly evaluates it and wraps the owned result in a new TrySendThunk.

§Type Signature

forall A E. TryThunk A E -> TrySendThunk A E

§Parameters
  • thunk: The try-thunk to convert.
§Returns

A new TrySendThunk wrapping the evaluated result.

§Examples
use fp_library::types::*;
let thunk: TryThunk<i32, ()> = TryThunk::ok(42);
let send_thunk = TrySendThunk::from(thunk);
assert_eq!(send_thunk.evaluate(), Ok(42));
Source§

impl<A, E> From<TryThunk<'static, A, E>> for TryTrampoline<A, E>
where A: 'static, E: 'static,

§Type Parameters
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(thunk: TryThunk<'static, A, E>) -> Self

Converts a 'static TryThunk into a TryTrampoline.

This lifts a non-stack-safe TryThunk into the stack-safe TryTrampoline execution model. The resulting TryTrampoline evaluates the thunk when run.

§Type Signature

forall A E. crate A E -> TryTrampoline A E

§Parameters
  • thunk: The fallible thunk to convert.
§Returns

A new TryTrampoline instance that evaluates the thunk.

§Examples
use fp_library::types::*;
let thunk = TryThunk::new(|| Ok::<i32, String>(42));
let task = TryTrampoline::from(thunk);
assert_eq!(task.evaluate(), Ok(42));
Source§

impl<A: 'static, E: 'static> From<TryTrampoline<A, E>> for TryThunk<'static, A, E>

§Type Parameters
  • A: The type of the success value.
  • E: The type of the error value.
Source§

fn from(tramp: TryTrampoline<A, E>) -> Self

Converts a TryTrampoline into a TryThunk.

The resulting TryThunk will evaluate the trampoline when forced.

§Type Signature

forall A E. TryTrampoline A E -> TryThunk A E

§Parameters
  • tramp: The fallible trampoline to convert.
§Returns

A new TryThunk instance that evaluates the trampoline.

§Examples
use fp_library::types::*;

let tramp: TryTrampoline<i32, String> = TryTrampoline::ok(42);
let thunk: TryThunk<i32, String> = TryThunk::from(tramp);
assert_eq!(thunk.evaluate(), Ok(42));
Source§

impl<'a, E: 'a, A: 'a> InferableBrand_266801a817966495<'a, TryThunkBrand, E, A> for TryThunk<'a, A, E>

Generated InferableBrand_266801a817966495 implementation for TryThunk < 'a, A, E > with brand TryThunkBrand.

Source§

type Marker = Val

Dispatch marker: Val for owned types, Ref for references.
Source§

impl<'a, A: 'a, E: 'static> InferableBrand_cdc7cd43dac7585f<'a, TryThunkErrAppliedBrand<E>, A> for TryThunk<'a, A, E>

Generated InferableBrand_cdc7cd43dac7585f implementation for TryThunk < 'a, A, E > with brand TryThunkErrAppliedBrand < E >.

Source§

type Marker = Val

Dispatch marker: Val for owned types, Ref for references.
Source§

impl<'a, E: 'a, A: 'static> InferableBrand_cdc7cd43dac7585f<'a, TryThunkOkAppliedBrand<A>, E> for TryThunk<'a, A, E>

Generated InferableBrand_cdc7cd43dac7585f implementation for TryThunk < 'a, A, E > with brand TryThunkOkAppliedBrand < A >.

Source§

type Marker = Val

Dispatch marker: Val for owned types, Ref for references.
Source§

impl<'a, A: Monoid + 'a, E: 'a> Monoid for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The success value type.
  • E: The error value type.
Source§

fn empty() -> Self

Returns the identity TryThunk.

§Type Signature

forall A E. Monoid A => () -> TryThunk A E

§Returns

A TryThunk producing the identity value of A.

§Examples
use fp_library::{
	classes::*,
	types::*,
};

let t: TryThunk<String, ()> = TryThunk::empty();
assert_eq!(t.evaluate(), Ok("".to_string()));
Source§

impl<'a, A: Semigroup + 'a, E: 'a> Semigroup for TryThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The success value type.
  • E: The error value type.
Source§

fn append(a: Self, b: Self) -> Self

Combines two TryThunks by combining their results.

§Type Signature

forall A E. Semigroup A => (TryThunk A E, TryThunk A E) -> TryThunk A E

§Parameters
  • a: The first TryThunk.
  • b: The second TryThunk.
§Returns

A new TryThunk containing the combined result.

§Examples
use fp_library::{
	brands::*,
	classes::*,
	functions::*,
	types::*,
};

let t1: TryThunk<String, ()> = pure::<TryThunkErrAppliedBrand<()>, _>("Hello".to_string());
let t2: TryThunk<String, ()> = pure::<TryThunkErrAppliedBrand<()>, _>(" World".to_string());
let t3 = append::<_>(t1, t2);
assert_eq!(t3.evaluate(), Ok("Hello World".to_string()));

Auto Trait Implementations§

§

impl<'a, A, E> Freeze for TryThunk<'a, A, E>

§

impl<'a, A, E> !RefUnwindSafe for TryThunk<'a, A, E>

§

impl<'a, A, E> !Send for TryThunk<'a, A, E>

§

impl<'a, A, E> !Sync for TryThunk<'a, A, E>

§

impl<'a, A, E> Unpin for TryThunk<'a, A, E>

§

impl<'a, A, E> UnsafeUnpin for TryThunk<'a, A, E>

§

impl<'a, A, E> !UnwindSafe for TryThunk<'a, A, E>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T

Source§

fn pipe<B>(self, f: impl FnOnce(Self) -> B) -> B

Pipes self into a function, enabling left-to-right composition. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.