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:
TryThunkBrand: fully polymorphic over both error and success types (bifunctor).TryThunkErrAppliedBrand<E>: the error type is fixed, polymorphic over the success type (functor overOk).TryThunkOkAppliedBrand<A>: the success type is fixed, polymorphic over the error type (functor overErr).
§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.
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.
Sourcepub fn new(f: impl FnOnce() -> Result<A, E> + 'a) -> Self
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));Sourcepub fn pure(a: A) -> Self
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));Sourcepub fn defer(f: impl FnOnce() -> TryThunk<'a, A, E> + 'a) -> Self
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 aTryThunk.
§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));Sourcepub fn ok(a: A) -> Self
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));Sourcepub fn err(e: E) -> Self
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"));Sourcepub fn bind<B: 'a>(
self,
f: impl FnOnce(A) -> TryThunk<'a, B, E> + 'a,
) -> TryThunk<'a, B, E>
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: TheTryThunkinstance.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));Sourcepub fn map<B: 'a>(self, func: impl FnOnce(A) -> B + 'a) -> TryThunk<'a, B, E>
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: TheTryThunkinstance.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));Sourcepub fn map_err<E2: 'a>(
self,
f: impl FnOnce(E) -> E2 + 'a,
) -> TryThunk<'a, A, E2>
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: TheTryThunkinstance.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));Sourcepub fn catch(self, f: impl FnOnce(E) -> TryThunk<'a, A, E> + 'a) -> Self
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: TheTryThunkinstance.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));Sourcepub fn catch_with<E2: 'a>(
self,
f: impl FnOnce(E) -> TryThunk<'a, A, E2> + 'a,
) -> TryThunk<'a, A, E2>
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: TheTryThunkinstance.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));Sourcepub fn bimap<B: 'a, E2: 'a>(
self,
f: impl FnOnce(A) -> B + 'a,
g: impl FnOnce(E) -> E2 + 'a,
) -> TryThunk<'a, B, E2>
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: TheTryThunkinstance.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));Sourcepub fn lift2<B: 'a, C: 'a>(
self,
other: TryThunk<'a, B, E>,
f: impl FnOnce(A, B) -> C + 'a,
) -> TryThunk<'a, C, E>
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: TheTryThunkinstance.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()));Sourcepub fn then<B: 'a>(self, other: TryThunk<'a, B, E>) -> TryThunk<'a, B, E>
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: TheTryThunkinstance.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()));Sourcepub fn into_rc_try_lazy(self) -> RcTryLazy<'a, A, E>
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));Sourcepub fn into_arc_try_lazy(self) -> ArcTryLazy<'a, A, E>
pub fn into_arc_try_lazy(self) -> ArcTryLazy<'a, A, E>
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.
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.
Sourcepub fn catch_unwind_with(
f: impl FnOnce() -> A + UnwindSafe + 'a,
handler: impl FnOnce(Box<dyn Any + Send>) -> E + 'a,
) -> Self
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, dyn 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));Sourcepub fn into_inner(self) -> Thunk<'a, Result<A, E>>
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.
impl<'a, A: 'a> TryThunk<'a, A, String>
§Type Parameters
'a: The lifetime of the computation.A: The type of the computed value.
Sourcepub fn catch_unwind(f: impl FnOnce() -> A + UnwindSafe + 'a) -> Self
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) -> 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.
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
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.
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) -> Selfwhere
Self: Sized,
fn defer(f: impl FnOnce() -> Self + 'a) -> Selfwhere
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.
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
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.
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
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.
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
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>
§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.
impl<'a, A, E, Config> From<TryLazy<'a, A, E, Config>> 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.Config: The memoization configuration.
Source§fn from(memo: TryLazy<'a, A, E, Config>) -> Self
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. TryLazyConfig 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.
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
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.
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
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>
§Type Parameters
'a: The lifetime of the computation.
A: The type of the computed value.
E: The type of the error.
impl<'a, A, E> From<TryThunk<'a, A, E>> for TryLazy<'a, A, E, ArcLazyConfig>
§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
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>
§Type Parameters
'a: The lifetime of the computation.
A: The type of the success value.
E: The type of the error value.
impl<'a, A, E> From<TryThunk<'a, A, E>> for TrySendThunk<'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(thunk: TryThunk<'a, A, E>) -> Self
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.
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
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.
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
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, 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.
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§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.
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
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 firstTryThunk.b: The secondTryThunk.
§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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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