|
1 | 1 | use crate::marker::Destruct; |
2 | 2 |
|
3 | | -/// Struct representing a closure with owned data. |
4 | | -/// |
5 | | -/// Example: |
6 | | -/// ```no_build |
7 | | -/// use crate::const_closure::ConstFnOnceClosure; |
8 | | -/// const fn imp(state: i32, (arg,): (i32,)) -> i32 { |
9 | | -/// state + arg |
10 | | -/// } |
11 | | -/// let i = 5; |
12 | | -/// let cl = ConstFnOnceClosure::new(i, imp); |
13 | | -/// |
14 | | -/// assert!(7 == cl(2)); |
15 | | -/// ``` |
16 | | -pub(crate) struct ConstFnOnceClosure<CapturedData, Function> { |
17 | | - data: CapturedData, |
18 | | - func: Function, |
19 | | -} |
20 | | - |
21 | | -impl<CapturedData, Function> ConstFnOnceClosure<CapturedData, Function> { |
22 | | - /// Function for creating a new closure. |
23 | | - /// |
24 | | - /// `data` is the owned data that is captured from the environment (this data must be `~const Destruct`). |
25 | | - /// |
26 | | - /// `func` is the function of the closure, it gets the data and a tuple of the arguments closure |
27 | | - /// and return the return value of the closure. |
28 | | - #[allow(dead_code)] |
29 | | - pub(crate) const fn new<ClosureArguments, ClosureReturnValue>( |
30 | | - data: CapturedData, |
31 | | - func: Function, |
32 | | - ) -> Self |
33 | | - where |
34 | | - CapturedData: ~const Destruct, |
35 | | - Function: ~const Fn(CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct, |
36 | | - { |
37 | | - Self { data, func } |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -impl<CapturedData, ClosureArguments, Function> const FnOnce<ClosureArguments> |
42 | | - for ConstFnOnceClosure<CapturedData, Function> |
43 | | -where |
44 | | - CapturedData: ~const Destruct, |
45 | | - Function: ~const Fn<(CapturedData, ClosureArguments)> + ~const Destruct, |
46 | | -{ |
47 | | - type Output = Function::Output; |
48 | | - |
49 | | - extern "rust-call" fn call_once(self, args: ClosureArguments) -> Self::Output { |
50 | | - (self.func)(self.data, args) |
51 | | - } |
52 | | -} |
53 | | - |
54 | 3 | /// Struct representing a closure with mutably borrowed data. |
55 | 4 | /// |
56 | 5 | /// Example: |
@@ -112,77 +61,3 @@ where |
112 | 61 | (self.func)(self.data, args) |
113 | 62 | } |
114 | 63 | } |
115 | | - |
116 | | -/// Struct representing a closure with borrowed data. |
117 | | -/// |
118 | | -/// Example: |
119 | | -/// ```no_build |
120 | | -/// use crate::const_closure::ConstFnClosure; |
121 | | -/// |
122 | | -/// const fn imp(state: &i32, (arg,): (i32,)) -> i32 { |
123 | | -/// *state + arg |
124 | | -/// } |
125 | | -/// let i = 5; |
126 | | -/// let cl = ConstFnClosure::new(&i, imp); |
127 | | -/// |
128 | | -/// assert!(7 == cl(2)); |
129 | | -/// assert!(6 == cl(1)); |
130 | | -/// ``` |
131 | | -pub(crate) struct ConstFnClosure<'a, CapturedData: ?Sized, Function> { |
132 | | - data: &'a CapturedData, |
133 | | - func: Function, |
134 | | -} |
135 | | - |
136 | | -impl<'a, CapturedData: ?Sized, Function> ConstFnClosure<'a, CapturedData, Function> { |
137 | | - /// Function for creating a new closure. |
138 | | - /// |
139 | | - /// `data` is the a mutable borrow of data that is captured from the environment. |
140 | | - /// |
141 | | - /// `func` is the function of the closure, it gets the data and a tuple of the arguments closure |
142 | | - /// and return the return value of the closure. |
143 | | - #[allow(dead_code)] |
144 | | - pub(crate) const fn new<ClosureArguments, ClosureReturnValue>( |
145 | | - data: &'a CapturedData, |
146 | | - func: Function, |
147 | | - ) -> Self |
148 | | - where |
149 | | - Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, |
150 | | - { |
151 | | - Self { data, func } |
152 | | - } |
153 | | -} |
154 | | - |
155 | | -impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const |
156 | | - FnOnce<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> |
157 | | -where |
158 | | - Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct, |
159 | | -{ |
160 | | - type Output = ClosureReturnValue; |
161 | | - |
162 | | - extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output { |
163 | | - self.call_mut(args) |
164 | | - } |
165 | | -} |
166 | | - |
167 | | -impl<'a, CapturedData: ?Sized, Function, ClosureArguments, ClosureReturnValue> const |
168 | | - FnMut<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> |
169 | | -where |
170 | | - Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, |
171 | | -{ |
172 | | - extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output { |
173 | | - self.call(args) |
174 | | - } |
175 | | -} |
176 | | - |
177 | | -impl< |
178 | | - 'a, |
179 | | - CapturedData: ?Sized, |
180 | | - Function: ~const Fn(&CapturedData, ClosureArguments) -> ClosureReturnValue, |
181 | | - ClosureArguments, |
182 | | - ClosureReturnValue, |
183 | | -> const Fn<ClosureArguments> for ConstFnClosure<'a, CapturedData, Function> |
184 | | -{ |
185 | | - extern "rust-call" fn call(&self, args: ClosureArguments) -> Self::Output { |
186 | | - (self.func)(self.data, args) |
187 | | - } |
188 | | -} |
0 commit comments