Skip to content

Commit 20275c0

Browse files
committed
Auto merge of rust-lang#76136 - CDirkx:const-result, r=dtolnay
Stabilize some Result methods as const Stabilize the following methods of Result as const: - `is_ok` - `is_err` - `as_ref` A test is also included, analogous to the test for `const_option`. These methods are currently const under the unstable feature `const_result` (tracking issue: rust-lang#67520). 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) and [PR#76135](rust-lang#76135). Note: these methods are the only methods currently under the `const_result` feature, thus this PR results in the removal of the feature. Related: rust-lang#76225
2 parents 7512d77 + eeb9031 commit 20275c0

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
#![feature(const_ptr_offset)]
8888
#![feature(const_ptr_offset_from)]
8989
#![feature(const_raw_ptr_comparison)]
90-
#![feature(const_result)]
9190
#![feature(const_slice_from_raw_parts)]
9291
#![feature(const_slice_ptr_len)]
9392
#![feature(const_size_of_val)]

core/src/result.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<T, E> Result<T, E> {
273273
/// assert_eq!(x.is_ok(), false);
274274
/// ```
275275
#[must_use = "if you intended to assert that this is ok, consider `.unwrap()` instead"]
276-
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
276+
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
277277
#[inline]
278278
#[stable(feature = "rust1", since = "1.0.0")]
279279
pub const fn is_ok(&self) -> bool {
@@ -294,7 +294,7 @@ impl<T, E> Result<T, E> {
294294
/// assert_eq!(x.is_err(), true);
295295
/// ```
296296
#[must_use = "if you intended to assert that this is err, consider `.unwrap_err()` instead"]
297-
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
297+
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
298298
#[inline]
299299
#[stable(feature = "rust1", since = "1.0.0")]
300300
pub const fn is_err(&self) -> bool {
@@ -438,7 +438,7 @@ impl<T, E> Result<T, E> {
438438
/// assert_eq!(x.as_ref(), Err(&"Error"));
439439
/// ```
440440
#[inline]
441-
#[rustc_const_unstable(feature = "const_result", issue = "67520")]
441+
#[rustc_const_stable(feature = "const_result", since = "1.48.0")]
442442
#[stable(feature = "rust1", since = "1.0.0")]
443443
pub const fn as_ref(&self) -> Result<&T, &E> {
444444
match *self {

core/tests/result.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,19 @@ fn test_result_as_deref_mut() {
304304
let expected_result = Result::Err::<&mut u32, &mut Vec<i32>>(&mut expected_vec);
305305
assert_eq!(mut_err.as_deref_mut(), expected_result);
306306
}
307+
308+
#[test]
309+
fn result_const() {
310+
// test that the methods of `Result` are usable in a const context
311+
312+
const RESULT: Result<usize, bool> = Ok(32);
313+
314+
const REF: Result<&usize, &bool> = RESULT.as_ref();
315+
assert_eq!(REF, Ok(&32));
316+
317+
const IS_OK: bool = RESULT.is_ok();
318+
assert!(IS_OK);
319+
320+
const IS_ERR: bool = RESULT.is_err();
321+
assert!(!IS_ERR)
322+
}

0 commit comments

Comments
 (0)