Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsized ManuallyDrop #53033

Merged
merged 4 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
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> {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, no, this allows ManuallyDrop<Rc<T>> to coerce to ManuallyDrop<Rc<dyn Trait>> - do we need that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change even useful without a CoerceUnsized? AFAIK all types that do T: ?Sized also have a CoerceUnsized, or is that not the case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your test, the only needed CoercedUnsized impl is that for Box, pointers to ManuallyDrop can be coerced just because it's a ?Sized struct (you can test this with any such structs, there's no opt-in there).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done.


/// 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
Original file line number Diff line number Diff line change
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]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be less confusing if the array had two elements or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

drop(x);
}