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`````
This commit is contained in:
Mara Bos 2020-11-08 13:36:14 +01:00 committed by GitHub
commit 2967e58be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 8 deletions

View File

@ -406,7 +406,8 @@ pub fn replace(&self, val: T) -> T {
/// assert_eq!(five, 5);
/// ```
#[stable(feature = "move_cell", since = "1.17.0")]
pub fn into_inner(self) -> T {
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
pub const fn into_inner(self) -> T {
self.value.into_inner()
}
}
@ -668,12 +669,11 @@ pub const fn new(value: T) -> RefCell<T> {
/// let five = c.into_inner();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[inline]
pub fn into_inner(self) -> T {
pub const fn into_inner(self) -> T {
// Since this function takes `self` (the `RefCell`) by value, the
// compiler statically verifies that it is not currently borrowed.
// Therefore the following assertion is just a `debug_assert!`.
debug_assert!(self.borrow.get() == UNUSED);
self.value.into_inner()
}
@ -1682,7 +1682,8 @@ pub const fn new(value: T) -> UnsafeCell<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_inner(self) -> T {
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
pub const fn into_inner(self) -> T {
self.value
}
}

View File

@ -70,6 +70,7 @@
#![feature(cfg_target_has_atomic)]
#![feature(const_alloc_layout)]
#![feature(const_discriminant)]
#![feature(const_cell_into_inner)]
#![feature(const_checked_int_methods)]
#![feature(const_euclidean_int_methods)]
#![feature(const_float_classify)]

View File

@ -355,7 +355,8 @@ pub fn from_mut(v: &mut bool) -> &Self {
/// ```
#[inline]
#[stable(feature = "atomic_access", since = "1.15.0")]
pub fn into_inner(self) -> bool {
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
pub const fn into_inner(self) -> bool {
self.v.into_inner() != 0
}
@ -931,7 +932,8 @@ pub fn from_mut(v: &mut *mut T) -> &Self {
/// ```
#[inline]
#[stable(feature = "atomic_access", since = "1.15.0")]
pub fn into_inner(self) -> *mut T {
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
pub const fn into_inner(self) -> *mut T {
self.p.into_inner()
}
@ -1452,7 +1454,8 @@ pub fn from_mut(v: &mut $int_type) -> &Self {
```"),
#[inline]
#[$stable_access]
pub fn into_inner(self) -> $int_type {
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
pub const fn into_inner(self) -> $int_type {
self.v.into_inner()
}
}

View File

@ -422,3 +422,15 @@ fn refcell_format() {
let msg = format!("{name} {}", &*what.borrow(), name = &*name.borrow());
assert_eq!(msg, "rust rocks".to_string());
}
#[allow(dead_code)]
fn const_cells() {
const UNSAFE_CELL: UnsafeCell<i32> = UnsafeCell::new(3);
const _: i32 = UNSAFE_CELL.into_inner();
const REF_CELL: RefCell<i32> = RefCell::new(3);
const _: i32 = REF_CELL.into_inner();
const CELL: Cell<i32> = Cell::new(3);
const _: i32 = CELL.into_inner();
}

View File

@ -9,6 +9,7 @@
#![feature(box_syntax)]
#![feature(cell_update)]
#![feature(const_assume)]
#![feature(const_cell_into_inner)]
#![feature(core_intrinsics)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]