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.

Like Thunk, this is NOT memoized. Each TryThunk::evaluate 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.evaluate() {
    Ok(val) => assert_eq!(val, 42),
    Err(_) => panic!("Should not fail"),
}

Implementations§

Source§

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

Source

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

Creates a new TryThunk from a thunk.

§Type Signature

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

§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_thunk: TryThunk<i32, ()> = TryThunk::new(|| Ok(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn pure(a: A) -> Self
where A: 'a,

Returns a pure value (already computed).

§Type Signature

forall self. A -> self

§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>(f: F) -> Self
where F: FnOnce() -> TryThunk<'a, A, E> + 'a,

Defers a computation that returns a TryThunk.

§Type Signature

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

§Type Parameters
  • F: The type of the thunk.
§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::pure(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

pub fn ok(a: A) -> Self
where A: 'a,

Alias for pure.

Creates a successful computation.

§Type Signature

forall self. A -> self

§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
where E: 'a,

Returns a pure error.

§Type Signature

forall self. E -> self

§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, F>(self, f: F) -> TryThunk<'a, B, E>
where F: FnOnce(A) -> TryThunk<'a, B, E> + 'a,

Monadic bind: chains computations.

§Type Signature

forall b. (A -> TryThunk b 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_thunk: TryThunk<i32, ()> = TryThunk::pure(21).bind(|x| TryThunk::pure(x * 2));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

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

Functor map: transforms the result.

§Type Signature

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

§Type Parameters
  • B: The type of the result of the transformation.
  • F: The type of the transformation function.
§Parameters
  • 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::pure(21).map(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

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 -> e2) -> 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_thunk: TryThunk<i32, i32> = TryThunk::err(21).map_err(|x| x * 2);
assert_eq!(try_thunk.evaluate(), Err(42));
Source

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

Recovers from an error.

§Type Signature

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

§Type Parameters
  • F: The type of the recovery function.
§Parameters
  • 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::pure(42));
assert_eq!(try_thunk.evaluate(), Ok(42));
Source

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

Forces evaluation and returns the result.

§Type Signature

Result A E

§Returns

The result of the computation.

§Examples
use fp_library::types::*;

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

Trait Implementations§

Source§

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

Source§

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

Creates a TryThunk from a computation that produces it.

§Type Signature

forall self. Deferrable self => (() -> self) -> self

§Type Parameters
  • F: The type of the thunk.
§Parameters
  • f: A thunk that produces the try thunk.
§Returns

The deferred try thunk.

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

let task: TryThunk<i32, ()> = Deferrable::defer(|| TryThunk::pure(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,

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
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,

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn empty() -> Self

Returns the identity TryThunk.

§Type Signature

forall self. Monoid self => self

§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>

Source§

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

Combines two TryThunks by combining their results.

§Type Signature

forall self. Semigroup self => (self, self) -> self

§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::<TryThunkWithErrBrand<()>, _>("Hello".to_string());
let t2: TryThunk<String, ()> = pure::<TryThunkWithErrBrand<()>, _>(" 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> !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> 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.