fp_library/classes/
function.rs

1use crate::{classes::Category, make_type_apply};
2use std::ops::Deref;
3
4/// Abstraction for 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 wrappers over closures (`Arc<dyn Fn...>`, `Rc<dyn Fn...>`,
9/// etc.) in a generic context, allowing library users to choose between
10/// 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 Function: Category {
15	type Output<'a, A: 'a, B: 'a>: Deref<Target = dyn 'a + Fn(A) -> B>;
16
17	fn new<'a, A: 'a, B: 'a>(f: impl 'a + Fn(A) -> B) -> ApplyFunction<'a, Self, A, B>;
18}
19
20make_type_apply!(ApplyFunction, Function, ('a), (A, B), "' -> * -> *");