]> git.proxmox.com Git - rustc.git/blob - library/core/src/iter/adapters/by_ref_sized.rs
0b5e2a89ef3dedfe43fd6f6b95e638ba14ecb3cd
[rustc.git] / library / core / src / iter / adapters / by_ref_sized.rs
1 use crate::ops::Try;
2
3 /// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
4 ///
5 /// Ideally this will no longer be required, eventually, but as can be seen in
6 /// the benchmarks (as of Feb 2022 at least) `by_ref` can have performance cost.
7 pub(crate) struct ByRefSized<'a, I>(pub &'a mut I);
8
9 impl<I: Iterator> Iterator for ByRefSized<'_, I> {
10 type Item = I::Item;
11
12 fn next(&mut self) -> Option<Self::Item> {
13 self.0.next()
14 }
15
16 fn size_hint(&self) -> (usize, Option<usize>) {
17 self.0.size_hint()
18 }
19
20 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
21 self.0.advance_by(n)
22 }
23
24 fn nth(&mut self, n: usize) -> Option<Self::Item> {
25 self.0.nth(n)
26 }
27
28 fn fold<B, F>(self, init: B, f: F) -> B
29 where
30 F: FnMut(B, Self::Item) -> B,
31 {
32 self.0.fold(init, f)
33 }
34
35 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
36 where
37 F: FnMut(B, Self::Item) -> R,
38 R: Try<Output = B>,
39 {
40 self.0.try_fold(init, f)
41 }
42 }