Skip to main content

fp_library/classes/
clone_fn.rs

1//! Cloneable wrappers over closures for generic handling of functions in higher-kinded contexts.
2//!
3//! The [`CloneFn`] trait defines the type of the wrapper, parameterized by
4//! a [`ClosureMode`](crate::dispatch::ClosureMode) that determines whether the
5//! wrapped closure takes owned values ([`Val`](crate::dispatch::Val)) or
6//! references ([`Ref`](crate::dispatch::Ref)).
7//!
8//! The [`LiftFn`] trait provides construction of Val-mode wrapped functions.
9//!
10//! ### Examples
11//!
12//! ```
13//! use fp_library::{
14//! 	brands::*,
15//! 	functions::*,
16//! };
17//!
18//! let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
19//! assert_eq!(f(5), 10);
20//! ```
21
22#[fp_macros::document_module]
23mod inner {
24	use {
25		crate::{
26			classes::RefCountedPointer,
27			dispatch::{
28				ClosureMode,
29				Ref,
30				Val,
31			},
32		},
33		fp_macros::*,
34		std::ops::Deref,
35	};
36
37	/// A trait for cloneable wrappers over closures, parameterized by closure mode.
38	///
39	/// This trait is implemented by "Brand" types (like [`RcFnBrand`][crate::brands::RcFnBrand])
40	/// to provide a way to type-check cloneable wrappers over closures (`Rc<dyn Fn...>` or
41	/// `Arc<dyn Fn...>`) in a generic context.
42	///
43	/// The `Mode` parameter determines the `Deref` target:
44	/// - [`Val`]: wraps `Fn(A) -> B` (by-value closures)
45	/// - [`Ref`]: wraps `Fn(&A) -> B` (by-reference closures)
46	///
47	/// The default mode is [`Val`], so existing code using `CloneFn` without
48	/// a mode parameter is unchanged.
49	///
50	/// For construction of wrapped functions, see [`LiftFn`].
51	#[document_type_parameters(
52		"The closure mode. Either [`Val`] (by-value, default) or [`Ref`] (by-reference)."
53	)]
54	pub trait CloneFn<Mode: ClosureMode = Val> {
55		/// The pointer brand backing this function wrapper.
56		///
57		/// Each `CloneFn` implementor is backed by exactly one reference-counted
58		/// pointer type. For [`FnBrand<P>`](crate::brands::FnBrand), this is `P`.
59		type PointerBrand: RefCountedPointer;
60
61		/// The type of the cloneable function wrapper.
62		///
63		/// This associated type represents the concrete type of the wrapper (e.g., `Rc<dyn Fn(A) -> B>`)
64		/// that implements `Clone` and dereferences to the underlying closure.
65		type Of<'a, A: 'a, B: 'a>: 'a + Clone + Deref<Target = Mode::Target<'a, A, B>>;
66	}
67
68	/// A trait for constructing Val-mode cloneable function wrappers from closures.
69	///
70	/// This is separated from [`CloneFn`] because the closure parameter type
71	/// (`Fn(A) -> B`) is specific to [`Val`] mode. For by-reference mode
72	/// construction, see [`RefLiftFn`].
73	pub trait LiftFn: CloneFn<Val> {
74		/// Creates a new cloneable function wrapper.
75		///
76		/// This function wraps the provided closure `f` into a cloneable function.
77		#[document_signature]
78		///
79		#[document_type_parameters(
80			"The lifetime of the function and its captured data.",
81			"The input type of the function.",
82			"The output type of the function."
83		)]
84		///
85		#[document_parameters("The closure to wrap.")]
86		#[document_returns("The wrapped cloneable function.")]
87		#[document_examples]
88		///
89		/// ```
90		/// use fp_library::{
91		/// 	brands::*,
92		/// 	functions::*,
93		/// };
94		///
95		/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
96		/// assert_eq!(f(5), 10);
97		/// ```
98		fn new<'a, A: 'a, B: 'a>(f: impl 'a + Fn(A) -> B) -> <Self as CloneFn>::Of<'a, A, B>;
99	}
100
101	/// Creates a new cloneable function wrapper.
102	///
103	/// Free function version that dispatches to [the type class' associated function][`LiftFn::new`].
104	#[document_signature]
105	///
106	#[document_type_parameters(
107		"The lifetime of the function and its captured data.",
108		"The brand of the cloneable function wrapper.",
109		"The input type of the function.",
110		"The output type of the function."
111	)]
112	///
113	#[document_parameters("The closure to wrap.")]
114	#[document_returns("The wrapped cloneable function.")]
115	#[document_examples]
116	///
117	/// ```
118	/// use fp_library::{
119	/// 	brands::*,
120	/// 	functions::*,
121	/// };
122	///
123	/// let f = lift_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2);
124	/// assert_eq!(f(5), 10);
125	/// ```
126	pub fn new<'a, Brand, A, B>(f: impl 'a + Fn(A) -> B) -> <Brand as CloneFn>::Of<'a, A, B>
127	where
128		Brand: LiftFn, {
129		<Brand as LiftFn>::new(f)
130	}
131
132	/// A trait for constructing Ref-mode cloneable function wrappers from closures.
133	///
134	/// This mirrors [`LiftFn`] but for by-reference closures (`Fn(&A) -> B`).
135	pub trait RefLiftFn: CloneFn<Ref> {
136		/// Creates a new cloneable by-reference function wrapper.
137		///
138		/// This function wraps the provided closure `f` (which takes `&A`)
139		/// into a cloneable function.
140		#[document_signature]
141		///
142		#[document_type_parameters(
143			"The lifetime of the function and its captured data.",
144			"The input type (the closure receives `&A`).",
145			"The output type of the function."
146		)]
147		///
148		#[document_parameters("The by-reference closure to wrap.")]
149		#[document_returns("The wrapped cloneable by-reference function.")]
150		#[document_examples]
151		///
152		/// ```
153		/// use fp_library::{
154		/// 	brands::*,
155		/// 	functions::*,
156		/// };
157		///
158		/// let f = ref_lift_fn_new::<RcFnBrand, _, _>(|x: &i32| *x * 2);
159		/// assert_eq!(f(&5), 10);
160		/// ```
161		fn ref_new<'a, A: 'a, B: 'a>(
162			f: impl 'a + Fn(&A) -> B
163		) -> <Self as CloneFn<Ref>>::Of<'a, A, B>;
164	}
165
166	/// Creates a new cloneable by-reference function wrapper.
167	///
168	/// Free function version that dispatches to [`RefLiftFn::ref_new`].
169	#[document_signature]
170	///
171	#[document_type_parameters(
172		"The lifetime of the function and its captured data.",
173		"The brand of the cloneable function wrapper.",
174		"The input type (the closure receives `&A`).",
175		"The output type of the function."
176	)]
177	///
178	#[document_parameters("The by-reference closure to wrap.")]
179	#[document_returns("The wrapped cloneable by-reference function.")]
180	#[document_examples]
181	///
182	/// ```
183	/// use fp_library::{
184	/// 	brands::*,
185	/// 	functions::*,
186	/// };
187	///
188	/// let f = ref_lift_fn_new::<RcFnBrand, _, _>(|x: &i32| *x * 2);
189	/// assert_eq!(f(&5), 10);
190	/// ```
191	pub fn ref_new<'a, Brand, A, B>(
192		f: impl 'a + Fn(&A) -> B
193	) -> <Brand as CloneFn<Ref>>::Of<'a, A, B>
194	where
195		Brand: RefLiftFn, {
196		<Brand as RefLiftFn>::ref_new(f)
197	}
198}
199
200pub use inner::*;