diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-01-10 04:18:34 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-10 04:18:34 +0900 |
| commit | 78db333f9dd6786b156e2b3e4bec85f959d22606 (patch) | |
| tree | 9058a3e8dcb00399ee292fa91d1bf4f111675908 /src/liballoc/collections/btree/node.rs | |
| parent | 02ef0dcf692c3fbd822ba8f7859d3a827a5d9800 (diff) | |
| parent | 9b92bf83156fbe4892fd7a1aa186ce15cce3b770 (diff) | |
Rollup merge of #67725 - ssomers:into_key_slice_mut, r=RalfJung
Simplify into_key_slice_mut Remove a rare and tiny but superfluous run-time check from into_key_slice_mut. In #67459, I wrote that "`get_mut` [...] does visit `into_key_slice_mut`" and that was wrong. No function that operates on a map that (still) has a shared root ever dives into `into_key_slice_mut`. So it's more clear to remove the (previously existing, and always incomplete) code it has for dealing with shared roots, as well as a petty performance improvement for those using exotically aligned key types. ~~Also, some testing of the `range` function initially added to #67686 but hardly related.~~ r? @RalfJung
Diffstat (limited to 'src/liballoc/collections/btree/node.rs')
| -rw-r--r-- | src/liballoc/collections/btree/node.rs | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 260e51d635d..f40e0b0c304 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -397,6 +397,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> { /// 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 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> { } /// 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() } } @@ -589,20 +591,15 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> { unsafe { &mut *(self.root as *mut Root<K, V>) } } + /// The caller must ensure that the node is not the shared root. 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(), + ) } } |
