Add back ConstFnMutClosure::new, fix formatting

This commit is contained in:
onestacked 2022-09-30 17:41:01 +02:00
parent 9a641a533c
commit 10739d475e
5 changed files with 48 additions and 47 deletions

View File

@ -1267,7 +1267,7 @@ where
{ {
f(v1).cmp(&f(v2)) f(v1).cmp(&f(v2))
} }
min_by(v1, v2, ConstFnMutClosure { data: &mut f, func: imp }) min_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
} }
/// Compares and returns the maximum of two values. /// Compares and returns the maximum of two values.
@ -1352,7 +1352,7 @@ where
{ {
f(v1).cmp(&f(v2)) f(v1).cmp(&f(v2))
} }
max_by(v1, v2, ConstFnMutClosure { data: &mut f, func: imp }) max_by(v1, v2, ConstFnMutClosure::new(&mut f, imp))
} }
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types // Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types

View File

@ -23,36 +23,53 @@ pub(crate) struct ConstFnMutClosure<CapturedData, Function> {
/// The Function of the Closure, must be: Fn(CapturedData, ClosureArgs) -> ClosureReturn /// The Function of the Closure, must be: Fn(CapturedData, ClosureArgs) -> ClosureReturn
pub func: Function, pub func: Function,
} }
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<&'a mut CapturedData, Function> {
/// Function for creating a new closure.
///
/// `data` is the a mutable borrow of data that is captured from the environment.
/// If you want Data to be a tuple of mutable Borrows, the struct must be constructed manually.
///
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
/// and return the return value of the closure.
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
data: &'a mut CapturedData,
func: Function,
) -> Self
where
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
{
Self { data, func }
}
}
macro_rules! impl_fn_mut_tuple { macro_rules! impl_fn_mut_tuple {
($($var:ident)*) => { ($($var:ident)*) => {
#[allow(unused_parens)] #[allow(unused_parens)]
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function> FnOnce<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
where where
Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct, Function: ~const Fn(($(&mut $var),*), ClosureArguments) -> ClosureReturnValue+ ~const Destruct,
{ {
type Output = ClosureReturnValue; type Output = ClosureReturnValue;
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output { extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
self.call_mut(args) self.call_mut(args)
}
} }
} #[allow(unused_parens)]
#[allow(unused_parens)] impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const
impl<'a, $($var,)* ClosureArguments, Function, ClosureReturnValue> const FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function>
FnMut<ClosureArguments> for ConstFnMutClosure<($(&'a mut $var),*), Function> where
where Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue,
Function: ~const Fn(($(&mut $var),*), ClosureArguments)-> ClosureReturnValue, {
{ extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output { #[allow(non_snake_case)]
#[allow(non_snake_case)] let ($($var),*) = &mut self.data;
let ($($var),*) = &mut self.data; (self.func)(($($var),*), args)
(self.func)(($($var),*), args) }
} }
}
}; };
} }
impl_fn_mut_tuple!(A); impl_fn_mut_tuple!(A);
impl_fn_mut_tuple!(A B); impl_fn_mut_tuple!(A B);
impl_fn_mut_tuple!(A B C); impl_fn_mut_tuple!(A B C);

View File

@ -88,11 +88,7 @@ where
Self: Sized, Self: Sized,
F: FnMut(B, Self::Item) -> B, F: FnMut(B, Self::Item) -> B,
{ {
self.try_fold( self.try_fold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
init,
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
)
.0
} }
} }
@ -136,11 +132,7 @@ where
Self: Sized, Self: Sized,
F: FnMut(B, Self::Item) -> B, F: FnMut(B, Self::Item) -> B,
{ {
self.try_rfold( self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
init,
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
)
.0
} }
} }

View File

@ -44,12 +44,8 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
F: FnMut(B, Self::Item) -> B, F: FnMut(B, Self::Item) -> B,
{ {
// `fold` needs ownership, so this can't forward directly. // `fold` needs ownership, so this can't forward directly.
I::try_fold( I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
self.0, .0
init,
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp },
)
.0
} }
#[inline] #[inline]
@ -88,7 +84,7 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
I::try_rfold( I::try_rfold(
self.0, self.0,
init, init,
ConstFnMutClosure { data: &mut f, func: NeverShortCircuit::wrap_mut_2_imp }, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
) )
.0 .0
} }

View File

@ -209,11 +209,7 @@ where
Self: Sized, Self: Sized,
F: FnMut(B, Self::Item) -> B, F: FnMut(B, Self::Item) -> B,
{ {
self.try_fold( self.try_fold(init, ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0
init,
ConstFnMutClosure { data: &mut fold, func: NeverShortCircuit::wrap_mut_2_imp },
)
.0
} }
} }