Skip to content

Commit 02cfabe

Browse files
committed
Update documentation for drop_in_place()
1 parent 67ddb33 commit 02cfabe

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

library/core/src/ptr/mod.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -436,23 +436,41 @@ mod mut_ptr;
436436
///
437437
/// # Safety
438438
///
439-
/// Behavior is undefined if any of the following conditions are violated:
439+
/// Immediately upon executing, `drop_in_place` takes out a mutable borrow on the
440+
/// pointed-to-value. Effectively, this function is implemented like so:
441+
///
442+
/// ```
443+
/// # struct Foo { x: i32 }
444+
/// fn drop_in_place(to_drop: *mut Foo) {
445+
/// let mut value = &mut *to_drop;
446+
/// // ... drop the fields of `value` ...
447+
/// }
448+
/// ```
449+
///
450+
/// This implies that the behavior is undefined if any of the following
451+
/// conditions are violated:
440452
///
441453
/// * `to_drop` must be [valid] for both reads and writes.
442454
///
443-
/// * `to_drop` must be properly aligned.
455+
/// * `to_drop` must be properly aligned, even if T has size 0.
456+
///
457+
/// * `to_drop` must be nonnull, even if T has size 0.
458+
///
459+
/// * The value `to_drop` points to must be valid for dropping, which may mean
460+
/// it must uphold additional invariants. These invariants depend on the type
461+
/// of the value being dropped. For instance, when dropping a Box, the box's
462+
/// pointer to the heap must be valid.
444463
///
445-
/// * The value `to_drop` points to must be valid for dropping, which may mean it must uphold
446-
/// additional invariants - this is type-dependent.
464+
/// * While `drop_in_place` is executing, the only way to access parts of
465+
/// `to_drop` is through the `&mut self` references supplied to the
466+
/// `Drop::drop` methods that `drop_in_place` invokes.
447467
///
448468
/// Additionally, if `T` is not [`Copy`], using the pointed-to value after
449469
/// calling `drop_in_place` can cause undefined behavior. Note that `*to_drop =
450470
/// foo` counts as a use because it will cause the value to be dropped
451471
/// again. [`write()`] can be used to overwrite data without causing it to be
452472
/// dropped.
453473
///
454-
/// Note that even if `T` has size `0`, the pointer must be non-null and properly aligned.
455-
///
456474
/// [valid]: self#safety
457475
///
458476
/// # Examples

0 commit comments

Comments
 (0)