mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 20:45:45 +03:00
Simplify into_key_slice_mut and document bits and bobs
This commit is contained in:
@@ -397,6 +397,7 @@ pub fn keys(&self) -> &[K] {
|
||||
|
||||
/// Borrows a view into the values stored in the node.
|
||||
/// The caller must ensure that the node is not the shared root.
|
||||
/// This function is not public, so doesn't have to support shared roots like `keys` does.
|
||||
fn vals(&self) -> &[V] {
|
||||
self.reborrow().into_val_slice()
|
||||
}
|
||||
@@ -514,6 +515,7 @@ fn as_leaf_mut(&mut self) -> *mut LeafNode<K, V> {
|
||||
}
|
||||
|
||||
/// The caller must ensure that the node is not the shared root.
|
||||
/// This function is not public, so doesn't have to support shared roots like `keys` does.
|
||||
fn keys_mut(&mut self) -> &mut [K] {
|
||||
unsafe { self.reborrow_mut().into_key_slice_mut() }
|
||||
}
|
||||
@@ -590,19 +592,13 @@ pub fn into_root_mut(self) -> &'a mut Root<K, V> {
|
||||
}
|
||||
|
||||
fn into_key_slice_mut(mut self) -> &'a mut [K] {
|
||||
// Same as for `into_key_slice` above, we try to avoid a run-time check.
|
||||
if (mem::align_of::<NodeHeader<K, V, K>>() > mem::align_of::<NodeHeader<K, V>>()
|
||||
|| mem::size_of::<NodeHeader<K, V, K>>() != mem::size_of::<NodeHeader<K, V>>())
|
||||
&& self.is_shared_root()
|
||||
{
|
||||
&mut []
|
||||
} else {
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(
|
||||
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
|
||||
self.len(),
|
||||
)
|
||||
}
|
||||
debug_assert!(!self.is_shared_root());
|
||||
// We cannot be the shared root, so `as_leaf_mut` is okay.
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(
|
||||
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
|
||||
self.len(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,11 @@ pub fn search_node<BorrowType, K, V, Type, Q: ?Sized>(
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the index in the node at which the key (or an equivalent) exists
|
||||
/// or could exist, and whether it exists in the node itself. If it doesn't
|
||||
/// exist in the node itself, it may exist in the subtree with that index
|
||||
/// (if the node has subtrees). If the key doesn't exist in node or subtree,
|
||||
/// the returned index is the position or subtree to insert at.
|
||||
pub fn search_linear<BorrowType, K, V, Type, Q: ?Sized>(
|
||||
node: &NodeRef<BorrowType, K, V, Type>,
|
||||
key: &Q,
|
||||
@@ -54,6 +59,12 @@ pub fn search_linear<BorrowType, K, V, Type, Q: ?Sized>(
|
||||
Q: Ord,
|
||||
K: Borrow<Q>,
|
||||
{
|
||||
// This function is defined over all borrow types (immutable, mutable, owned),
|
||||
// and may be called on the shared root in each case.
|
||||
// Crucially, we use `keys()` here, i.e., we work with immutable data.
|
||||
// We do not need to make `keys_mut()` public and require support for the shared root.
|
||||
// Using `keys()` is fine here even if BorrowType is mutable, as all we return
|
||||
// is an index -- not a reference.
|
||||
for (i, k) in node.keys().iter().enumerate() {
|
||||
match key.cmp(k.borrow()) {
|
||||
Ordering::Greater => {}
|
||||
|
||||
Reference in New Issue
Block a user