1
- use crate :: ops:: Try ;
1
+ use crate :: ops:: { NeverShortCircuit , Try } ;
2
2
3
3
/// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
4
4
///
@@ -8,36 +8,40 @@ use crate::ops::Try;
8
8
#[ derive( Debug ) ]
9
9
pub struct ByRefSized < ' a , I > ( pub & ' a mut I ) ;
10
10
11
+ // The following implementations use UFCS-style, rather than trusting autoderef,
12
+ // to avoid accidentally calling the `&mut Iterator` implementations.
13
+
11
14
#[ unstable( feature = "std_internals" , issue = "none" ) ]
12
15
impl < I : Iterator > Iterator for ByRefSized < ' _ , I > {
13
16
type Item = I :: Item ;
14
17
15
18
#[ inline]
16
19
fn next ( & mut self ) -> Option < Self :: Item > {
17
- self . 0 . next ( )
20
+ I :: next ( self . 0 )
18
21
}
19
22
20
23
#[ inline]
21
24
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
22
- self . 0 . size_hint ( )
25
+ I :: size_hint ( self . 0 )
23
26
}
24
27
25
28
#[ inline]
26
29
fn advance_by ( & mut self , n : usize ) -> Result < ( ) , usize > {
27
- self . 0 . advance_by ( n)
30
+ I :: advance_by ( self . 0 , n)
28
31
}
29
32
30
33
#[ inline]
31
34
fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
32
- self . 0 . nth ( n)
35
+ I :: nth ( self . 0 , n)
33
36
}
34
37
35
38
#[ inline]
36
39
fn fold < B , F > ( self , init : B , f : F ) -> B
37
40
where
38
41
F : FnMut ( B , Self :: Item ) -> B ,
39
42
{
40
- self . 0 . fold ( init, f)
43
+ // `fold` needs ownership, so this can't forward directly.
44
+ I :: try_fold ( self . 0 , init, NeverShortCircuit :: wrap_mut_2 ( f) ) . 0
41
45
}
42
46
43
47
#[ inline]
@@ -46,33 +50,34 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
46
50
F : FnMut ( B , Self :: Item ) -> R ,
47
51
R : Try < Output = B > ,
48
52
{
49
- self . 0 . try_fold ( init, f)
53
+ I :: try_fold ( self . 0 , init, f)
50
54
}
51
55
}
52
56
53
57
#[ unstable( feature = "std_internals" , issue = "none" ) ]
54
58
impl < I : DoubleEndedIterator > DoubleEndedIterator for ByRefSized < ' _ , I > {
55
59
#[ inline]
56
60
fn next_back ( & mut self ) -> Option < Self :: Item > {
57
- self . 0 . next_back ( )
61
+ I :: next_back ( self . 0 )
58
62
}
59
63
60
64
#[ inline]
61
65
fn advance_back_by ( & mut self , n : usize ) -> Result < ( ) , usize > {
62
- self . 0 . advance_back_by ( n)
66
+ I :: advance_back_by ( self . 0 , n)
63
67
}
64
68
65
69
#[ inline]
66
70
fn nth_back ( & mut self , n : usize ) -> Option < Self :: Item > {
67
- self . 0 . nth_back ( n)
71
+ I :: nth_back ( self . 0 , n)
68
72
}
69
73
70
74
#[ inline]
71
75
fn rfold < B , F > ( self , init : B , f : F ) -> B
72
76
where
73
77
F : FnMut ( B , Self :: Item ) -> B ,
74
78
{
75
- self . 0 . rfold ( init, f)
79
+ // `rfold` needs ownership, so this can't forward directly.
80
+ I :: try_rfold ( self . 0 , init, NeverShortCircuit :: wrap_mut_2 ( f) ) . 0
76
81
}
77
82
78
83
#[ inline]
@@ -81,6 +86,6 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
81
86
F : FnMut ( B , Self :: Item ) -> R ,
82
87
R : Try < Output = B > ,
83
88
{
84
- self . 0 . try_rfold ( init, f)
89
+ I :: try_rfold ( self . 0 , init, f)
85
90
}
86
91
}
0 commit comments