2 const_closure::ConstFnMutClosure,
3 ops::{NeverShortCircuit, Try},
6 /// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
8 /// Ideally this will no longer be required, eventually, but as can be seen in
9 /// the benchmarks (as of Feb 2022 at least) `by_ref` can have performance cost.
10 #[unstable(feature = "std_internals", issue = "none")]
12 pub struct ByRefSized<'a, I>(pub &'a mut I);
14 // The following implementations use UFCS-style, rather than trusting autoderef,
15 // to avoid accidentally calling the `&mut Iterator` implementations.
17 #[unstable(feature = "std_internals", issue = "none")]
18 impl<I: Iterator> Iterator for ByRefSized<'_, I> {
22 fn next(&mut self) -> Option<Self::Item> {
27 fn size_hint(&self) -> (usize, Option<usize>) {
32 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
33 I::advance_by(self.0, n)
37 fn nth(&mut self, n: usize) -> Option<Self::Item> {
42 fn fold<B, F>(self, init: B, mut f: F) -> B
44 F: FnMut(B, Self::Item) -> B,
46 // `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))
52 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
54 F: FnMut(B, Self::Item) -> R,
57 I::try_fold(self.0, init, f)
61 #[unstable(feature = "std_internals", issue = "none")]
62 impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
64 fn next_back(&mut self) -> Option<Self::Item> {
69 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
70 I::advance_back_by(self.0, n)
74 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
75 I::nth_back(self.0, n)
79 fn rfold<B, F>(self, init: B, mut f: F) -> B
81 F: FnMut(B, Self::Item) -> B,
83 // `rfold` needs ownership, so this can't forward directly.
87 ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
93 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
95 F: FnMut(B, Self::Item) -> R,
98 I::try_rfold(self.0, init, f)