@@ -3,21 +3,24 @@ use crate::mem::swap;
3
3
/// An iterator that removes all but the first of consecutive elements in a
4
4
/// given iterator according to the [`PartialEq`] trait implementation.
5
5
///
6
- /// This `struct` is created by the [` dedup`] method on [`Iterator`]. See its
7
- /// documentation for more.
6
+ /// This `struct` is created by [`Iterator:: dedup`].
7
+ /// See its documentation for more.
8
8
///
9
- /// [`dedup`]: Iterator::dedup
10
- /// [`Iterator`]: trait.Iterator.html
9
+ /// [`Iterator::dedup`]: Iterator::dedup
11
10
#[ unstable( feature = "iter_dedup" , reason = "recently added" , issue = "83748" ) ]
12
11
#[ derive( Debug , Clone , Copy ) ]
13
12
pub struct Dedup < I , T > {
14
13
inner : I ,
15
14
last : Option < T > ,
16
15
}
17
16
18
- impl < I , T > Dedup < I , T > {
19
- pub ( crate ) const fn new ( inner : I ) -> Self {
20
- Self { inner, last : None }
17
+ impl < I , T > Dedup < I , T >
18
+ where
19
+ I : Iterator < Item = T > ,
20
+ {
21
+ pub ( crate ) fn new ( inner : I ) -> Self {
22
+ let mut inner = inner;
23
+ Self { last : inner. next ( ) , inner }
21
24
}
22
25
}
23
26
30
33
type Item = T ;
31
34
32
35
fn next ( & mut self ) -> Option < Self :: Item > {
33
- if self . last . is_none ( ) {
34
- self . last = self . inner . next ( ) ;
35
- }
36
-
37
36
let last_item = self . last . as_ref ( ) ?;
38
37
let mut next = loop {
39
38
let curr = self . inner . next ( ) ;
@@ -58,11 +57,10 @@ where
58
57
/// An iterator that removes all but the first of consecutive elements in a
59
58
/// given iterator satisfying a given equality relation.
60
59
///
61
- /// This `struct` is created by the [`dedup_by`] method on [` Iterator`].
60
+ /// This `struct` is created by [` Iterator::dedup_by `].
62
61
/// See its documentation for more.
63
62
///
64
- /// [`dedup_by`]: Iterator::dedup_by
65
- /// [`Iterator`]: trait.Iterator.html
63
+ /// [`Iterator::dedup_by`]: Iterator::dedup_by
66
64
#[ unstable( feature = "iter_dedup" , reason = "recently added" , issue = "83748" ) ]
67
65
#[ derive( Debug , Clone , Copy ) ]
68
66
pub struct DedupBy < I , F , T > {
@@ -71,9 +69,13 @@ pub struct DedupBy<I, F, T> {
71
69
last : Option < T > ,
72
70
}
73
71
74
- impl < I , F , T > DedupBy < I , F , T > {
75
- pub ( crate ) const fn new ( inner : I , same_bucket : F ) -> Self {
76
- Self { inner, same_bucket, last : None }
72
+ impl < I , F , T > DedupBy < I , F , T >
73
+ where
74
+ I : Iterator < Item = T > ,
75
+ {
76
+ pub ( crate ) fn new ( inner : I , same_bucket : F ) -> Self {
77
+ let mut inner = inner;
78
+ Self { last : inner. next ( ) , inner, same_bucket }
77
79
}
78
80
}
79
81
@@ -114,11 +116,10 @@ where
114
116
/// An iterator that removes all but the first of consecutive elements in a
115
117
/// given iterator that resolve to the same key.
116
118
///
117
- /// This `struct` is created by the [`dedup_by_key`] method on [` Iterator`].
119
+ /// This `struct` is created by [` Iterator::dedup_by_key `].
118
120
/// See its documentation for more.
119
121
///
120
- /// [`dedup_by_key`]: Iterator::dedup_by_key
121
- /// [`Iterator`]: trait.Iterator.html
122
+ /// [`Iterator::dedup_by_key`]: Iterator::dedup_by_key
122
123
#[ unstable( feature = "iter_dedup" , reason = "recently added" , issue = "83748" ) ]
123
124
#[ derive( Debug , Clone , Copy ) ]
124
125
pub struct DedupByKey < I , F , T > {
@@ -127,9 +128,13 @@ pub struct DedupByKey<I, F, T> {
127
128
last : Option < T > ,
128
129
}
129
130
130
- impl < I , F , T > DedupByKey < I , F , T > {
131
- pub ( crate ) const fn new ( inner : I , key : F ) -> Self {
132
- Self { inner, key, last : None }
131
+ impl < I , F , T > DedupByKey < I , F , T >
132
+ where
133
+ I : Iterator < Item = T > ,
134
+ {
135
+ pub ( crate ) fn new ( inner : I , key : F ) -> Self {
136
+ let mut inner = inner;
137
+ Self { last : inner. next ( ) , inner, key }
133
138
}
134
139
}
135
140
0 commit comments