Skip to content

Commit cc480ba

Browse files
committed
Moved initialization of last into contructor
1 parent 1ec9aa8 commit cc480ba

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

library/core/src/iter/adapters/dedup.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ use crate::mem::swap;
33
/// An iterator that removes all but the first of consecutive elements in a
44
/// given iterator according to the [`PartialEq`] trait implementation.
55
///
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.
88
///
9-
/// [`dedup`]: Iterator::dedup
10-
/// [`Iterator`]: trait.Iterator.html
9+
/// [`Iterator::dedup`]: Iterator::dedup
1110
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
1211
#[derive(Debug, Clone, Copy)]
1312
pub struct Dedup<I, T> {
1413
inner: I,
1514
last: Option<T>,
1615
}
1716

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 }
2124
}
2225
}
2326

@@ -30,10 +33,6 @@ where
3033
type Item = T;
3134

3235
fn next(&mut self) -> Option<Self::Item> {
33-
if self.last.is_none() {
34-
self.last = self.inner.next();
35-
}
36-
3736
let last_item = self.last.as_ref()?;
3837
let mut next = loop {
3938
let curr = self.inner.next();
@@ -58,11 +57,10 @@ where
5857
/// An iterator that removes all but the first of consecutive elements in a
5958
/// given iterator satisfying a given equality relation.
6059
///
61-
/// This `struct` is created by the [`dedup_by`] method on [`Iterator`].
60+
/// This `struct` is created by [`Iterator::dedup_by`].
6261
/// See its documentation for more.
6362
///
64-
/// [`dedup_by`]: Iterator::dedup_by
65-
/// [`Iterator`]: trait.Iterator.html
63+
/// [`Iterator::dedup_by`]: Iterator::dedup_by
6664
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
6765
#[derive(Debug, Clone, Copy)]
6866
pub struct DedupBy<I, F, T> {
@@ -71,9 +69,13 @@ pub struct DedupBy<I, F, T> {
7169
last: Option<T>,
7270
}
7371

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 }
7779
}
7880
}
7981

@@ -114,11 +116,10 @@ where
114116
/// An iterator that removes all but the first of consecutive elements in a
115117
/// given iterator that resolve to the same key.
116118
///
117-
/// This `struct` is created by the [`dedup_by_key`] method on [`Iterator`].
119+
/// This `struct` is created by [`Iterator::dedup_by_key`].
118120
/// See its documentation for more.
119121
///
120-
/// [`dedup_by_key`]: Iterator::dedup_by_key
121-
/// [`Iterator`]: trait.Iterator.html
122+
/// [`Iterator::dedup_by_key`]: Iterator::dedup_by_key
122123
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
123124
#[derive(Debug, Clone, Copy)]
124125
pub struct DedupByKey<I, F, T> {
@@ -127,9 +128,13 @@ pub struct DedupByKey<I, F, T> {
127128
last: Option<T>,
128129
}
129130

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 }
133138
}
134139
}
135140

0 commit comments

Comments
 (0)