Skip to content

Commit

Permalink
Stabilize some Result methods as const
Browse files Browse the repository at this point in the history
Stabilize the following methods of `Result` as const:
 - `is_ok`
 - `is_err`
 - `as_ref`

Possible because of stabilization of #49146 (Allow if and match in constants).
  • Loading branch information
CDirkx committed Aug 31, 2020
1 parent 36b0d7e commit 518f1cc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 0 additions & 1 deletion library/core/src/lib.rs
Expand Up @@ -87,7 +87,6 @@
#![feature(const_ptr_offset)]
#![feature(const_ptr_offset_from)]
#![feature(const_raw_ptr_comparison)]
#![feature(const_result)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
#![feature(const_size_of_val)]
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/result.rs
Expand Up @@ -273,7 +273,7 @@ impl<T, E> Result<T, E> {
/// assert_eq!(x.is_ok(), false);
/// ```
#[must_use = "if you intended to assert that this is ok, consider `.unwrap()` instead"]
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_ok(&self) -> bool {
Expand All @@ -294,7 +294,7 @@ impl<T, E> Result<T, E> {
/// assert_eq!(x.is_err(), true);
/// ```
#[must_use = "if you intended to assert that this is err, consider `.unwrap_err()` instead"]
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn is_err(&self) -> bool {
Expand Down Expand Up @@ -438,7 +438,7 @@ impl<T, E> Result<T, E> {
/// assert_eq!(x.as_ref(), Err(&"Error"));
/// ```
#[inline]
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
#[stable(feature = "rust1", since = "1.0.0")]
pub const fn as_ref(&self) -> Result<&T, &E> {
match *self {
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/consts/const-result.rs
@@ -0,0 +1,12 @@
// run-pass

fn main() {
const X: Result<i32, bool> = Ok(32);
const Y: Result<&i32, &bool> = X.as_ref();

const IS_OK: bool = X.is_ok();
assert!(IS_OK);

const IS_ERR: bool = Y.is_err();
assert!(!IS_ERR)
}

0 comments on commit 518f1cc

Please sign in to comment.