Skip to content

Commit

Permalink
unsized ManuallyDrop
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 3, 2018
1 parent 88e0ff1 commit b3d2346
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/libcore/mem.rs
Expand Up @@ -956,7 +956,7 @@ 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> {
pub struct ManuallyDrop<T: ?Sized> {
value: T,
}

Expand Down Expand Up @@ -990,7 +990,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 +1008,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,13 +1017,16 @@ 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
}
}

#[unstable(feature = "coerce_unsized", issue = "27732")]
impl<T: CoerceUnsized<U>, U> CoerceUnsized<ManuallyDrop<U>> for ManuallyDrop<T> {}

/// A pinned reference.
///
/// A pinned reference is a lot like a mutable reference, except that it is not
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/tests/manually_drop.rs
Expand Up @@ -21,4 +21,8 @@ fn smoke() {

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

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

0 comments on commit b3d2346

Please sign in to comment.