From 753dac16abd16411e1833b35ba82cabac6997cec Mon Sep 17 00:00:00 2001 From: inquisitivecrystal <22333129+inquisitivecrystal@users.noreply.github.com> Date: Tue, 31 Aug 2021 14:44:13 -0700 Subject: [PATCH 1/2] Stabilize `UnsafeCell::raw_get()` --- library/core/src/cell.rs | 5 ++--- library/std/src/lib.rs | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs index 85b43f4884760..2adf6a549e641 100644 --- a/library/core/src/cell.rs +++ b/library/core/src/cell.rs @@ -1921,7 +1921,7 @@ impl UnsafeCell { } /// Gets a mutable pointer to the wrapped value. - /// The difference to [`get`] is that this function accepts a raw pointer, + /// The difference from [`get`] is that this function accepts a raw pointer, /// which is useful to avoid the creation of temporary references. /// /// The result can be cast to a pointer of any kind. @@ -1937,7 +1937,6 @@ impl UnsafeCell { /// calling `get` would require creating a reference to uninitialized data: /// /// ``` - /// #![feature(unsafe_cell_raw_get)] /// use std::cell::UnsafeCell; /// use std::mem::MaybeUninit; /// @@ -1948,7 +1947,7 @@ impl UnsafeCell { /// assert_eq!(uc.into_inner(), 5); /// ``` #[inline(always)] - #[unstable(feature = "unsafe_cell_raw_get", issue = "66358")] + #[stable(feature = "unsafe_cell_raw_get", since = "1.56.0")] pub const fn raw_get(this: *const Self) -> *mut T { // We can just cast the pointer from `UnsafeCell` to `T` because of // #[repr(transparent)]. This exploits libstd's special status, there is diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 028a066b5a130..43c7ec5fad33a 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -331,7 +331,6 @@ #![feature(try_reserve)] #![feature(try_reserve_kind)] #![feature(unboxed_closures)] -#![feature(unsafe_cell_raw_get)] #![feature(unwrap_infallible)] #![feature(vec_into_raw_parts)] #![feature(vec_spare_capacity)] From 227e004d3fda91b72eef2b5e064dcbf3647dc68e Mon Sep 17 00:00:00 2001 From: inquisitivecrystal <22333129+inquisitivecrystal@users.noreply.github.com> Date: Tue, 31 Aug 2021 16:32:01 -0700 Subject: [PATCH 2/2] Add a few tests for `UnsafeCell` --- library/core/tests/cell.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/library/core/tests/cell.rs b/library/core/tests/cell.rs index 77517879dd250..85a006c5d5bef 100644 --- a/library/core/tests/cell.rs +++ b/library/core/tests/cell.rs @@ -2,6 +2,38 @@ use core::cell::*; use core::default::Default; use std::mem::drop; +#[test] +fn smoketest_unsafe_cell() { + let mut x = UnsafeCell::new(10); + let ref_mut = &mut x; + unsafe { + // The asserts are repeated in order to ensure that `get()` + // is non-mutating. + assert_eq!(*ref_mut.get(), 10); + assert_eq!(*ref_mut.get(), 10); + *ref_mut.get_mut() += 5; + assert_eq!(*ref_mut.get(), 15); + assert_eq!(*ref_mut.get(), 15); + assert_eq!(x.into_inner(), 15); + } +} + +#[test] +fn unsafe_cell_raw_get() { + let x = UnsafeCell::new(10); + let ptr = &x as *const UnsafeCell; + unsafe { + // The asserts are repeated in order to ensure that `raw_get()` + // is non-mutating. + assert_eq!(*UnsafeCell::raw_get(ptr), 10); + assert_eq!(*UnsafeCell::raw_get(ptr), 10); + *UnsafeCell::raw_get(ptr) += 5; + assert_eq!(*UnsafeCell::raw_get(ptr), 15); + assert_eq!(*UnsafeCell::raw_get(ptr), 15); + assert_eq!(x.into_inner(), 15); + } +} + #[test] fn smoketest_cell() { let x = Cell::new(10);