Skip to content

Commit 97b7170

Browse files
committed
const Cell methods
1 parent 779e19d commit 97b7170

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

library/core/src/cell.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252

253253
use crate::cmp::Ordering;
254254
use crate::fmt::{self, Debug, Display};
255-
use crate::marker::{PhantomData, Unsize};
255+
use crate::marker::{Destruct, PhantomData, Unsize};
256256
use crate::mem::{self, ManuallyDrop};
257257
use crate::ops::{self, CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
258258
use crate::panic::const_panic;
@@ -429,7 +429,11 @@ impl<T> Cell<T> {
429429
/// ```
430430
#[inline]
431431
#[stable(feature = "rust1", since = "1.0.0")]
432-
pub fn set(&self, val: T) {
432+
#[rustc_const_unstable(feature = "const_cell_traits", issue = "147787")]
433+
pub const fn set(&self, val: T)
434+
where
435+
T: [const] Destruct,
436+
{
433437
self.replace(val);
434438
}
435439

@@ -561,7 +565,12 @@ impl<T: Copy> Cell<T> {
561565
/// ```
562566
#[inline]
563567
#[stable(feature = "cell_update", since = "1.88.0")]
564-
pub fn update(&self, f: impl FnOnce(T) -> T) {
568+
#[rustc_const_unstable(feature = "const_cell_traits", issue = "147787")]
569+
pub const fn update(&self, f: impl [const] FnOnce(T) -> T)
570+
where
571+
// FIXME(const-hack): `Copy` should imply `const Destruct`
572+
T: [const] Destruct,
573+
{
565574
let old = self.get();
566575
self.set(f(old));
567576
}
@@ -654,7 +663,11 @@ impl<T: Default> Cell<T> {
654663
/// assert_eq!(c.into_inner(), 0);
655664
/// ```
656665
#[stable(feature = "move_cell", since = "1.17.0")]
657-
pub fn take(&self) -> T {
666+
#[rustc_const_unstable(feature = "const_cell_traits", issue = "147787")]
667+
pub const fn take(&self) -> T
668+
where
669+
T: [const] Default,
670+
{
658671
self.replace(Default::default())
659672
}
660673
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#![feature(char_internals)]
1717
#![feature(char_max_len)]
1818
#![feature(clone_to_uninit)]
19+
#![feature(const_cell_traits)]
1920
#![feature(const_cmp)]
2021
#![feature(const_convert)]
2122
#![feature(const_destruct)]

library/coretests/tests/ptr.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,38 +1049,37 @@ fn test_ptr_default() {
10491049
#[test]
10501050
fn test_const_drop_in_place() {
10511051
const COUNTER: usize = {
1052-
let mut counter = 0;
1053-
let counter_ptr = &raw mut counter;
1052+
use core::cell::Cell;
1053+
1054+
let counter = Cell::new(0);
10541055

10551056
// only exists to make `Drop` indirect impl
10561057
#[allow(dead_code)]
1057-
struct Test(Dropped);
1058+
struct Test<'a>(Dropped<'a>);
10581059

1059-
struct Dropped(*mut usize);
1060-
impl const Drop for Dropped {
1060+
struct Dropped<'a>(&'a Cell<usize>);
1061+
impl const Drop for Dropped<'_> {
10611062
fn drop(&mut self) {
1062-
unsafe {
1063-
*self.0 += 1;
1064-
}
1063+
self.0.set(self.0.get() + 1);
10651064
}
10661065
}
10671066

1068-
let mut one = ManuallyDrop::new(Test(Dropped(counter_ptr)));
1069-
let mut two = ManuallyDrop::new(Test(Dropped(counter_ptr)));
1070-
let mut three = ManuallyDrop::new(Test(Dropped(counter_ptr)));
1071-
assert!(counter == 0);
1067+
let mut one = ManuallyDrop::new(Test(Dropped(&counter)));
1068+
let mut two = ManuallyDrop::new(Test(Dropped(&counter)));
1069+
let mut three = ManuallyDrop::new(Test(Dropped(&counter)));
1070+
assert!(counter.get() == 0);
10721071
unsafe {
10731072
ManuallyDrop::drop(&mut one);
10741073
}
1075-
assert!(counter == 1);
1074+
assert!(counter.get() == 1);
10761075
unsafe {
10771076
ManuallyDrop::drop(&mut two);
10781077
}
1079-
assert!(counter == 2);
1078+
assert!(counter.get() == 2);
10801079
unsafe {
10811080
ManuallyDrop::drop(&mut three);
10821081
}
1083-
counter
1082+
counter.get()
10841083
};
10851084
assert_eq!(COUNTER, 3);
10861085
}

0 commit comments

Comments
 (0)