Skip to content

Commit 10ddabc

Browse files
Lamblambinoo
Lamb
authored andcommitted
const fn for option copied, take & replace + tests
fix: move test that require mut to another Adding TODOs for Option::take and Option::copied TODO to FIXME + moving const stability under normal Moving const stability attr under normal stab attr move more rustc stability attributes
1 parent 757a65b commit 10ddabc

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#![feature(const_maybe_uninit_assume_init)]
9292
#![feature(const_option)]
9393
#![feature(const_pin)]
94+
#![feature(const_replace)]
9495
#![feature(const_ptr_offset)]
9596
#![feature(const_ptr_offset_from)]
9697
#![feature(const_ptr_read)]

library/core/src/option.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,8 @@ impl<T> Option<T> {
540540
/// ```
541541
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
542542
#[inline]
543-
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
544543
#[stable(feature = "rust1", since = "1.0.0")]
544+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
545545
pub const fn is_some(&self) -> bool {
546546
matches!(*self, Some(_))
547547
}
@@ -560,8 +560,8 @@ impl<T> Option<T> {
560560
#[must_use = "if you intended to assert that this doesn't have a value, consider \
561561
`.and_then(|_| panic!(\"`Option` had a value when expected `None`\"))` instead"]
562562
#[inline]
563-
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
564563
#[stable(feature = "rust1", since = "1.0.0")]
564+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
565565
pub const fn is_none(&self) -> bool {
566566
!self.is_some()
567567
}
@@ -1312,8 +1312,10 @@ impl<T> Option<T> {
13121312
/// ```
13131313
#[inline]
13141314
#[stable(feature = "rust1", since = "1.0.0")]
1315-
pub fn take(&mut self) -> Option<T> {
1316-
mem::take(self)
1315+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1316+
pub const fn take(&mut self) -> Option<T> {
1317+
// FIXME replace `mem::replace` by `mem::take` when the latter is const ready
1318+
mem::replace(self, None)
13171319
}
13181320

13191321
/// Replaces the actual value in the option by the value given in parameter,
@@ -1334,8 +1336,9 @@ impl<T> Option<T> {
13341336
/// assert_eq!(old, None);
13351337
/// ```
13361338
#[inline]
1339+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
13371340
#[stable(feature = "option_replace", since = "1.31.0")]
1338-
pub fn replace(&mut self, value: T) -> Option<T> {
1341+
pub const fn replace(&mut self, value: T) -> Option<T> {
13391342
mem::replace(self, Some(value))
13401343
}
13411344

@@ -1440,8 +1443,14 @@ impl<T: Copy> Option<&T> {
14401443
/// assert_eq!(copied, Some(12));
14411444
/// ```
14421445
#[stable(feature = "copied", since = "1.35.0")]
1443-
pub fn copied(self) -> Option<T> {
1444-
self.map(|&t| t)
1446+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1447+
pub const fn copied(self) -> Option<T> {
1448+
// FIXME: this implementation, which sidesteps using `Option::map` since it's not const
1449+
// ready yet, should be reverted when possible to avoid code repetition
1450+
match self {
1451+
Some(&v) => Some(v),
1452+
None => None,
1453+
}
14451454
}
14461455
}
14471456

library/core/tests/option.rs

+13
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ fn option_const() {
367367

368368
const IS_NONE: bool = OPTION.is_none();
369369
assert!(!IS_NONE);
370+
371+
const COPIED: Option<usize> = OPTION.as_ref().copied();
372+
assert_eq!(COPIED, OPTION);
373+
}
374+
375+
#[test]
376+
const fn option_const_mut() {
377+
// test that the methods of `Option` that take mutable references are usable in a const context
378+
379+
let mut option: Option<usize> = Some(32);
380+
381+
let _take = option.take();
382+
let _replace = option.replace(42);
370383
}
371384

372385
#[test]

0 commit comments

Comments
 (0)