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