Skip to main content

TryLazy

Struct TryLazy 

Source
pub struct TryLazy<'a, A, E, Config: LazyConfig = RcLazyConfig>(/* private fields */)
where
    A: 'a,
    E: 'a;
Expand description

A lazily-computed, memoized value that may fail.

The computation runs at most once. If it succeeds, the value is cached. If it fails, the error is cached. Subsequent accesses return the cached result.

§Type Parameters

  • A: The type of the computed value.
  • E: The type of the error.
  • Config: The memoization configuration.

§Fields

  • 0: The internal lazy cell.

Implementations§

Source§

impl<'a, A, E, Config: LazyConfig> TryLazy<'a, A, E, Config>
where A: 'a, E: 'a,

Source

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

Gets the memoized result, computing on first access.

§Type Signature

Result A E

§Returns

A result containing a reference to the value or error.

§Examples
use fp_library::types::*;

let memo = TryLazy::<_, _, RcLazyConfig>::new(|| Ok::<i32, ()>(42));
assert_eq!(memo.evaluate(), Ok(&42));
Source§

impl<'a, A, E> TryLazy<'a, A, E, RcLazyConfig>
where A: 'a, E: 'a,

Source

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

Creates a new TryLazy that will run f on first access.

§Type Signature

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

§Type Parameters
  • F: The type of the initializer closure.
§Parameters
  • f: The closure that produces the result.
§Returns

A new TryLazy instance.

§Examples
use fp_library::types::*;

let memo = TryLazy::<_, _, RcLazyConfig>::new(|| Ok::<i32, ()>(42));
assert_eq!(memo.evaluate(), Ok(&42));
Source§

impl<'a, A> TryLazy<'a, A, String, RcLazyConfig>
where A: 'a,

Source

pub fn catch_unwind<F>(f: F) -> Self
where F: FnOnce() -> A + UnwindSafe + 'a,

Creates a TryLazy that catches unwinds (panics).

§Type Signature

forall self. UnwindSafe () -> A => (() -> A) -> self

§Type Parameters
  • F: The type of the initializer closure.
§Parameters
  • f: The closure that might panic.
§Returns

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

§Examples
use fp_library::types::*;

let memo = TryLazy::<_, String, RcLazyConfig>::catch_unwind(|| {
    if true { panic!("oops") }
    42
});
assert_eq!(memo.evaluate(), Err(&"oops".to_string()));
Source§

impl<'a, A, E> TryLazy<'a, A, E, ArcLazyConfig>
where A: 'a, E: 'a,

Source

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

Creates a new TryLazy that will run f on first access.

§Type Signature

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

§Type Parameters
  • F: The type of the initializer closure.
§Parameters
  • f: The closure that produces the result.
§Returns

A new TryLazy instance.

§Examples
use fp_library::types::*;

let memo = TryLazy::<_, _, ArcLazyConfig>::new(|| Ok::<i32, ()>(42));
assert_eq!(memo.evaluate(), Ok(&42));

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, A, E> Deferrable<'a> for TryLazy<'a, A, E, RcLazyConfig>
where A: Clone + 'a, E: Clone + 'a,

Source§

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

Defers a computation that produces a TryLazy value.

This flattens the nested structure: instead of TryLazy<TryLazy<A, E>, E>, we get TryLazy<A, E>. The inner TryLazy is computed only when the outer TryLazy is evaluated.

§Type Signature

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

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

A new TryLazy value.

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

let lazy = TryLazy::<_, (), RcLazyConfig>::defer(|| RcTryLazy::new(|| Ok(42)));
assert_eq!(lazy.evaluate(), Ok(&42));
Source§

impl<'a, A, E> From<Lazy<'a, A>> for TryLazy<'a, A, E, RcLazyConfig>
where A: Clone + 'a, E: 'a,

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(memo: Lazy<'a, A, ArcLazyConfig>) -> 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, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
where A: Clone + Send + 'static, E: Clone + Send + 'static, Config: LazyConfig,

Source§

fn from(memo: TryLazy<'static, 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, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
where A: Send, E: Send,

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Defers a computation that produces a thread-safe TryLazy value.

This flattens the nested structure: instead of ArcTryLazy<ArcTryLazy<A, E>, E>, we get ArcTryLazy<A, E>. The inner TryLazy is computed only when the outer TryLazy is evaluated.

§Type Signature

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

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

A new ArcTryLazy value.

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

let lazy: ArcTryLazy<i32, ()> = ArcTryLazy::send_defer(|| ArcTryLazy::new(|| Ok(42)));
assert_eq!(lazy.evaluate(), Ok(&42));

Auto Trait Implementations§

§

impl<'a, A, E, Config> Freeze for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: Freeze,

§

impl<'a, A, E, Config> RefUnwindSafe for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: RefUnwindSafe,

§

impl<'a, A, E, Config> Send for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: Send,

§

impl<'a, A, E, Config> Sync for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: Sync,

§

impl<'a, A, E, Config> Unpin for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: Unpin,

§

impl<'a, A, E, Config> UnwindSafe for TryLazy<'a, A, E, Config>
where <Config as LazyConfig>::TryLazy<'a, A, E>: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.