Make Result::as_mut const

This commit is contained in:
woppopo 2021-10-17 01:18:00 +09:00
parent 12b5bce0d7
commit ea28abee28
3 changed files with 26 additions and 1 deletions

View file

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

View file

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

View file

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