[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:
Taras Tsugrii 2023-07-31 16:58:04 -07:00
parent fb53384c94
commit 9eae73a5de

View File

@ -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));