From 3a02e06002e3c34cd63c1237d0998414c56f7efc Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Wed, 5 Aug 2020 18:07:33 +0200 Subject: [PATCH] BTreeMap: purge innocent use of into_kv_mut --- library/alloc/src/collections/btree/map.rs | 6 +++--- library/alloc/src/collections/btree/node.rs | 22 +++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index e7d243bfcb0f7..8e800f48c69de 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -245,7 +245,7 @@ where fn replace(&mut self, key: K) -> Option { let root = Self::ensure_is_owned(&mut self.root); match search::search_tree::, K, (), K>(root.node_as_mut(), &key) { - Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)), + Found(handle) => Some(mem::replace(handle.into_key_mut(), key)), GoDown(handle) => { VacantEntry { key, handle, length: &mut self.length, _marker: PhantomData } .insert(()); @@ -811,7 +811,7 @@ impl BTreeMap { { let root_node = self.root.as_mut()?.node_as_mut(); match search::search_tree(root_node, key) { - Found(handle) => Some(handle.into_kv_mut().1), + Found(handle) => Some(handle.into_val_mut()), GoDown(_) => None, } } @@ -2748,7 +2748,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn into_mut(self) -> &'a mut V { - self.handle.into_kv_mut().1 + self.handle.into_val_mut() } /// Sets the value of the entry with the `OccupiedEntry`'s key, diff --git a/library/alloc/src/collections/btree/node.rs b/library/alloc/src/collections/btree/node.rs index f70869148d59f..d844433832471 100644 --- a/library/alloc/src/collections/btree/node.rs +++ b/library/alloc/src/collections/btree/node.rs @@ -1018,6 +1018,16 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeTyp } impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType>, marker::KV> { + pub fn into_key_mut(self) -> &'a mut K { + let keys = self.node.into_key_slice_mut(); + unsafe { keys.get_unchecked_mut(self.idx) } + } + + pub fn into_val_mut(self) -> &'a mut V { + let vals = self.node.into_val_slice_mut(); + unsafe { vals.get_unchecked_mut(self.idx) } + } + pub fn into_kv_mut(self) -> (&'a mut K, &'a mut V) { unsafe { let (keys, vals) = self.node.into_slices_mut(); @@ -1216,8 +1226,8 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: unsafe { let (k, v, edge) = self.reborrow_mut().left_edge().descend().pop(); - let k = mem::replace(self.reborrow_mut().into_kv_mut().0, k); - let v = mem::replace(self.reborrow_mut().into_kv_mut().1, v); + let k = mem::replace(self.kv_mut().0, k); + let v = mem::replace(self.kv_mut().1, v); match self.reborrow_mut().right_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push_front(k, v), @@ -1233,8 +1243,8 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: unsafe { let (k, v, edge) = self.reborrow_mut().right_edge().descend().pop_front(); - let k = mem::replace(self.reborrow_mut().into_kv_mut().0, k); - let v = mem::replace(self.reborrow_mut().into_kv_mut().1, v); + let k = mem::replace(self.kv_mut().0, k); + let v = mem::replace(self.kv_mut().1, v); match self.reborrow_mut().left_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push(k, v), @@ -1262,7 +1272,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let left_kv = left_node.reborrow_mut().into_kv_pointers_mut(); let right_kv = right_node.reborrow_mut().into_kv_pointers_mut(); let parent_kv = { - let kv = self.reborrow_mut().into_kv_mut(); + let kv = self.kv_mut(); (kv.0 as *mut K, kv.1 as *mut V) }; @@ -1319,7 +1329,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let left_kv = left_node.reborrow_mut().into_kv_pointers_mut(); let right_kv = right_node.reborrow_mut().into_kv_pointers_mut(); let parent_kv = { - let kv = self.reborrow_mut().into_kv_mut(); + let kv = self.kv_mut(); (kv.0 as *mut K, kv.1 as *mut V) };