Skip to content

Commit 2967e58

Browse files
authored
Rollup merge of #78728 - a1phyr:const_cell_into_inner, r=dtolnay
Constantify `UnsafeCell::into_inner` and related Tracking issue: #78729 This PR constantifies: - `UnsafeCell::into_inner` - `Cell::into_inner` - `RefCell::into_inner` - `Atomic*::into_inner` r? `````@dtolnay`````
2 parents a619e25 + 795bbfe commit 2967e58

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

library/core/src/cell.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ impl<T> Cell<T> {
406406
/// assert_eq!(five, 5);
407407
/// ```
408408
#[stable(feature = "move_cell", since = "1.17.0")]
409-
pub fn into_inner(self) -> T {
409+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
410+
pub const fn into_inner(self) -> T {
410411
self.value.into_inner()
411412
}
412413
}
@@ -668,12 +669,11 @@ impl<T> RefCell<T> {
668669
/// let five = c.into_inner();
669670
/// ```
670671
#[stable(feature = "rust1", since = "1.0.0")]
672+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
671673
#[inline]
672-
pub fn into_inner(self) -> T {
674+
pub const fn into_inner(self) -> T {
673675
// Since this function takes `self` (the `RefCell`) by value, the
674676
// compiler statically verifies that it is not currently borrowed.
675-
// Therefore the following assertion is just a `debug_assert!`.
676-
debug_assert!(self.borrow.get() == UNUSED);
677677
self.value.into_inner()
678678
}
679679

@@ -1682,7 +1682,8 @@ impl<T> UnsafeCell<T> {
16821682
/// ```
16831683
#[inline]
16841684
#[stable(feature = "rust1", since = "1.0.0")]
1685-
pub fn into_inner(self) -> T {
1685+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
1686+
pub const fn into_inner(self) -> T {
16861687
self.value
16871688
}
16881689
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#![feature(cfg_target_has_atomic)]
7171
#![feature(const_alloc_layout)]
7272
#![feature(const_discriminant)]
73+
#![feature(const_cell_into_inner)]
7374
#![feature(const_checked_int_methods)]
7475
#![feature(const_euclidean_int_methods)]
7576
#![feature(const_float_classify)]

library/core/src/sync/atomic.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ impl AtomicBool {
355355
/// ```
356356
#[inline]
357357
#[stable(feature = "atomic_access", since = "1.15.0")]
358-
pub fn into_inner(self) -> bool {
358+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
359+
pub const fn into_inner(self) -> bool {
359360
self.v.into_inner() != 0
360361
}
361362

@@ -931,7 +932,8 @@ impl<T> AtomicPtr<T> {
931932
/// ```
932933
#[inline]
933934
#[stable(feature = "atomic_access", since = "1.15.0")]
934-
pub fn into_inner(self) -> *mut T {
935+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
936+
pub const fn into_inner(self) -> *mut T {
935937
self.p.into_inner()
936938
}
937939

@@ -1452,7 +1454,8 @@ assert_eq!(some_var.into_inner(), 5);
14521454
```"),
14531455
#[inline]
14541456
#[$stable_access]
1455-
pub fn into_inner(self) -> $int_type {
1457+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
1458+
pub const fn into_inner(self) -> $int_type {
14561459
self.v.into_inner()
14571460
}
14581461
}

library/core/tests/cell.rs

+12
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,15 @@ fn refcell_format() {
422422
let msg = format!("{name} {}", &*what.borrow(), name = &*name.borrow());
423423
assert_eq!(msg, "rust rocks".to_string());
424424
}
425+
426+
#[allow(dead_code)]
427+
fn const_cells() {
428+
const UNSAFE_CELL: UnsafeCell<i32> = UnsafeCell::new(3);
429+
const _: i32 = UNSAFE_CELL.into_inner();
430+
431+
const REF_CELL: RefCell<i32> = RefCell::new(3);
432+
const _: i32 = REF_CELL.into_inner();
433+
434+
const CELL: Cell<i32> = Cell::new(3);
435+
const _: i32 = CELL.into_inner();
436+
}

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(box_syntax)]
1010
#![feature(cell_update)]
1111
#![feature(const_assume)]
12+
#![feature(const_cell_into_inner)]
1213
#![feature(core_intrinsics)]
1314
#![feature(core_private_bignum)]
1415
#![feature(core_private_diy_float)]

0 commit comments

Comments
 (0)