Skip to content

Commit

Permalink
Rollup merge of rust-lang#76135 - CDirkx:const-option, r=dtolnay
Browse files Browse the repository at this point in the history
Stabilize some Option methods as const

Stabilize the following methods of `Option` as const:
 - `is_some`
 - `is_none`
 - `as_ref`

These methods are currently const under the unstable feature `const_option` (tracking issue: rust-lang#67441).
I believe these methods to be eligible for stabilization because of the stabilization of rust-lang#49146 (Allow if and match in constants) and the trivial implementations, see also:  [PR#75463](rust-lang#75463).

Related: rust-lang#76225
  • Loading branch information
RalfJung committed Sep 19, 2020
2 parents 47fc3ed + 04e4a39 commit af0594f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl<T> Option<T> {
/// ```
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
#[inline]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_some(&self) -> bool {
matches!(*self, Some(_))
Expand All @@ -195,7 +195,7 @@ 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")]
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_none(&self) -> bool {
!self.is_some()
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<T> Option<T> {
/// println!("still can print text: {:?}", text);
/// ```
#[inline]
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn as_ref(&self) -> Option<&T> {
match *self {
Expand Down
16 changes: 16 additions & 0 deletions library/core/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,19 @@ fn test_replace() {
assert_eq!(x, Some(3));
assert_eq!(old, None);
}

#[test]
fn option_const() {
// test that the methods of `Option` are usable in a const context

const OPTION: Option<usize> = Some(32);

const REF: Option<&usize> = OPTION.as_ref();
assert_eq!(REF, Some(&32));

const IS_SOME: bool = OPTION.is_some();
assert!(IS_SOME);

const IS_NONE: bool = OPTION.is_none();
assert!(!IS_NONE);
}
14 changes: 0 additions & 14 deletions src/test/ui/consts/const-option.rs

This file was deleted.

0 comments on commit af0594f

Please sign in to comment.