1
1
use crate :: iter:: { FusedIterator , Peekable } ;
2
2
3
3
/// An iterator that maps the values of `iter` with `f`.
4
+ ///
5
+ /// This struct is created by the [`peek_map`] method on [`Peekable`]. See its
6
+ /// documentation for more.
7
+ ///
8
+ /// [`peek_map`]: Peekable::peek_map
9
+ /// [`Peekable`]: struct.Peekable.html
10
+ ///
11
+ /// # Examples
12
+ ///
13
+ /// Basic usage:
14
+ ///
15
+ /// ```
16
+ /// #![feature(peek_map)]
17
+ ///
18
+ /// let a = [1, 2, 3];
19
+ /// let mut iter = a.into_iter().peekable().peek_map(|x, next| x * *next.unwrap_or(&1));
20
+ ///
21
+ /// assert_eq!(iter.next(), Some(2));
22
+ /// assert_eq!(iter.next(), Some(6));
23
+ /// assert_eq!(iter.next(), Some(3));
24
+ /// assert_eq!(iter.next(), None);
25
+ /// ```
4
26
#[ must_use = "iterators are lazy and do nothing unless consumed" ]
5
27
#[ unstable( feature = "peek_map" , issue = "118474" ) ]
6
28
#[ derive( Debug ) ]
7
- pub struct PeekMap < P , F > {
8
- pub ( crate ) peekable : P ,
29
+ pub struct PeekMap < T , F > {
30
+ pub ( crate ) t : T ,
9
31
f : F ,
10
32
}
11
33
12
34
impl < I : Iterator , F > PeekMap < Peekable < I > , F > {
13
35
pub ( in crate :: iter) fn new ( peekable : Peekable < I > , f : F ) -> PeekMap < Peekable < I > , F > {
14
- PeekMap { peekable, f }
36
+ PeekMap { t : peekable, f }
15
37
}
16
38
}
17
39
@@ -24,12 +46,12 @@ where
24
46
25
47
#[ inline]
26
48
fn next ( & mut self ) -> Option < B > {
27
- Some ( ( & mut self . f ) ( self . peekable . next ( ) ?, self . peekable . peek ( ) ) )
49
+ Some ( ( & mut self . f ) ( self . t . next ( ) ?, self . t . peek ( ) ) )
28
50
}
29
51
30
52
#[ inline]
31
53
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
32
- self . peekable . size_hint ( )
54
+ self . t . size_hint ( )
33
55
}
34
56
}
35
57
#[ unstable( feature = "peek_map" , issue = "118474" ) ]
@@ -38,11 +60,11 @@ where
38
60
F : FnMut ( I :: Item , Option < & I :: Item > ) -> B ,
39
61
{
40
62
fn len ( & self ) -> usize {
41
- self . peekable . len ( )
63
+ self . t . len ( )
42
64
}
43
65
44
66
fn is_empty ( & self ) -> bool {
45
- self . peekable . is_empty ( )
67
+ self . t . is_empty ( )
46
68
}
47
69
}
48
70
#[ unstable( feature = "peek_map" , issue = "118474" ) ]
0 commit comments