Skip to content

Commit 9ee846b

Browse files
committed
expose drop_in_place as ptr::drop_in_place
1 parent aca2057 commit 9ee846b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/liballoc/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
#![feature(unsize)]
9797
#![feature(core_slice_ext)]
9898
#![feature(core_str_ext)]
99+
#![feature(drop_in_place)]
100+
99101
#![cfg_attr(stage0, feature(alloc_system))]
100102
#![cfg_attr(not(stage0), feature(needs_allocator))]
101103

src/libcore/intrinsics.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,26 @@ extern "rust-intrinsic" {
195195

196196
pub fn size_of_val<T: ?Sized>(_: &T) -> usize;
197197
pub fn min_align_of_val<T: ?Sized>(_: &T) -> usize;
198-
pub fn drop_in_place<T: ?Sized>(_: *mut T);
198+
199+
/// Executes the destructor (if any) of the pointed-to value.
200+
///
201+
/// This has two usecases:
202+
///
203+
/// * It is *required* to use `drop_in_place` to drop unsized types like
204+
/// trait objects, because they can't be read out onto the stack and
205+
/// dropped normally.
206+
///
207+
/// * It is friendlier to the optimizer to do this over `ptr::read` when
208+
/// dropping manually allocated memory (e.g. when writing Box/Rc/Vec),
209+
/// as the compiler doesn't need to prove that it's sound to elide the
210+
/// copy.
211+
///
212+
/// # Undefined Behaviour
213+
///
214+
/// This has all the same safety problems as `ptr::read` with respect to
215+
/// invalid pointers, types, and double drops.
216+
#[unstable(feature = "drop_in_place", reason = "just exposed, needs FCP", issue = "27908")]
217+
pub fn drop_in_place<T: ?Sized>(to_drop: *mut T);
199218

200219
/// Gets a static string slice containing the name of a type.
201220
pub fn type_name<T: ?Sized>() -> &'static str;

src/libcore/ptr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub use intrinsics::copy;
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
pub use intrinsics::write_bytes;
4141

42+
pub use intrinsics::drop_in_place;
43+
4244
/// Creates a null raw pointer.
4345
///
4446
/// # Examples

0 commit comments

Comments
 (0)