Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some Option methods const #73930

Merged
merged 1 commit into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
12 changes: 8 additions & 4 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ impl<T> Option<T> {
/// [`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(_))
}

Expand All @@ -200,8 +201,9 @@ impl<T> Option<T> {
#[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()
}

Expand Down Expand Up @@ -259,8 +261,9 @@ impl<T> Option<T> {
/// 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,
Expand Down Expand Up @@ -580,8 +583,9 @@ impl<T> Option<T> {
/// 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() } }
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// run-pass

#![feature(const_option)]

const X: Option<i32> = 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)
}