Skip to content

Commit 8c40b9c

Browse files
authored
Rollup merge of #147788 - clarfonthey:const-cell, r=oli-obk
const Cell methods Tracking: #147787 r? `@oli-obk`
2 parents aa64f56 + 31a2c56 commit 8c40b9c

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
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/manually_drop.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,41 @@ fn smoke() {
2727
drop(x);
2828
drop(y);
2929
}
30+
31+
#[test]
32+
fn const_drop_in_place() {
33+
const COUNTER: usize = {
34+
use core::cell::Cell;
35+
36+
let counter = Cell::new(0);
37+
38+
// only exists to make `Drop` indirect impl
39+
#[allow(dead_code)]
40+
struct Test<'a>(Dropped<'a>);
41+
42+
struct Dropped<'a>(&'a Cell<usize>);
43+
impl const Drop for Dropped<'_> {
44+
fn drop(&mut self) {
45+
self.0.set(self.0.get() + 1);
46+
}
47+
}
48+
49+
let mut one = ManuallyDrop::new(Test(Dropped(&counter)));
50+
let mut two = ManuallyDrop::new(Test(Dropped(&counter)));
51+
let mut three = ManuallyDrop::new(Test(Dropped(&counter)));
52+
assert!(counter.get() == 0);
53+
unsafe {
54+
ManuallyDrop::drop(&mut one);
55+
}
56+
assert!(counter.get() == 1);
57+
unsafe {
58+
ManuallyDrop::drop(&mut two);
59+
}
60+
assert!(counter.get() == 2);
61+
unsafe {
62+
ManuallyDrop::drop(&mut three);
63+
}
64+
counter.get()
65+
};
66+
assert_eq!(COUNTER, 3);
67+
}

library/coretests/tests/ptr.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::cell::RefCell;
22
use core::marker::Freeze;
3-
use core::mem::{ManuallyDrop, MaybeUninit};
3+
use core::mem::MaybeUninit;
44
use core::num::NonZero;
55
use core::ptr;
66
use core::ptr::*;
@@ -1045,42 +1045,3 @@ fn test_ptr_default() {
10451045
let default = PtrMutDefaultTest::default();
10461046
assert!(default.ptr.is_null());
10471047
}
1048-
1049-
#[test]
1050-
fn test_const_drop_in_place() {
1051-
const COUNTER: usize = {
1052-
let mut counter = 0;
1053-
let counter_ptr = &raw mut counter;
1054-
1055-
// only exists to make `Drop` indirect impl
1056-
#[allow(dead_code)]
1057-
struct Test(Dropped);
1058-
1059-
struct Dropped(*mut usize);
1060-
impl const Drop for Dropped {
1061-
fn drop(&mut self) {
1062-
unsafe {
1063-
*self.0 += 1;
1064-
}
1065-
}
1066-
}
1067-
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);
1072-
unsafe {
1073-
ManuallyDrop::drop(&mut one);
1074-
}
1075-
assert!(counter == 1);
1076-
unsafe {
1077-
ManuallyDrop::drop(&mut two);
1078-
}
1079-
assert!(counter == 2);
1080-
unsafe {
1081-
ManuallyDrop::drop(&mut three);
1082-
}
1083-
counter
1084-
};
1085-
assert_eq!(COUNTER, 3);
1086-
}

0 commit comments

Comments
 (0)