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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Type-level functions.
use PhantomData;
/// A function that operates purely on the level of types.
///
/// These can be used in `typewit` to
/// [map the type arguments of `TypeEq`](crate::TypeEq::project).
///
/// Type-level functions can also be declared with the
/// [`type_fn`](macro@crate::type_fn) macro.
///
/// # Examples
///
/// ### Manual Implementation
///
/// ```rust
/// use typewit::{TypeFn, CallFn};
///
/// let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
/// let _: String = string;
/// assert_eq!(string, "foo, bar");
///
///
/// struct AddOutput<Lhs>(core::marker::PhantomData<Lhs>);
///
/// // This part is optional,
/// // only necessary to pass the function as a value, not just as a type.
/// impl<Lhs> AddOutput<Lhs> {
/// const NEW: Self = Self(core::marker::PhantomData);
/// }
///
/// impl<Lhs, Rhs> TypeFn<Rhs> for AddOutput<Lhs>
/// where
/// Lhs: core::ops::Add<Rhs>
/// {
/// type Output = Lhs::Output;
/// }
/// ```
///
/// ### Macro-based Implementation
///
/// This example uses the [`type_fn`](macro@crate::type_fn) macro
/// to declare the type-level function,
/// and is otherwise equivalent to the manual one.
///
/// ```rust
/// use typewit::CallFn;
///
/// let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
/// let _: String = string;
/// assert_eq!(string, "foo, bar");
///
/// typewit::type_fn! {
/// struct AddOutput<Lhs>;
///
/// impl<Rhs> Rhs => Lhs::Output
/// where Lhs: core::ops::Add<Rhs>
/// }
/// ```
///
/// Calls the `F` [type-level function](TypeFn) with `T` as its argument.
///
/// # Example
///
/// ```rust
/// use typewit::CallFn;
/// use core::ops::Mul;
///
/// assert_eq!(mul(3u8, &5u8), 15u8);
///
/// fn mul<L, R>(l: L, r: R) -> CallFn<MulOutput<L>, R>
/// where
/// L: core::ops::Mul<R>
/// {
/// l * r
/// }
///
/// // Declares `struct MulOutput<Lhs>`,
/// // a type-level function from `Rhs` to the return type of `Lhs * Rhs`.
/// typewit::type_fn! {
/// struct MulOutput<Lhs>;
///
/// impl<Rhs> Rhs => <Lhs as Mul<Rhs>>::Output
/// where Lhs: core::ops::Mul<Rhs>
/// }
/// ```
///
pub type CallFn<F, T> = Output;
///////////////////////////////////////////////////////
/// Type-level function from `T` to `&'a T`
>);
////////////////
/// Type-level function from `T` to `&'a mut T`
>);
////////////////
/// Type-level function from `T` to `Box<T>`
;
////////////////
/// Type-level identity function
;
////////////////
/// Type-level function which implements `TypeFn` by delegating to `F`
///
/// This is mostly a workaround to write `F: TypeFn<T>` bounds in Rust 1.57.0
/// (trait bounds in `const fn`s were stabilized in Rust 1.61.0).
///
/// Because `Foo<F>: Trait`-style bounds unintentionally work in 1.57.0,
/// this crate uses `Invoke<F>: TypeFn<T>`
/// when the `"rust_1_61"` feature is disabled,
/// and `F: TypeFn<T>` when it is enabled.
///
>);
// This type alias makes it so that docs for newer Rust versions don't
// show `Invoke<F>`, keeping the method bounds the same as in 1.0.0.
pub type InvokeAlias<F> = ;
pub type InvokeAlias<F> = F;