pub struct Lazy<'a, A, Config: LazyConfig = RcLazyConfig>(/* private fields */)
where
A: 'a;Expand description
A lazily-computed, memoized value with shared semantics.
The computation runs at most once; subsequent accesses return the cached value.
Cloning a Lazy shares the underlying cache - all clones see the same value.
§Type Parameters
A: The type of the computed value.Config: The memoization configuration (determines Rc vs Arc).
§Fields
0: The internal lazy cell.
§Examples
use fp_library::types::*;
let memo = Lazy::<_, RcLazyConfig>::new(|| 5);
let shared = memo.clone();
// First force computes and caches:
let value = memo.evaluate();
// Second force returns cached value (shared sees same result):
assert_eq!(shared.evaluate(), value);Implementations§
Source§impl<'a, A, Config: LazyConfig> Lazy<'a, A, Config>where
A: 'a,
impl<'a, A, Config: LazyConfig> Lazy<'a, A, Config>where
A: 'a,
Source§impl<'a, A> Lazy<'a, A, RcLazyConfig>where
A: 'a,
impl<'a, A> Lazy<'a, A, RcLazyConfig>where
A: 'a,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnOnce() -> A + 'a,
pub fn new<F>(f: F) -> Selfwhere
F: FnOnce() -> A + 'a,
Creates a new Lazy that will run f on first access.
§Type Signature
forall self. (() -> A) -> self
§Type Parameters
F: The type of the initializer closure.
§Parameters
f: The closure that produces the value.
§Returns
A new Lazy instance.
§Examples
use fp_library::types::*;
let memo = Lazy::<_, RcLazyConfig>::new(|| 42);
assert_eq!(*memo.evaluate(), 42);Sourcepub fn pure(a: A) -> Self
pub fn pure(a: A) -> Self
Creates a Lazy from an already-computed value.
The value is immediately available without any computation.
§Type Signature
forall self. A -> self
§Parameters
a: The pre-computed value to wrap.
§Returns
A new Lazy instance containing the value.
§Examples
use fp_library::types::*;
let lazy = Lazy::<_, RcLazyConfig>::pure(42);
assert_eq!(*lazy.evaluate(), 42);Source§impl<'a, A> Lazy<'a, A, ArcLazyConfig>where
A: 'a,
impl<'a, A> Lazy<'a, A, ArcLazyConfig>where
A: 'a,
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new Lazy that will run f on first access.
§Type Signature
forall self. (() -> A) -> self
§Type Parameters
F: The type of the initializer closure.
§Parameters
f: The closure that produces the value.
§Returns
A new Lazy instance.
§Examples
use fp_library::types::*;
let lazy = Lazy::<_, ArcLazyConfig>::new(|| 42);
assert_eq!(*lazy.evaluate(), 42);Sourcepub fn pure(a: A) -> Selfwhere
A: Send,
pub fn pure(a: A) -> Selfwhere
A: Send,
Creates a Lazy from an already-computed value.
The value is immediately available without any computation.
Requires Send since ArcLazy is thread-safe.
§Type Signature
forall self. A -> self
§Parameters
a: The pre-computed value to wrap.
§Returns
A new Lazy instance containing the value.
§Examples
use fp_library::types::*;
let lazy = Lazy::<_, ArcLazyConfig>::pure(42);
assert_eq!(*lazy.evaluate(), 42);Trait Implementations§
Source§impl<'a, A, Config: LazyConfig> Clone for Lazy<'a, A, Config>where
A: 'a,
impl<'a, A, Config: LazyConfig> Clone for Lazy<'a, A, Config>where
A: 'a,
Source§impl<'a, A> Deferrable<'a> for Lazy<'a, A, RcLazyConfig>where
A: Clone + 'a,
impl<'a, A> Deferrable<'a> for Lazy<'a, A, RcLazyConfig>where
A: Clone + 'a,
Source§fn defer<F>(f: F) -> Self
fn defer<F>(f: F) -> Self
Defers a computation that produces a Lazy value.
This flattens the nested structure: instead of Lazy<Lazy<A>>, we get Lazy<A>.
The inner Lazy is computed only when the outer Lazy 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 Lazy value.
§Examples
use fp_library::{brands::*, classes::*, types::*, functions::*};
let lazy = Lazy::<_, RcLazyConfig>::defer(|| RcLazy::pure(42));
assert_eq!(*lazy.evaluate(), 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, A, Config> From<Lazy<'a, A, Config>> for Thunk<'a, A>where
A: Clone + 'a,
Config: LazyConfig,
impl<'a, A, Config> From<Lazy<'a, A, Config>> for Thunk<'a, A>where
A: Clone + 'a,
Config: LazyConfig,
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,
Source§impl<A: 'static + Send + Clone, Config: LazyConfig> From<Lazy<'static, A, Config>> for Trampoline<A>
impl<A: 'static + Send + Clone, Config: LazyConfig> From<Lazy<'static, A, Config>> for Trampoline<A>
Source§impl<A, E, Config> From<Lazy<'static, A, Config>> for TryTrampoline<A, E>
impl<A, E, Config> From<Lazy<'static, A, Config>> for TryTrampoline<A, E>
Source§impl<'a, A> From<Trampoline<A>> for Lazy<'a, A, RcLazyConfig>where
A: Send,
impl<'a, A> From<Trampoline<A>> for Lazy<'a, A, RcLazyConfig>where
A: Send,
Source§fn from(task: Trampoline<A>) -> Self
fn from(task: Trampoline<A>) -> Self
Source§impl<'a, A> SendDeferrable<'a> for Lazy<'a, A, ArcLazyConfig>
impl<'a, A> SendDeferrable<'a> for Lazy<'a, A, ArcLazyConfig>
Source§fn send_defer<F>(f: F) -> Self
fn send_defer<F>(f: F) -> Self
Defers a computation that produces a thread-safe Lazy value.
This flattens the nested structure: instead of ArcLazy<ArcLazy<A>>, we get ArcLazy<A>.
The inner Lazy is computed only when the outer Lazy 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 ArcLazy value.
§Examples
use fp_library::{brands::*, classes::*, types::*};
let lazy = ArcLazy::send_defer(|| ArcLazy::pure(42));
assert_eq!(*lazy.evaluate(), 42);Auto Trait Implementations§
impl<'a, A, Config> Freeze for Lazy<'a, A, Config>
impl<'a, A, Config> RefUnwindSafe for Lazy<'a, A, Config>
impl<'a, A, Config> Send for Lazy<'a, A, Config>
impl<'a, A, Config> Sync for Lazy<'a, A, Config>
impl<'a, A, Config> Unpin for Lazy<'a, A, Config>
impl<'a, A, Config> UnwindSafe for Lazy<'a, A, 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