Skip to content

Commit 2836e55

Browse files
pcwaltonerikdesjardins
authored andcommitted
Update documentation for drop_in_place()
1 parent 21b8815 commit 2836e55

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

library/core/src/ptr/mod.rs

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

0 commit comments

Comments
 (0)