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

Implement Copy and Clone for UnsafeCell<T> #55207

Closed
Closed
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
8 changes: 3 additions & 5 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,16 +1512,14 @@ impl<T: ?Sized> UnsafeCell<T> {
}
}

// TODO: correct stable attribute
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Clone> Clone for UnsafeCell<T> {
#[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")]
impl<T: Clone + Copy> Clone for UnsafeCell<T> {
fn clone(&self) -> UnsafeCell<T> {
UnsafeCell::new(self.value.clone())
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't call clone(), that is still unknown code executing.

It should make a Copy instead.

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 there an explict way to call copy? or is this acceptable?

#[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")]
impl<T: Clone + Copy> Clone for UnsafeCell<T> {
    fn clone(&self) -> UnsafeCell<T> {
        UnsafeCell::new(self.value)
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

Yes that should do it.

Copy link
Member

Choose a reason for hiding this comment

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

Together with a comment why we are not calling clone().

Copy link
Member Author

Choose a reason for hiding this comment

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

@RalfJung impl'd in 67bcac6, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Since Self: Copy the body of this method can even be just *self, though this is equivalent.

}
}

// TODO: correct stable attribute
#[stable(feature = "rust1", since = "1.0.0")]
#[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")]
impl<T: Copy> Copy for UnsafeCell<T> {}

#[stable(feature = "unsafe_cell_default", since = "1.10.0")]
Expand Down