Skip to main content

SendDeferrable

Trait SendDeferrable 

Source
pub trait SendDeferrable<'a>: Deferrable<'a> {
    // Required method
    fn send_defer(f: impl FnOnce() -> Self + Send + 'a) -> Self
       where Self: Sized;
}
Expand description

A trait for deferred lazy evaluation with thread-safe thunks.

This extends Deferrable with the additional requirement that the thunk must be Send, following the same supertrait pattern used by SendCloneableFn: CloneableFn.

Every SendDeferrable type is also Deferrable, so generic code written against Deferrable accepts both single-threaded and thread-safe types.

Unlike SendCloneableFn, which wraps multi-use Fn closures that are Send + Sync, this trait accepts a FnOnce closure that only needs to be Send (not Sync), since deferred computations are executed at most once.

§Laws

SendDeferrable instances must satisfy the following law:

  • Transparency: send_defer(|| x) is observationally equivalent to x when evaluated.

§Why there is no generic fix

As with Deferrable, lazy self-reference requires shared ownership and interior mutability, which are properties specific to Lazy. The concrete function arc_lazy_fix provides this capability for ArcLazy specifically.

§Type Parameters

  • 'a: The lifetime of the computation.

§Examples

Transparency law for ArcLazy:

use fp_library::{
	functions::*,
	types::*,
};

// Transparency: send_defer(|| x) is equivalent to x when evaluated.
let x = ArcLazy::pure(42);
let deferred: ArcLazy<i32> = send_defer(|| ArcLazy::pure(42));
assert_eq!(*deferred.evaluate(), *x.evaluate());

Required Methods§

Source

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

Creates a deferred value from a thread-safe thunk.

§Type Signature

(() -> Self) -> Self

§Parameters
  • f: The function that produces the value.
§Returns

A deferred value.

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

let memo: ArcLazy<i32> = send_defer(|| ArcLazy::new(|| 42));
assert_eq!(*memo.evaluate(), 42);

Implementors§

Source§

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

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
Source§

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

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the computed value.
  • E: The type of the error.
Source§

impl<'a, A: Send + 'a> SendDeferrable<'a> for SendThunk<'a, A>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the value produced by the computation.
Source§

impl<'a, A: Send + 'a, E: Send + 'a> SendDeferrable<'a> for TrySendThunk<'a, A, E>

§Type Parameters
  • 'a: The lifetime of the computation.
  • A: The type of the success value.
  • E: The type of the error value.