From 3e947ef031ae05c4ed56bbd6b3380bef644923f1 Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Tue, 21 Jan 2020 16:12:19 +0100 Subject: [PATCH] Declare unsafe functions that can no longer handle shared roots --- src/liballoc/collections/btree/node.rs | 8 ++++---- src/liballoc/collections/btree/search.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 37501a51e16e3..d9cdebb4f7354 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -386,7 +386,7 @@ impl NodeRef { /// Borrows a view into the keys stored in the node. /// The caller must ensure that the node is not the shared root. - pub fn keys(&self) -> &[K] { + pub unsafe fn keys(&self) -> &[K] { self.reborrow().into_key_slice() } @@ -521,10 +521,10 @@ impl<'a, K, V, Type> NodeRef, K, V, Type> { impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { /// The caller must ensure that the node is not the shared root. - fn into_key_slice(self) -> &'a [K] { + unsafe fn into_key_slice(self) -> &'a [K] { debug_assert!(!self.is_shared_root()); // We cannot be the shared root, so `as_leaf` is okay. - unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len()) } + slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len()) } /// The caller must ensure that the node is not the shared root. @@ -537,7 +537,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { /// The caller must ensure that the node is not the shared root. fn into_slices(self) -> (&'a [K], &'a [V]) { let k = unsafe { ptr::read(&self) }; - (k.into_key_slice(), self.into_val_slice()) + (unsafe { k.into_key_slice() }, self.into_val_slice()) } } diff --git a/src/liballoc/collections/btree/search.rs b/src/liballoc/collections/btree/search.rs index 25e206f4f73db..579624cdd2b6a 100644 --- a/src/liballoc/collections/btree/search.rs +++ b/src/liballoc/collections/btree/search.rs @@ -64,9 +64,9 @@ where // Using `keys()` is fine here even if BorrowType is mutable, as all we return // is an index -- not a reference. let len = node.len(); - // Skip search for empty nodes because `keys()` does not work on shared roots. if len > 0 { - for (i, k) in node.keys().iter().enumerate() { + let keys = unsafe { node.keys() }; // safe because a non-empty node cannot be the shared root + for (i, k) in keys.iter().enumerate() { match key.cmp(k.borrow()) { Ordering::Greater => {} Ordering::Equal => return (i, true),