Skip to content

Commit 6f9e6c9

Browse files
authored
Rollup merge of #146785 - hkBst:btree-1, r=joboet
btree: safety comments for init and new
2 parents 02faee6 + e2de670 commit 6f9e6c9

File tree

1 file changed

+7
-1
lines changed
  • library/alloc/src/collections/btree

1 file changed

+7
-1
lines changed

library/alloc/src/collections/btree/node.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ struct LeafNode<K, V> {
6767

6868
impl<K, V> LeafNode<K, V> {
6969
/// Initializes a new `LeafNode` in-place.
70+
///
71+
/// # Safety
72+
///
73+
/// The caller must ensure that `this` points to a (possibly uninitialized) `LeafNode`
7074
unsafe fn init(this: *mut Self) {
7175
// As a general policy, we leave fields uninitialized if they can be, as this should
7276
// be both slightly faster and easier to track in Valgrind.
@@ -79,9 +83,11 @@ impl<K, V> LeafNode<K, V> {
7983

8084
/// Creates a new boxed `LeafNode`.
8185
fn new<A: Allocator + Clone>(alloc: A) -> Box<Self, A> {
86+
let mut leaf = Box::new_uninit_in(alloc);
8287
unsafe {
83-
let mut leaf = Box::new_uninit_in(alloc);
88+
// SAFETY: `leaf` points to a `LeafNode`
8489
LeafNode::init(leaf.as_mut_ptr());
90+
// SAFETY: `leaf` was just initialized
8591
leaf.assume_init()
8692
}
8793
}

0 commit comments

Comments
 (0)