Skip to content

Commit 9a12d72

Browse files
committed
Constantify UnsafeCell::into_inner and related
Also includes: - Cell::into_inner - RefCell::into_inner - Atomic*::into_inner
1 parent 5629309 commit 9a12d72

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 = "none")]
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 = "none")]
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 = "none")]
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
@@ -365,7 +365,8 @@ impl AtomicBool {
365365
/// ```
366366
#[inline]
367367
#[stable(feature = "atomic_access", since = "1.15.0")]
368-
pub fn into_inner(self) -> bool {
368+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "none")]
369+
pub const fn into_inner(self) -> bool {
369370
self.v.into_inner() != 0
370371
}
371372

@@ -941,7 +942,8 @@ impl<T> AtomicPtr<T> {
941942
/// ```
942943
#[inline]
943944
#[stable(feature = "atomic_access", since = "1.15.0")]
944-
pub fn into_inner(self) -> *mut T {
945+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "none")]
946+
pub const fn into_inner(self) -> *mut T {
945947
self.p.into_inner()
946948
}
947949

@@ -1462,7 +1464,8 @@ assert_eq!(some_var.into_inner(), 5);
14621464
```"),
14631465
#[inline]
14641466
#[$stable_access]
1465-
pub fn into_inner(self) -> $int_type {
1467+
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "none")]
1468+
pub const fn into_inner(self) -> $int_type {
14661469
self.v.into_inner()
14671470
}
14681471
}

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)