Skip to content

Commit f044a84

Browse files
authored
Rollup merge of #89977 - woppopo:result_const_as_mut, r=oli-obk
Make Result::as_mut const Adding `const` for `Result::as_mut`. Tracking issue: #82814
2 parents 1ee7c29 + ea28abe commit f044a84

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Diff for: library/core/src/result.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ impl<T, E> Result<T, E> {
729729
/// ```
730730
#[inline]
731731
#[stable(feature = "rust1", since = "1.0.0")]
732-
pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
732+
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
733+
pub const fn as_mut(&mut self) -> Result<&mut T, &mut E> {
733734
match *self {
734735
Ok(ref mut x) => Ok(x),
735736
Err(ref mut x) => Err(x),

Diff for: library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#![feature(once_cell)]
6666
#![feature(unsized_tuple_coercion)]
6767
#![feature(const_option)]
68+
#![feature(const_result)]
6869
#![feature(integer_atomics)]
6970
#![feature(int_roundings)]
7071
#![feature(slice_group_by)]

Diff for: library/core/tests/result.rs

+23
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,29 @@ fn result_const() {
352352
assert!(!IS_ERR)
353353
}
354354

355+
#[test]
356+
const fn result_const_mut() {
357+
let mut result: Result<usize, bool> = Ok(32);
358+
359+
{
360+
let as_mut = result.as_mut();
361+
match as_mut {
362+
Ok(v) => *v = 42,
363+
Err(_) => unreachable!(),
364+
}
365+
}
366+
367+
let mut result_err: Result<usize, bool> = Err(false);
368+
369+
{
370+
let as_mut = result_err.as_mut();
371+
match as_mut {
372+
Ok(_) => unreachable!(),
373+
Err(v) => *v = true,
374+
}
375+
}
376+
}
377+
355378
#[test]
356379
fn result_opt_conversions() {
357380
#[derive(Copy, Clone, Debug, PartialEq)]

0 commit comments

Comments
 (0)