Skip to content

Commit 9a906dd

Browse files
committed
Refactor Dedup::next as suggested by @camsteffen
1 parent fcbff04 commit 9a906dd

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,11 @@ where
125125

126126
#[inline]
127127
fn next(&mut self) -> Option<Self::Item> {
128-
if self.last.is_none() {
129-
self.last = Some(self.inner.next())
130-
}
131-
132-
// Safety: the above if statement ensures that `self.last` is always `Some`
133-
let last = unsafe { self.last.as_mut().unwrap_unchecked() };
128+
let Self { inner, last, same_bucket } = self;
129+
let last = last.get_or_insert_with(|| inner.next());
134130
let last_item = last.as_ref()?;
135-
let mut next = loop {
136-
let curr = self.inner.next();
137-
if let Some(curr_item) = &curr {
138-
if !(self.same_bucket)(last_item, curr_item) {
139-
break curr;
140-
}
141-
} else {
142-
break None;
143-
}
144-
};
145-
146-
crate::mem::swap(last, &mut next);
147-
next
131+
let next = inner.find(|next_item| !(same_bucket)(last_item, next_item));
132+
crate::mem::replace(last, next)
148133
}
149134

150135
#[inline]

0 commit comments

Comments
 (0)