Skip to content

Commit d313529

Browse files
authored
Rollup merge of #88551 - inquisitivecrystal:unsafe_cell_raw_get, r=m-ou-se
Stabilize `UnsafeCell::raw_get()` This PR stabilizes the associated function `UnsafeCell::raw_get()`. The FCP has [already completed](#66358 (comment)). While there was some discussion about the naming after the close of the FCP, it looks like people have agreed on this name. Still, it would probably be best if a `libs-api` member had a look at this and stated whether more discussion is needed. While I was at it, I added some tests for `UnsafeCell`, because there were barely any. Closes #66358.
2 parents f436b6d + 227e004 commit d313529

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

library/core/src/cell.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@ impl<T: ?Sized> UnsafeCell<T> {
19211921
}
19221922

19231923
/// Gets a mutable pointer to the wrapped value.
1924-
/// The difference to [`get`] is that this function accepts a raw pointer,
1924+
/// The difference from [`get`] is that this function accepts a raw pointer,
19251925
/// which is useful to avoid the creation of temporary references.
19261926
///
19271927
/// The result can be cast to a pointer of any kind.
@@ -1937,7 +1937,6 @@ impl<T: ?Sized> UnsafeCell<T> {
19371937
/// calling `get` would require creating a reference to uninitialized data:
19381938
///
19391939
/// ```
1940-
/// #![feature(unsafe_cell_raw_get)]
19411940
/// use std::cell::UnsafeCell;
19421941
/// use std::mem::MaybeUninit;
19431942
///
@@ -1948,7 +1947,7 @@ impl<T: ?Sized> UnsafeCell<T> {
19481947
/// assert_eq!(uc.into_inner(), 5);
19491948
/// ```
19501949
#[inline(always)]
1951-
#[unstable(feature = "unsafe_cell_raw_get", issue = "66358")]
1950+
#[stable(feature = "unsafe_cell_raw_get", since = "1.56.0")]
19521951
pub const fn raw_get(this: *const Self) -> *mut T {
19531952
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
19541953
// #[repr(transparent)]. This exploits libstd's special status, there is

library/core/tests/cell.rs

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@ use core::cell::*;
22
use core::default::Default;
33
use std::mem::drop;
44

5+
#[test]
6+
fn smoketest_unsafe_cell() {
7+
let mut x = UnsafeCell::new(10);
8+
let ref_mut = &mut x;
9+
unsafe {
10+
// The asserts are repeated in order to ensure that `get()`
11+
// is non-mutating.
12+
assert_eq!(*ref_mut.get(), 10);
13+
assert_eq!(*ref_mut.get(), 10);
14+
*ref_mut.get_mut() += 5;
15+
assert_eq!(*ref_mut.get(), 15);
16+
assert_eq!(*ref_mut.get(), 15);
17+
assert_eq!(x.into_inner(), 15);
18+
}
19+
}
20+
21+
#[test]
22+
fn unsafe_cell_raw_get() {
23+
let x = UnsafeCell::new(10);
24+
let ptr = &x as *const UnsafeCell<i32>;
25+
unsafe {
26+
// The asserts are repeated in order to ensure that `raw_get()`
27+
// is non-mutating.
28+
assert_eq!(*UnsafeCell::raw_get(ptr), 10);
29+
assert_eq!(*UnsafeCell::raw_get(ptr), 10);
30+
*UnsafeCell::raw_get(ptr) += 5;
31+
assert_eq!(*UnsafeCell::raw_get(ptr), 15);
32+
assert_eq!(*UnsafeCell::raw_get(ptr), 15);
33+
assert_eq!(x.into_inner(), 15);
34+
}
35+
}
36+
537
#[test]
638
fn smoketest_cell() {
739
let x = Cell::new(10);

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@
331331
#![feature(try_reserve)]
332332
#![feature(try_reserve_kind)]
333333
#![feature(unboxed_closures)]
334-
#![feature(unsafe_cell_raw_get)]
335334
#![feature(unwrap_infallible)]
336335
#![feature(vec_into_raw_parts)]
337336
#![feature(vec_spare_capacity)]

0 commit comments

Comments
 (0)