Skip to content

Commit 10739d4

Browse files
committed
Add back ConstFnMutClosure::new, fix formatting
1 parent 9a641a5 commit 10739d4

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

library/core/src/cmp.rs

+2-2
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 { data: &mut f, func: imp })
1270+
min_by(v1, v2, ConstFnMutClosure::new(&mut f, 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 { data: &mut f, func: imp })
1355+
max_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
13561356
}
13571357

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

library/core/src/const_closure.rs

+40-23
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,53 @@ pub(crate) struct ConstFnMutClosure<CapturedData, Function> {
2323
/// The Function of the Closure, must be: Fn(CapturedData, ClosureArgs) -> ClosureReturn
2424
pub func: Function,
2525
}
26+
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<&'a mut CapturedData, Function> {
27+
/// Function for creating a new closure.
28+
///
29+
/// `data` is the a mutable borrow of data that is captured from the environment.
30+
/// If you want Data to be a tuple of mutable Borrows, the struct must be constructed manually.
31+
///
32+
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
33+
/// and return the return value of the closure.
34+
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
35+
data: &'a mut CapturedData,
36+
func: Function,
37+
) -> Self
38+
where
39+
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
40+
{
41+
Self { data, func }
42+
}
43+
}
2644

2745
macro_rules! impl_fn_mut_tuple {
2846
($($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;
47+
#[allow(unused_parens)]
48+
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
49+
FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
50+
where
51+
Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct,
52+
{
53+
type Output = ClosureReturnValue;
3654

37-
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
38-
self.call_mut(args)
55+
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
56+
self.call_mut(args)
57+
}
3958
}
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)
59+
#[allow(unused_parens)]
60+
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
61+
FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
62+
where
63+
Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue,
64+
{
65+
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
66+
#[allow(non_snake_case)]
67+
let ($($var),*) = &mut self.data;
68+
(self.func)(($($var),*), args)
69+
}
5170
}
52-
}
53-
5471
};
55-
}
72+
}
5673
impl_fn_mut_tuple!(A);
5774
impl_fn_mut_tuple!(A B);
5875
impl_fn_mut_tuple!(A B C);

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

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

@@ -136,11 +132,7 @@ where
136132
Self: Sized,
137133
F: FnMut(B, Self::Item) -> B,
138134
{
139-
self.try_rfold(
140-
init,
141-
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
142-
)
143-
.0
135+
self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
144136
}
145137
}
146138

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ 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(
48-
self.0,
49-
init,
50-
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
51-
)
52-
.0
47+
I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
48+
.0
5349
}
5450

5551
#[inline]
@@ -88,7 +84,7 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
8884
I::try_rfold(
8985
self.0,
9086
init,
91-
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
87+
ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
9288
)
9389
.0
9490
}

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

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

0 commit comments

Comments
 (0)