Skip to content

Commit

Permalink
Declare unsafe functions that can no longer handle shared roots
Browse files Browse the repository at this point in the history
  • Loading branch information
ssomers committed Jan 21, 2020
1 parent 1b800a5 commit 3e947ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {

/// 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()
}

Expand Down Expand Up @@ -521,10 +521,10 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {

impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, 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.
Expand All @@ -537,7 +537,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, 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())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/collections/btree/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3e947ef

Please sign in to comment.