fp_library/classes/
clonable_fn.rs

1use crate::{classes::Category, make_type_apply};
2use std::ops::Deref;
3
4/// Abstraction for clonable wrappers over closures.
5///
6/// This trait is implemented by "Brand" types (like [`ArcFnBrand`][crate::brands::ArcFnBrand]
7/// and [`RcFnBrand`][crate::brands::RcFnBrand]) to provide a way to construct
8/// and type-check clonable wrappers over closures (`Arc<dyn Fn...>` or
9/// `Rc<dyn Fn...>`) in a generic context, allowing library users to choose
10/// between implementations at function call sites.
11///
12/// The lifetime `'a` ensures the function doesn't outlive referenced data,
13/// while generic types `A` and `B` represent the input and output types, respectively.
14pub trait ClonableFn: Category {
15	type Output<'a, A: 'a, B: 'a>: Clone + Deref<Target = dyn 'a + Fn(A) -> B>;
16
17	fn new<'a, A: 'a, B: 'a>(f: impl 'a + Fn(A) -> B) -> ApplyFn<'a, Self, A, B>;
18}
19
20make_type_apply!(ApplyFn, ClonableFn, ('a), (A, B), "' -> * -> *");