Skip to content

Commit 3e947ef

Browse files
committed
Declare unsafe functions that can no longer handle shared roots
1 parent 1b800a5 commit 3e947ef

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/liballoc/collections/btree/node.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
386386

387387
/// Borrows a view into the keys stored in the node.
388388
/// The caller must ensure that the node is not the shared root.
389-
pub fn keys(&self) -> &[K] {
389+
pub unsafe fn keys(&self) -> &[K] {
390390
self.reborrow().into_key_slice()
391391
}
392392

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

522522
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
523523
/// The caller must ensure that the node is not the shared root.
524-
fn into_key_slice(self) -> &'a [K] {
524+
unsafe fn into_key_slice(self) -> &'a [K] {
525525
debug_assert!(!self.is_shared_root());
526526
// We cannot be the shared root, so `as_leaf` is okay.
527-
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len()) }
527+
slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len())
528528
}
529529

530530
/// The caller must ensure that the node is not the shared root.
@@ -537,7 +537,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
537537
/// The caller must ensure that the node is not the shared root.
538538
fn into_slices(self) -> (&'a [K], &'a [V]) {
539539
let k = unsafe { ptr::read(&self) };
540-
(k.into_key_slice(), self.into_val_slice())
540+
(unsafe { k.into_key_slice() }, self.into_val_slice())
541541
}
542542
}
543543

src/liballoc/collections/btree/search.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ where
6464
// Using `keys()` is fine here even if BorrowType is mutable, as all we return
6565
// is an index -- not a reference.
6666
let len = node.len();
67-
// Skip search for empty nodes because `keys()` does not work on shared roots.
6867
if len > 0 {
69-
for (i, k) in node.keys().iter().enumerate() {
68+
let keys = unsafe { node.keys() }; // safe because a non-empty node cannot be the shared root
69+
for (i, k) in keys.iter().enumerate() {
7070
match key.cmp(k.borrow()) {
7171
Ordering::Greater => {}
7272
Ordering::Equal => return (i, true),

0 commit comments

Comments
 (0)