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 all commits
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
13 changes: 13 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,19 @@ impl<T: ?Sized> 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> {
// NOTE: we are explicitly not calling `.clone()` here, to enforce the
// use of Copy, rather than relying on what could be a custom impl
// of Clone
UnsafeCell::new(self.value)
}
}

#[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")]
impl<T: Default> Default for UnsafeCell<T> {
/// Creates an `UnsafeCell`, with the `Default` value for T.
Expand Down