1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Pointer brands that can perform unsized coercion to `dyn Fn` trait objects.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::*,
//! };
//!
//! let f = coerce_fn::<RcBrand, _, _>(|x: i32| x + 1);
//! assert_eq!(f(1), 2);
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::classes::*,
fp_macros::*,
};
/// Trait for pointer brands that can perform unsized coercion to `dyn Fn`.
pub trait UnsizedCoercible: RefCountedPointer + 'static {
/// Coerces a sized closure to a `dyn Fn` wrapped in this pointer type.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the closure.",
"The input type of the function.",
"The output type of the function."
)]
///
#[document_parameters("The closure to coerce.")]
///
#[document_returns("The closure wrapped in the pointer type as a trait object.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// let f = coerce_fn::<RcBrand, _, _>(|x: i32| x + 1);
/// assert_eq!(f(1), 2);
/// ```
fn coerce_fn<'a, A: 'a, B: 'a>(
f: impl 'a + Fn(A) -> B
) -> Self::CloneableOf<'a, dyn 'a + Fn(A) -> B>;
}
/// Coerces a sized closure to a `dyn Fn` wrapped in this pointer type.
///
/// Free function version that dispatches to [the type class' associated function][`UnsizedCoercible::coerce_fn`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the closure.",
"The brand of the pointer.",
"The input type of the function.",
"The output type of the function."
)]
///
#[document_parameters("The closure to coerce.")]
///
#[document_returns("The closure wrapped in the pointer type as a trait object.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::unsized_coercible::*,
/// functions::*,
/// };
///
/// let f = coerce_fn::<RcBrand, _, _>(|x: i32| x + 1);
/// assert_eq!(f(1), 2);
/// ```
pub fn coerce_fn<'a, Brand: UnsizedCoercible, A: 'a, B: 'a>(
func: impl 'a + Fn(A) -> B
) -> Brand::CloneableOf<'a, dyn 'a + Fn(A) -> B> {
Brand::coerce_fn::<A, B>(func)
}
}
pub use inner::*;