From b3d2346230736b253d8f2a9df461901b7de67abd Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 3 Aug 2018 18:02:34 +0200 Subject: [PATCH 1/4] unsized ManuallyDrop --- src/libcore/mem.rs | 11 ++++++++--- src/libcore/tests/manually_drop.rs | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 8a74e7c6f1cc6..1eec4def1bbff 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -956,7 +956,7 @@ pub fn discriminant(v: &T) -> Discriminant { #[stable(feature = "manually_drop", since = "1.20.0")] #[lang = "manually_drop"] #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ManuallyDrop { +pub struct ManuallyDrop { value: T, } @@ -990,7 +990,9 @@ impl ManuallyDrop { pub fn into_inner(slot: ManuallyDrop) -> T { slot.value } +} +impl ManuallyDrop { /// Manually drops the contained value. /// /// # Safety @@ -1006,7 +1008,7 @@ impl ManuallyDrop { } #[stable(feature = "manually_drop", since = "1.20.0")] -impl Deref for ManuallyDrop { +impl Deref for ManuallyDrop { type Target = T; #[inline] fn deref(&self) -> &Self::Target { @@ -1015,13 +1017,16 @@ impl Deref for ManuallyDrop { } #[stable(feature = "manually_drop", since = "1.20.0")] -impl DerefMut for ManuallyDrop { +impl DerefMut for ManuallyDrop { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.value } } +#[unstable(feature = "coerce_unsized", issue = "27732")] +impl, U> CoerceUnsized> for ManuallyDrop {} + /// A pinned reference. /// /// A pinned reference is a lot like a mutable reference, except that it is not diff --git a/src/libcore/tests/manually_drop.rs b/src/libcore/tests/manually_drop.rs index 96bc9247da6e7..b8826ad408761 100644 --- a/src/libcore/tests/manually_drop.rs +++ b/src/libcore/tests/manually_drop.rs @@ -21,4 +21,8 @@ fn smoke() { let x = ManuallyDrop::new(TypeWithDrop); drop(x); + + // also test unsizing + let x : Box> = Box::new(ManuallyDrop::new([TypeWithDrop])); + drop(x); } From 3dcdb8a83e953bfa033349e07c0f62b4230ea514 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 6 Aug 2018 15:52:27 +0200 Subject: [PATCH 2/4] remove unnecessary CoerceUnsized impl --- src/libcore/mem.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 1eec4def1bbff..9efac3e8d4cb2 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1024,9 +1024,6 @@ impl DerefMut for ManuallyDrop { } } -#[unstable(feature = "coerce_unsized", issue = "27732")] -impl, U> CoerceUnsized> for ManuallyDrop {} - /// A pinned reference. /// /// A pinned reference is a lot like a mutable reference, except that it is not From d865adcf1eea0772be93a2b330073238b0803e17 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 6 Aug 2018 15:52:36 +0200 Subject: [PATCH 3/4] unconfuse @eddyb --- src/libcore/tests/manually_drop.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/tests/manually_drop.rs b/src/libcore/tests/manually_drop.rs index b8826ad408761..82dfb8d4c0b2a 100644 --- a/src/libcore/tests/manually_drop.rs +++ b/src/libcore/tests/manually_drop.rs @@ -23,6 +23,7 @@ fn smoke() { drop(x); // also test unsizing - let x : Box> = Box::new(ManuallyDrop::new([TypeWithDrop])); + let x : Box> = + Box::new(ManuallyDrop::new([TypeWithDrop, TypeWithDrop])); drop(x); } From 5ee5a7eb5568011b29a9fee16b00e2b576603da3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 9 Aug 2018 11:56:01 +0200 Subject: [PATCH 4/4] repr(transparent) --- src/libcore/mem.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 9efac3e8d4cb2..ea711c69393a4 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -956,6 +956,7 @@ pub fn discriminant(v: &T) -> Discriminant { #[stable(feature = "manually_drop", since = "1.20.0")] #[lang = "manually_drop"] #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[repr(transparent)] pub struct ManuallyDrop { value: T, }