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,
impl<'a, A, E, Config: LazyConfig> TryLazy<'a, A, E, Config>where
A: 'a,
E: 'a,
Source§impl<'a, A, E> TryLazy<'a, A, E, RcLazyConfig>where
A: 'a,
E: 'a,
impl<'a, A, E> TryLazy<'a, A, E, RcLazyConfig>where
A: 'a,
E: 'a,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
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,
impl<'a, A> TryLazy<'a, A, String, RcLazyConfig>where
A: 'a,
Sourcepub fn catch_unwind<F>(f: F) -> Selfwhere
F: FnOnce() -> A + UnwindSafe + 'a,
pub fn catch_unwind<F>(f: F) -> Selfwhere
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,
impl<'a, A, E> TryLazy<'a, A, E, ArcLazyConfig>where
A: 'a,
E: 'a,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
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,
impl<'a, A, E, Config: LazyConfig> Clone for TryLazy<'a, A, E, Config>where
A: 'a,
E: 'a,
Source§impl<'a, A, E> Deferrable<'a> for TryLazy<'a, A, E, RcLazyConfig>
impl<'a, A, E> Deferrable<'a> for TryLazy<'a, A, E, RcLazyConfig>
Source§fn defer<F>(f: F) -> Self
fn defer<F>(f: F) -> Self
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,
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
fn from(memo: Lazy<'a, A, RcLazyConfig>) -> Self
Source§impl<'a, A, E> From<Lazy<'a, A, ArcLazyConfig>> for TryLazy<'a, A, E, ArcLazyConfig>
impl<'a, A, E> From<Lazy<'a, A, ArcLazyConfig>> for TryLazy<'a, A, E, ArcLazyConfig>
Source§fn from(memo: Lazy<'a, A, ArcLazyConfig>) -> Self
fn from(memo: Lazy<'a, A, ArcLazyConfig>) -> Self
Source§impl<A, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
impl<A, E, Config> From<TryLazy<'static, A, E, Config>> for TryTrampoline<A, E>
Source§impl<'a, A, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
impl<'a, A, E> From<TryTrampoline<A, E>> for TryLazy<'a, A, E, RcLazyConfig>
Source§fn from(task: TryTrampoline<A, E>) -> Self
fn from(task: TryTrampoline<A, E>) -> Self
Source§impl<'a, A, E> SendDeferrable<'a> for TryLazy<'a, A, E, ArcLazyConfig>
impl<'a, A, E> SendDeferrable<'a> for TryLazy<'a, A, E, ArcLazyConfig>
Source§fn send_defer<F>(f: F) -> Self
fn send_defer<F>(f: F) -> Self
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>
impl<'a, A, E, Config> RefUnwindSafe for TryLazy<'a, A, E, Config>
impl<'a, A, E, Config> Send for TryLazy<'a, A, E, Config>
impl<'a, A, E, Config> Sync for TryLazy<'a, A, E, Config>
impl<'a, A, E, Config> Unpin for TryLazy<'a, A, E, Config>
impl<'a, A, E, Config> UnwindSafe for TryLazy<'a, A, E, Config>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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