pub struct TryThunk<'a, A, E>(/* private fields */);Expand description
A deferred computation that may fail with error type E.
Like Thunk, this is NOT memoized. Each TryThunk::run re-executes.
Unlike Thunk, the result is Result<A, E>.
§Type Parameters
A: The type of the value produced by the computation on success.E: The type of the error produced by the computation on failure.
§Fields
0: The closure that performs the computation.
§Examples
use fp_library::types::*;
let computation: TryThunk<i32, &str> = TryThunk::new(|| {
Ok(42)
});
match computation.run() {
Ok(val) => assert_eq!(val, 42),
Err(_) => panic!("Should not fail"),
}Implementations§
Source§impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>
impl<'a, A: 'a, E: 'a> TryThunk<'a, A, E>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new TryThunk from a thunk.
§Type Signature
forall e a. (Unit -> Result a e) -> TryThunk a e
§Type Parameters
F: The type of the thunk.
§Parameters
f: The thunk to wrap.
§Returns
A new TryThunk instance.
§Examples
use fp_library::types::*;
let try_eval: TryThunk<i32, ()> = TryThunk::new(|| Ok(42));
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn pure(a: A) -> Selfwhere
A: 'a,
pub fn pure(a: A) -> Selfwhere
A: 'a,
Returns a pure value (already computed).
§Type Signature
forall e a. 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_eval: TryThunk<i32, ()> = TryThunk::pure(42);
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn ok(a: A) -> Selfwhere
A: 'a,
pub fn ok(a: A) -> Selfwhere
A: 'a,
Alias for pure.
Creates a successful computation.
§Type Signature
forall e a. 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_eval: TryThunk<i32, ()> = TryThunk::ok(42);
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn bind<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
pub fn bind<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
Monadic bind: chains computations.
§Type Signature
forall e b a. (a -> TryThunk b e, TryThunk a e) -> TryThunk b e
§Type Parameters
B: The type of the result of the new computation.F: The type of the function to apply.
§Parameters
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_eval: TryThunk<i32, ()> = TryThunk::pure(21).bind(|x| TryThunk::pure(x * 2));
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn and_then<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
pub fn and_then<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>
Alias for bind.
Chains computations.
§Type Signature
forall e b a. (a -> TryThunk b e, TryThunk a e) -> TryThunk b e
§Type Parameters
B: The type of the result of the new computation.F: The type of the function to apply.
§Parameters
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_eval: TryThunk<i32, ()> = TryThunk::ok(21).and_then(|x| TryThunk::ok(x * 2));
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn map<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>where
F: FnOnce(A) -> B + 'a,
pub fn map<B: 'a, F>(self, f: F) -> TryThunk<'a, B, E>where
F: FnOnce(A) -> B + 'a,
Functor map: transforms the result.
§Type Signature
forall e b a. (a -> b, TryThunk a e) -> TryThunk b e
§Type Parameters
B: The type of the result of the transformation.F: The type of the transformation function.
§Parameters
f: 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_eval: TryThunk<i32, ()> = TryThunk::pure(21).map(|x| x * 2);
assert_eq!(try_eval.run(), Ok(42));Sourcepub fn map_err<E2: 'a, F>(self, f: F) -> TryThunk<'a, A, E2>where
F: FnOnce(E) -> E2 + 'a,
pub fn map_err<E2: 'a, F>(self, f: F) -> TryThunk<'a, A, E2>where
F: FnOnce(E) -> E2 + 'a,
Map error: transforms the error.
§Type Signature
forall e2 e a. (e -> e2, TryThunk a e) -> TryThunk a e2
§Type Parameters
E2: The type of the new error.F: The type of the transformation function.
§Parameters
f: The function to apply to the error.
§Returns
A new TryThunk instance with the transformed error.
§Examples
use fp_library::types::*;
let try_eval: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
assert_eq!(try_eval.run(), Err(42));Trait Implementations§
Source§impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>where
A: Clone + 'a,
E: 'a,
Config: LazyConfig,
impl<'a, A, E, Config> From<Lazy<'a, A, Config>> for TryThunk<'a, A, E>where
A: Clone + 'a,
E: 'a,
Config: LazyConfig,
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> !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