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

Stabilize UnsafeCell::raw_get() #88551

Merged
merged 2 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ impl<T: ?Sized> UnsafeCell<T> {
}

/// 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.
Expand All @@ -1937,7 +1937,6 @@ impl<T: ?Sized> UnsafeCell<T> {
/// calling `get` would require creating a reference to uninitialized data:
///
/// ```
/// #![feature(unsafe_cell_raw_get)]
/// use std::cell::UnsafeCell;
/// use std::mem::MaybeUninit;
///
Expand All @@ -1948,7 +1947,7 @@ impl<T: ?Sized> UnsafeCell<T> {
/// 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<T>` to `T` because of
// #[repr(transparent)]. This exploits libstd's special status, there is
Expand Down
32 changes: 32 additions & 0 deletions library/core/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>;
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);
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down