about summary refs log tree commit diff
path: root/library
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-09-19 22:53:56 +0200
committerGitHub <noreply@github.com>2025-09-19 22:53:56 +0200
commit6f9e6c9935a77e4ff2a732255b20b5270ef2a36d (patch)
treee01cc4c6a824da04f980c52dbc853d85903f668b /library
parent02faee604455dd57d92d2361c9a25eabbeb60d33 (diff)
parente2de670558111dc6ce46d764aed6cb3dc4222794 (diff)
downloadrust-6f9e6c9935a77e4ff2a732255b20b5270ef2a36d.tar.gz
rust-6f9e6c9935a77e4ff2a732255b20b5270ef2a36d.zip
Rollup merge of #146785 - hkBst:btree-1, r=joboet
btree: safety comments for init and new
Diffstat (limited to 'library')
-rw-r--r--library/alloc/src/collections/btree/node.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/library/alloc/src/collections/btree/node.rs b/library/alloc/src/collections/btree/node.rs
index 37f784a322c..b233e1740b7 100644
--- a/library/alloc/src/collections/btree/node.rs
+++ b/library/alloc/src/collections/btree/node.rs
@@ -67,6 +67,10 @@ struct LeafNode<K, V> {
 
 impl<K, V> LeafNode<K, V> {
     /// Initializes a new `LeafNode` in-place.
+    ///
+    /// # Safety
+    ///
+    /// The caller must ensure that `this` points to a (possibly uninitialized) `LeafNode`
     unsafe fn init(this: *mut Self) {
         // As a general policy, we leave fields uninitialized if they can be, as this should
         // be both slightly faster and easier to track in Valgrind.
@@ -79,9 +83,11 @@ impl<K, V> LeafNode<K, V> {
 
     /// Creates a new boxed `LeafNode`.
     fn new<A: Allocator + Clone>(alloc: A) -> Box<Self, A> {
+        let mut leaf = Box::new_uninit_in(alloc);
         unsafe {
-            let mut leaf = Box::new_uninit_in(alloc);
+            // SAFETY: `leaf` points to a `LeafNode`
             LeafNode::init(leaf.as_mut_ptr());
+            // SAFETY: `leaf` was just initialized
             leaf.assume_init()
         }
     }