pub trait SendDeferrable<'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.
SendDeferrable is the thread-safe counterpart of
Deferrable. Where Deferrable accepts any
FnOnce() -> Self + 'a, this trait requires FnOnce() -> Self + Send + 'a,
ensuring the closure can be sent across thread boundaries. The two traits
are independent: implementing one does not require implementing the other.
Unlike SendCloneFn, 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 toxwhen 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§
Sourcefn send_defer(f: impl FnOnce() -> Self + Send + 'a) -> Selfwhere
Self: Sized,
fn send_defer(f: impl FnOnce() -> Self + Send + 'a) -> Selfwhere
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§
impl<'a, A> SendDeferrable<'a> for Lazy<'a, A, ArcLazyConfig>
§Type Parameters
'a: The lifetime of the computation.A: The type of the computed value.
impl<'a, A, E> SendDeferrable<'a> for TryLazy<'a, A, E, ArcLazyConfig>
§Type Parameters
'a: The lifetime of the computation.A: The type of the computed value.E: The type of the error.
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.
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.