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