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:
Matthias Krüger 2023-08-01 06:55:55 +02:00 committed by GitHub
commit a902550233
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,12 +49,11 @@ pub fn from_presorted_elements(elements: Vec<(K, 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));