Skip to content

Commit

Permalink
Rollup merge of #114313 - ttsugriy:sm-insert, r=petrochenkov
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
matthiaskrgr authored Aug 1, 2023
2 parents ff8b96f + 9eae73a commit a902550
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_data_structures/src/sorted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ impl<K: Ord, V> SortedMap<K, V> {
}

#[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) {
Ok(index) => {
let slot = unsafe { self.data.get_unchecked_mut(index) };
mem::swap(&mut slot.1, &mut value);
Some(value)
Some(mem::replace(&mut slot.1, value))
}
Err(index) => {
self.data.insert(index, (key, value));
Expand Down

0 comments on commit a902550

Please sign in to comment.