-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Simplify into_key_slice_mut #67725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify into_key_slice_mut #67725
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We? I'd prefer something more objective like the slice or whatever it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't actually write that comment, and think it's somewhat superfluous, but please come up with a concrete suggestion here, like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would go with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that's any better, on the contrary, it comes across as poor grammar and capitalization to me. Also, there are already at least 3 comments of this "we = self" style in the file, so at least one author and one reviewer didn't object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The style of the public API documentation of Option, Vector, BTreeSet, leads to (in unscientifically determined order of popularity):
If there are multiple instances involved, we explicitly use |
||
unsafe { | ||
slice::from_raw_parts_mut( | ||
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys), | ||
self.len(), | ||
) | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.