]> git.proxmox.com Git - rustc.git/blob - library/core/src/iter/adapters/by_ref_sized.rs
New upstream version 1.63.0+dfsg1
[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 #[unstable(feature = "std_internals", issue = "none")]
8 #[derive(Debug)]
9 pub struct ByRefSized<'a, I>(pub &'a mut I);
10
11 #[unstable(feature = "std_internals", issue = "none")]
12 impl<I: Iterator> Iterator for ByRefSized<'_, I> {
13 type Item = I::Item;
14
15 #[inline]
16 fn next(&mut self) -> Option<Self::Item> {
17 self.0.next()
18 }
19
20 #[inline]
21 fn size_hint(&self) -> (usize, Option<usize>) {
22 self.0.size_hint()
23 }
24
25 #[inline]
26 fn advance_by(&mut self, n: usize) -> Result<(), usize> {
27 self.0.advance_by(n)
28 }
29
30 #[inline]
31 fn nth(&mut self, n: usize) -> Option<Self::Item> {
32 self.0.nth(n)
33 }
34
35 #[inline]
36 fn fold<B, F>(self, init: B, f: F) -> B
37 where
38 F: FnMut(B, Self::Item) -> B,
39 {
40 self.0.fold(init, f)
41 }
42
43 #[inline]
44 fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
45 where
46 F: FnMut(B, Self::Item) -> R,
47 R: Try<Output = B>,
48 {
49 self.0.try_fold(init, f)
50 }
51 }
52
53 #[unstable(feature = "std_internals", issue = "none")]
54 impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
55 #[inline]
56 fn next_back(&mut self) -> Option<Self::Item> {
57 self.0.next_back()
58 }
59
60 #[inline]
61 fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
62 self.0.advance_back_by(n)
63 }
64
65 #[inline]
66 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
67 self.0.nth_back(n)
68 }
69
70 #[inline]
71 fn rfold<B, F>(self, init: B, f: F) -> B
72 where
73 F: FnMut(B, Self::Item) -> B,
74 {
75 self.0.rfold(init, f)
76 }
77
78 #[inline]
79 fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
80 where
81 F: FnMut(B, Self::Item) -> R,
82 R: Try<Output = B>,
83 {
84 self.0.try_rfold(init, f)
85 }
86 }