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