Skip to content

Commit 56020e7

Browse files
committed
const Cell methods
1 parent 779e19d commit 56020e7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
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
}

0 commit comments

Comments
 (0)