Skip to content

Commit f6cd31c

Browse files
authored
Rollup merge of #73930 - a1phyr:feature_const_option, r=dtolnay
Make some Option methods const Tracking issue: #67441 Constantify the following methods of `Option`: - `as_ref` - `is_some` - `is_none` - `iter` (not sure about this one, but it is possible, and will be useful when const traits are a thing) cc @rust-lang/wg-const-eval @rust-lang/libs
2 parents 3006ea3 + e7d9944 commit f6cd31c

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#![feature(const_panic)]
8484
#![feature(const_fn_union)]
8585
#![feature(const_generics)]
86+
#![feature(const_option)]
8687
#![feature(const_ptr_offset)]
8788
#![feature(const_ptr_offset_from)]
8889
#![feature(const_raw_ptr_comparison)]

src/libcore/option.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ impl<T> Option<T> {
179179
/// [`Some`]: #variant.Some
180180
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
181181
#[inline]
182+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
182183
#[stable(feature = "rust1", since = "1.0.0")]
183-
pub fn is_some(&self) -> bool {
184+
pub const fn is_some(&self) -> bool {
184185
matches!(*self, Some(_))
185186
}
186187

@@ -200,8 +201,9 @@ impl<T> Option<T> {
200201
#[must_use = "if you intended to assert that this doesn't have a value, consider \
201202
`.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"]
202203
#[inline]
204+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
203205
#[stable(feature = "rust1", since = "1.0.0")]
204-
pub fn is_none(&self) -> bool {
206+
pub const fn is_none(&self) -> bool {
205207
!self.is_some()
206208
}
207209

@@ -259,8 +261,9 @@ impl<T> Option<T> {
259261
/// println!("still can print text: {:?}", text);
260262
/// ```
261263
#[inline]
264+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
262265
#[stable(feature = "rust1", since = "1.0.0")]
263-
pub fn as_ref(&self) -> Option<&T> {
266+
pub const fn as_ref(&self) -> Option<&T> {
264267
match *self {
265268
Some(ref x) => Some(x),
266269
None => None,
@@ -580,8 +583,9 @@ impl<T> Option<T> {
580583
/// assert_eq!(x.iter().next(), None);
581584
/// ```
582585
#[inline]
586+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
583587
#[stable(feature = "rust1", since = "1.0.0")]
584-
pub fn iter(&self) -> Iter<'_, T> {
588+
pub const fn iter(&self) -> Iter<'_, T> {
585589
Iter { inner: Item { opt: self.as_ref() } }
586590
}
587591

src/test/ui/consts/const-option.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
3+
#![feature(const_option)]
4+
5+
const X: Option<i32> = Some(32);
6+
const Y: Option<&i32> = X.as_ref();
7+
8+
const IS_SOME: bool = X.is_some();
9+
const IS_NONE: bool = Y.is_none();
10+
11+
fn main() {
12+
assert!(IS_SOME);
13+
assert!(!IS_NONE)
14+
}

0 commit comments

Comments
 (0)