mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Rollup merge of #114313 - ttsugriy:sm-insert, r=petrochenkov
[rustc_data_structures] Simplify SortedMap::insert. It looks like current usage of `swap` is aimed at achieving what `std::mem::replace` does but more concisely and idiomatically.
This commit is contained in:
commit
a902550233
@ -49,12 +49,11 @@ impl<K: Ord, V> SortedMap<K, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
|
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
|
||||||
match self.lookup_index_for(&key) {
|
match self.lookup_index_for(&key) {
|
||||||
Ok(index) => {
|
Ok(index) => {
|
||||||
let slot = unsafe { self.data.get_unchecked_mut(index) };
|
let slot = unsafe { self.data.get_unchecked_mut(index) };
|
||||||
mem::swap(&mut slot.1, &mut value);
|
Some(mem::replace(&mut slot.1, value))
|
||||||
Some(value)
|
|
||||||
}
|
}
|
||||||
Err(index) => {
|
Err(index) => {
|
||||||
self.data.insert(index, (key, value));
|
self.data.insert(index, (key, value));
|
||||||
|
Loading…
Reference in New Issue
Block a user