pub trait SendCloneableFn: CloneableFn {
type SendOf<'a, A, B>: Clone + Send + Sync + Deref<Target = dyn Fn(A) -> B + Send + Sync + 'a>;
// Required method
fn send_cloneable_fn_new<'a, A, B>(
f: impl 'a + Fn(A) -> B + Send + Sync,
) -> <Self as SendCloneableFn>::SendOf<'a, A, B>;
}Expand description
Abstraction for thread-safe cloneable wrappers over closures.
This trait extends CloneableFn to enforce Send + Sync bounds on the
wrapped closure and the wrapper itself. This is implemented by types like
ArcFnBrand but not RcFnBrand.
The lifetime 'a ensures the function doesn’t outlive referenced data,
while generic types A and B represent the input and output types, respectively.
Required Associated Types§
Sourcetype SendOf<'a, A, B>: Clone + Send + Sync + Deref<Target = dyn Fn(A) -> B + Send + Sync + 'a>
type SendOf<'a, A, B>: Clone + Send + Sync + Deref<Target = dyn Fn(A) -> B + Send + Sync + 'a>
The type of the thread-safe cloneable function wrapper.
This associated type represents the concrete type of the wrapper (e.g., Arc<dyn Fn(A) -> B + Send + Sync>)
that implements Clone, Send, Sync and dereferences to the underlying closure.
Required Methods§
Sourcefn send_cloneable_fn_new<'a, A, B>(
f: impl 'a + Fn(A) -> B + Send + Sync,
) -> <Self as SendCloneableFn>::SendOf<'a, A, B>
fn send_cloneable_fn_new<'a, A, B>( f: impl 'a + Fn(A) -> B + Send + Sync, ) -> <Self as SendCloneableFn>::SendOf<'a, A, B>
Creates a new thread-safe cloneable function wrapper.
This method wraps a closure into a thread-safe cloneable function wrapper.
§Type Signature
forall a b. (a -> b) -> a -> b
§Type Parameters
'a: The lifetime of the function and its captured data.A: The input type of the function.B: The output type of the function.
§Parameters
f: The closure to wrap. Must beSend + Sync._: The input value to the function.
§Returns
The wrapped thread-safe cloneable function.
§Examples
use fp_library::{functions::*, brands::*};
use std::thread;
let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x * 2);
// Can be sent to another thread
let handle = thread::spawn(move || {
assert_eq!(f(5), 10);
});
handle.join().unwrap();Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.