Skip to content

Commit

Permalink
Auto merge of #53033 - RalfJung:manually_dro, r=SimonSapin
Browse files Browse the repository at this point in the history
unsized ManuallyDrop

I think this matches what @eddyb had in #52711 originally.

~~However, I have never added a `CoerceUnsized` before so I am not sure if I did this right. I copied the `unstable` attribute on the `impl` from elsewhere, but AFAIK it is useless because `impl`'s are insta-stable... so shouldn't this rather say "stable since 1.30"?~~

This is insta-stable and hence requires FCP, at least.

Fixes #47034
  • Loading branch information
bors committed Aug 14, 2018
2 parents a8763b5 + 5ee5a7e commit 5bb9239
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,8 @@ pub fn discriminant<T>(v: &T) -> Discriminant<T> {
#[stable(feature = "manually_drop", since = "1.20.0")]
#[lang = "manually_drop"]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ManuallyDrop<T> {
#[repr(transparent)]
pub struct ManuallyDrop<T: ?Sized> {
value: T,
}

Expand Down Expand Up @@ -990,7 +991,9 @@ impl<T> ManuallyDrop<T> {
pub fn into_inner(slot: ManuallyDrop<T>) -> T {
slot.value
}
}

impl<T: ?Sized> ManuallyDrop<T> {
/// Manually drops the contained value.
///
/// # Safety
Expand All @@ -1006,7 +1009,7 @@ impl<T> ManuallyDrop<T> {
}

#[stable(feature = "manually_drop", since = "1.20.0")]
impl<T> Deref for ManuallyDrop<T> {
impl<T: ?Sized> Deref for ManuallyDrop<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
Expand All @@ -1015,7 +1018,7 @@ impl<T> Deref for ManuallyDrop<T> {
}

#[stable(feature = "manually_drop", since = "1.20.0")]
impl<T> DerefMut for ManuallyDrop<T> {
impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
Expand Down
5 changes: 5 additions & 0 deletions src/libcore/tests/manually_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ fn smoke() {

let x = ManuallyDrop::new(TypeWithDrop);
drop(x);

// also test unsizing
let x : Box<ManuallyDrop<[TypeWithDrop]>> =
Box::new(ManuallyDrop::new([TypeWithDrop, TypeWithDrop]));
drop(x);
}

0 comments on commit 5bb9239

Please sign in to comment.