Skip to content

Commit b73241a

Browse files
committed
Added more const_closure functionality.
1 parent f914b82 commit b73241a

File tree

5 files changed

+62
-49
lines changed

5 files changed

+62
-49
lines changed

library/core/src/cmp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ where
12671267
{
12681268
f(v1).cmp(&f(v2))
12691269
}
1270-
min_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
1270+
min_by(v1, v2, ConstFnMutClosure { data: &mut f, func: imp })
12711271
}
12721272

12731273
/// Compares and returns the maximum of two values.
@@ -1352,7 +1352,7 @@ where
13521352
{
13531353
f(v1).cmp(&f(v2))
13541354
}
1355-
max_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
1355+
max_by(v1, v2, ConstFnMutClosure { data: &mut f, func: imp })
13561356
}
13571357

13581358
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types

library/core/src/const_closure.rs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,45 @@ use crate::marker::Destruct;
1616
/// assert!(7 == cl(2));
1717
/// assert!(8 == cl(1));
1818
/// ```
19-
pub(crate) struct ConstFnMutClosure<'a, CapturedData: ?Sized, Function> {
20-
data: &'a mut CapturedData,
21-
func: Function,
19+
pub(crate) struct ConstFnMutClosure<CapturedData, Function> {
20+
/// The Data captured by the Closure.
21+
/// Must be either a (mutable) reference or a tuple of (mutable) references.
22+
pub data: CapturedData,
23+
/// The Function of the Closure, must be: Fn(CapturedData, ClosureArgs) -> ClosureReturn
24+
pub func: Function,
2225
}
2326

24-
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<'a, CapturedData, Function> {
25-
/// Function for creating a new closure.
26-
///
27-
/// `data` is the a mutable borrow of data that is captured from the environment.
28-
///
29-
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
30-
/// and return the return value of the closure.
31-
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
32-
data: &'a mut CapturedData,
33-
func: Function,
34-
) -> Self
35-
where
36-
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
37-
{
38-
Self { data, func }
39-
}
40-
}
41-
42-
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
43-
FnOnce<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
44-
where
45-
Function:
46-
~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct,
47-
{
48-
type Output = ClosureReturnValue;
27+
macro_rules! impl_fn_mut_tuple {
28+
($($var:ident)*) => {
29+
#[allow(unused_parens)]
30+
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
31+
FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
32+
where
33+
Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct,
34+
{
35+
type Output = ClosureReturnValue;
4936

50-
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
51-
self.call_mut(args)
52-
}
53-
}
37+
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
38+
self.call_mut(args)
39+
}
40+
}
41+
#[allow(unused_parens)]
42+
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
43+
FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
44+
where
45+
Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue,
46+
{
47+
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
48+
#[allow(non_snake_case)]
49+
let ($($var),*) = &mut self.data;
50+
(self.func)(($($var),*), args)
51+
}
52+
}
5453

55-
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
56-
FnMut<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
57-
where
58-
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
59-
{
60-
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
61-
(self.func)(self.data, args)
62-
}
63-
}
54+
};
55+
}
56+
impl_fn_mut_tuple!(A);
57+
impl_fn_mut_tuple!(A B);
58+
impl_fn_mut_tuple!(A B C);
59+
impl_fn_mut_tuple!(A B C D);
60+
impl_fn_mut_tuple!(A B C D E);

library/core/src/iter/adapters/array_chunks.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ where
8888
Self: Sized,
8989
F: FnMut(B, Self::Item) -> B,
9090
{
91-
self.try_fold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
91+
self.try_fold(
92+
init,
93+
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
94+
)
95+
.0
9296
}
9397
}
9498

@@ -132,7 +136,11 @@ where
132136
Self: Sized,
133137
F: FnMut(B, Self::Item) -> B,
134138
{
135-
self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
139+
self.try_rfold(
140+
init,
141+
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
142+
)
143+
.0
136144
}
137145
}
138146

library/core/src/iter/adapters/by_ref_sized.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
4444
F: FnMut(B, Self::Item) -> B,
4545
{
4646
// `fold` needs ownership, so this can't forward directly.
47-
I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
48-
.0
47+
I::try_fold(
48+
self.0,
49+
init,
50+
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
51+
)
52+
.0
4953
}
5054

5155
#[inline]
@@ -84,7 +88,7 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
8488
I::try_rfold(
8589
self.0,
8690
init,
87-
ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
91+
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
8892
)
8993
.0
9094
}

library/core/src/iter/adapters/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ where
209209
Self: Sized,
210210
F: FnMut(B, Self::Item) -> B,
211211
{
212-
self.try_fold(init, ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0
212+
self.try_fold(
213+
init,
214+
ConstFnMutClosure { data: &mut fold, func: NeverShortCircuit::wrap_mut_2_imp },
215+
)
216+
.0
213217
}
214218
}
215219

0 commit comments

Comments
 (0)