Skip to content

Commit 6cd757a

Browse files
committed
Make use of Option::unwrap_unchecked and prevent user from contructing ByPartialEq
1 parent 5dec9ce commit 6cd757a

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
iter::{InPlaceIterable, SourceIter},
3-
mem::swap,
4-
};
1+
use crate::iter::{InPlaceIterable, SourceIter};
52

63
/// A wrapper type around a key function.
74
///
@@ -61,6 +58,7 @@ where
6158
/// [`Iterator::dedup`]: Iterator::dedup
6259
#[unstable(feature = "iter_dedup", reason = "recently added", issue = "83748")]
6360
#[derive(Debug, Clone, Copy)]
61+
#[non_exhaustive]
6462
pub struct ByPartialEq;
6563

6664
impl ByPartialEq {
@@ -131,9 +129,9 @@ where
131129
self.last = Some(self.inner.next())
132130
}
133131

134-
let last = self.last.as_mut().unwrap();
132+
// Safety: the above if statement ensures that `self.last` is always `Some`
133+
let last = unsafe { self.last.as_mut().unwrap_unchecked() };
135134
let last_item = last.as_ref()?;
136-
137135
let mut next = loop {
138136
let curr = self.inner.next();
139137
if let Some(curr_item) = &curr {
@@ -145,7 +143,7 @@ where
145143
}
146144
};
147145

148-
swap(last, &mut next);
146+
crate::mem::swap(last, &mut next);
149147
next
150148
}
151149

0 commit comments

Comments
 (0)