From d8951ee8a5984f1d7b2abedb5b03d6e18751fcf1 Mon Sep 17 00:00:00 2001 From: James Munns Date: Fri, 19 Oct 2018 18:12:43 +0200 Subject: [PATCH 1/3] Implement Copy and Clone for UnsafeCell Only implemented if T implements Copy and/or Clone --- src/libcore/cell.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index ec7d366c3f5ce..421b741a3a330 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1512,6 +1512,18 @@ impl UnsafeCell { } } +// TODO: correct stable attribute +#[stable(feature = "rust1", since = "1.0.0")] +impl Clone for UnsafeCell { + fn clone(&self) -> UnsafeCell { + UnsafeCell::new(self.value.clone()) + } +} + +// TODO: correct stable attribute +#[stable(feature = "rust1", since = "1.0.0")] +impl Copy for UnsafeCell {} + #[stable(feature = "unsafe_cell_default", since = "1.10.0")] impl Default for UnsafeCell { /// Creates an `UnsafeCell`, with the `Default` value for T. From 4676b10670caa926d9eab2586060068095d255ce Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 1 Nov 2018 13:03:11 +0100 Subject: [PATCH 2/3] Modify stability attributes so CI can build, only impl Clone if T is Copy --- src/libcore/cell.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 421b741a3a330..d88d8d1fc6cd2 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1512,16 +1512,14 @@ impl UnsafeCell { } } -// TODO: correct stable attribute -#[stable(feature = "rust1", since = "1.0.0")] -impl Clone for UnsafeCell { +#[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")] +impl Clone for UnsafeCell { fn clone(&self) -> UnsafeCell { UnsafeCell::new(self.value.clone()) } } -// TODO: correct stable attribute -#[stable(feature = "rust1", since = "1.0.0")] +#[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")] impl Copy for UnsafeCell {} #[stable(feature = "unsafe_cell_default", since = "1.10.0")] From 67bcac6665d21c686f500b79fe3efecb09913198 Mon Sep 17 00:00:00 2001 From: James Munns Date: Thu, 1 Nov 2018 13:18:14 +0100 Subject: [PATCH 3/3] Remove call to clone, and explain why --- src/libcore/cell.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index d88d8d1fc6cd2..f7f58a02437c5 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1515,7 +1515,10 @@ impl UnsafeCell { #[stable(feature = "unsafe_cell_copy_clone_conservative", since = "1.31.0")] impl Clone for UnsafeCell { fn clone(&self) -> UnsafeCell { - UnsafeCell::new(self.value.clone()) + // 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) } }