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

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 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));
Source

pub fn pure(a: A) -> Self
where 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));
Source

pub fn ok(a: A) -> Self
where 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));
Source

pub fn err(e: E) -> Self
where E: 'a,

Returns a pure error.

§Type Signature

forall e a. 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_eval: TryThunk<i32, &str> = TryThunk::err("error");
assert_eq!(try_eval.run(), 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 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));
Source

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

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));
Source

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));
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 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));
Source

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

Forces evaluation and returns the result.

§Type Signature

forall e a. TryThunk a e -> Result a e

§Returns

The result of the computation.

§Examples
use fp_library::types::*;

let try_eval: TryThunk<i32, ()> = TryThunk::pure(42);
assert_eq!(try_eval.run(), Ok(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,

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.

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.