From e7d9944e205dc59f1174b584cd8ed41deced9646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Wed, 1 Jul 2020 15:07:23 +0200 Subject: [PATCH] Add feature const_option --- src/libcore/lib.rs | 1 + src/libcore/option.rs | 12 ++++++++---- src/test/ui/consts/const-option.rs | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/consts/const-option.rs diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index aeb52bffbf24c..2aeb105bca2d1 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -85,6 +85,7 @@ #![feature(const_panic)] #![feature(const_fn_union)] #![feature(const_generics)] +#![feature(const_option)] #![feature(const_ptr_offset)] #![feature(const_ptr_offset_from)] #![cfg_attr(not(bootstrap), feature(const_raw_ptr_comparison))] diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 7aca6af3de6f3..7ba244214ae03 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -179,8 +179,9 @@ impl Option { /// [`Some`]: #variant.Some #[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"] #[inline] + #[rustc_const_unstable(feature = "const_option", issue = "67441")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_some(&self) -> bool { + pub const fn is_some(&self) -> bool { matches!(*self, Some(_)) } @@ -200,8 +201,9 @@ impl Option { #[must_use = "if you intended to assert that this doesn't have a value, consider \ `.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"] #[inline] + #[rustc_const_unstable(feature = "const_option", issue = "67441")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_none(&self) -> bool { + pub const fn is_none(&self) -> bool { !self.is_some() } @@ -259,8 +261,9 @@ impl Option { /// println!("still can print text: {:?}", text); /// ``` #[inline] + #[rustc_const_unstable(feature = "const_option", issue = "67441")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn as_ref(&self) -> Option<&T> { + pub const fn as_ref(&self) -> Option<&T> { match *self { Some(ref x) => Some(x), None => None, @@ -580,8 +583,9 @@ impl Option { /// assert_eq!(x.iter().next(), None); /// ``` #[inline] + #[rustc_const_unstable(feature = "const_option", issue = "67441")] #[stable(feature = "rust1", since = "1.0.0")] - pub fn iter(&self) -> Iter<'_, T> { + pub const fn iter(&self) -> Iter<'_, T> { Iter { inner: Item { opt: self.as_ref() } } } diff --git a/src/test/ui/consts/const-option.rs b/src/test/ui/consts/const-option.rs new file mode 100644 index 0000000000000..fbf20b9db6741 --- /dev/null +++ b/src/test/ui/consts/const-option.rs @@ -0,0 +1,14 @@ +// run-pass + +#![feature(const_option)] + +const X: Option = Some(32); +const Y: Option<&i32> = X.as_ref(); + +const IS_SOME: bool = X.is_some(); +const IS_NONE: bool = Y.is_none(); + +fn main() { + assert!(IS_SOME); + assert!(!IS_NONE) +}