Skip to content

Commit fcd199d

Browse files
authored
Rollup merge of rust-lang#131281 - RalfJung:const-cell, r=Amanieu
make Cell unstably const Now that we can do interior mutability in `const`, most of the Cell API can be `const fn`. :) The main exception is `set`, because it drops the old value. So from const context one has to use `replace`, which delegates the responsibility for dropping to the caller. Tracking issue: rust-lang#131283 `as_array_of_cells` is itself still unstable to I added the const-ness to the feature gate for that function and not to `const_cell`, Cc rust-lang#88248. r? libs-api
2 parents 460459d + 2ed6282 commit fcd199d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

core/src/cell.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,9 @@ impl<T> Cell<T> {
494494
/// ```
495495
#[inline]
496496
#[stable(feature = "move_cell", since = "1.17.0")]
497+
#[rustc_const_unstable(feature = "const_cell", issue = "131283")]
497498
#[rustc_confusables("swap")]
498-
pub fn replace(&self, val: T) -> T {
499+
pub const fn replace(&self, val: T) -> T {
499500
// SAFETY: This can cause data races if called from a separate thread,
500501
// but `Cell` is `!Sync` so this won't happen.
501502
mem::replace(unsafe { &mut *self.value.get() }, val)
@@ -535,7 +536,8 @@ impl<T: Copy> Cell<T> {
535536
/// ```
536537
#[inline]
537538
#[stable(feature = "rust1", since = "1.0.0")]
538-
pub fn get(&self) -> T {
539+
#[rustc_const_unstable(feature = "const_cell", issue = "131283")]
540+
pub const fn get(&self) -> T {
539541
// SAFETY: This can cause data races if called from a separate thread,
540542
// but `Cell` is `!Sync` so this won't happen.
541543
unsafe { *self.value.get() }
@@ -613,7 +615,8 @@ impl<T: ?Sized> Cell<T> {
613615
/// ```
614616
#[inline]
615617
#[stable(feature = "cell_get_mut", since = "1.11.0")]
616-
pub fn get_mut(&mut self) -> &mut T {
618+
#[rustc_const_unstable(feature = "const_cell", issue = "131283")]
619+
pub const fn get_mut(&mut self) -> &mut T {
617620
self.value.get_mut()
618621
}
619622

@@ -632,7 +635,8 @@ impl<T: ?Sized> Cell<T> {
632635
/// ```
633636
#[inline]
634637
#[stable(feature = "as_cell", since = "1.37.0")]
635-
pub fn from_mut(t: &mut T) -> &Cell<T> {
638+
#[rustc_const_unstable(feature = "const_cell", issue = "131283")]
639+
pub const fn from_mut(t: &mut T) -> &Cell<T> {
636640
// SAFETY: `&mut` ensures unique access.
637641
unsafe { &*(t as *mut T as *const Cell<T>) }
638642
}
@@ -686,7 +690,8 @@ impl<T> Cell<[T]> {
686690
/// assert_eq!(slice_cell.len(), 3);
687691
/// ```
688692
#[stable(feature = "as_cell", since = "1.37.0")]
689-
pub fn as_slice_of_cells(&self) -> &[Cell<T>] {
693+
#[rustc_const_unstable(feature = "const_cell", issue = "131283")]
694+
pub const fn as_slice_of_cells(&self) -> &[Cell<T>] {
690695
// SAFETY: `Cell<T>` has the same memory layout as `T`.
691696
unsafe { &*(self as *const Cell<[T]> as *const [Cell<T>]) }
692697
}
@@ -706,7 +711,8 @@ impl<T, const N: usize> Cell<[T; N]> {
706711
/// let array_cell: &[Cell<i32>; 3] = cell_array.as_array_of_cells();
707712
/// ```
708713
#[unstable(feature = "as_array_of_cells", issue = "88248")]
709-
pub fn as_array_of_cells(&self) -> &[Cell<T>; N] {
714+
#[rustc_const_unstable(feature = "as_array_of_cells", issue = "88248")]
715+
pub const fn as_array_of_cells(&self) -> &[Cell<T>; N] {
710716
// SAFETY: `Cell<T>` has the same memory layout as `T`.
711717
unsafe { &*(self as *const Cell<[T; N]> as *const [Cell<T>; N]) }
712718
}

0 commit comments

Comments
 (0)