1
Fork 0

Stabilize some Result methods as const

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).
This commit is contained in:
CDirkx 2020-08-31 02:43:17 +02:00
parent 36b0d7e257
commit 518f1ccb72
3 changed files with 15 additions and 4 deletions

View file

@ -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)]

View file

@ -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 {
@ -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 {
@ -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 {

View file

@ -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)
}