Skip to content

Commit

Permalink
Rollup merge of rust-lang#89977 - woppopo:result_const_as_mut, r=oli-obk
Browse files Browse the repository at this point in the history
Make Result::as_mut const

Adding `const` for `Result::as_mut`.

Tracking issue: rust-lang#82814
  • Loading branch information
matthiaskrgr authored Oct 17, 2021
2 parents 1ee7c29 + ea28abe commit f044a84
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 f044a84

Please sign in to comment.