Skip to content

Commit

Permalink
Make Result::as_mut const
Browse files Browse the repository at this point in the history
  • Loading branch information
lilasta committed Oct 17, 2021
1 parent 12b5bce commit ea28abe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,8 @@ impl<T, E> Result<T, E> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_mut(&mut self) -> Result<&mut T, &mut E> {
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
pub const fn as_mut(&mut self) -> Result<&mut T, &mut E> {
match *self {
Ok(ref mut x) => Ok(x),
Err(ref mut x) => Err(x),
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#![feature(once_cell)]
#![feature(unsized_tuple_coercion)]
#![feature(const_option)]
#![feature(const_result)]
#![feature(integer_atomics)]
#![feature(int_roundings)]
#![feature(slice_group_by)]
Expand Down
23 changes: 23 additions & 0 deletions library/core/tests/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,29 @@ fn result_const() {
assert!(!IS_ERR)
}

#[test]
const fn result_const_mut() {
let mut result: Result<usize, bool> = Ok(32);

{
let as_mut = result.as_mut();
match as_mut {
Ok(v) => *v = 42,
Err(_) => unreachable!(),
}
}

let mut result_err: Result<usize, bool> = Err(false);

{
let as_mut = result_err.as_mut();
match as_mut {
Ok(_) => unreachable!(),
Err(v) => *v = true,
}
}
}

#[test]
fn result_opt_conversions() {
#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down

0 comments on commit ea28abe

Please sign in to comment.