Skip to main content

Lazy

Struct Lazy 

Source
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,

Source

pub fn evaluate(&self) -> &A

Gets the memoized value, computing on first access.

§Type Signature

A

§Returns

A reference to the memoized value.

§Examples
use fp_library::types::*;

let memo = Lazy::<_, RcLazyConfig>::new(|| 42);
assert_eq!(*memo.evaluate(), 42);
Source§

impl<'a, A> Lazy<'a, A, RcLazyConfig>
where A: 'a,

Source

pub fn new<F>(f: F) -> Self
where 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);
Source

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,

Source

pub fn new<F>(f: F) -> Self
where F: FnOnce() -> A + Send + '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 lazy = Lazy::<_, ArcLazyConfig>::new(|| 42);
assert_eq!(*lazy.evaluate(), 42);
Source

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

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> Deferrable<'a> for Lazy<'a, A, RcLazyConfig>
where A: Clone + 'a,

Source§

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

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,

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, Config> From<Lazy<'a, A, Config>> for Thunk<'a, A>
where A: Clone + 'a, Config: LazyConfig,

Source§

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

Converts to this type from the input type.
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: 'static + Send + Clone, Config: LazyConfig> From<Lazy<'static, A, Config>> for Trampoline<A>

Source§

fn from(lazy: Lazy<'static, A, Config>) -> Self

Converts to this type from the input type.
Source§

impl<A, E, Config> From<Lazy<'static, A, Config>> for TryTrampoline<A, E>
where A: Clone + Send + 'static, E: Send + 'static, Config: LazyConfig,

Source§

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

Converts to this type from the input type.
Source§

impl<'a, A> From<Thunk<'a, A>> for Lazy<'a, A, RcLazyConfig>

Source§

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

Converts to this type from the input type.
Source§

impl<'a, A> From<Trampoline<A>> for Lazy<'a, A, RcLazyConfig>
where A: Send,

Source§

fn from(task: Trampoline<A>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A> SendDeferrable<'a> for Lazy<'a, A, ArcLazyConfig>
where A: 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 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>
where <Config as LazyConfig>::Lazy<'a, A>: Freeze,

§

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

§

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

§

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

§

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

§

impl<'a, A, Config> UnwindSafe for Lazy<'a, A, Config>
where <Config as LazyConfig>::Lazy<'a, A>: 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.