diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-10-17 18:19:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-17 18:19:00 +0200 |
| commit | f044a84f5daecf313893a5f47b8662d0e12697e8 (patch) | |
| tree | 7e3672cb0c68c74cffd33819bfe34f47a6b37edd | |
| parent | 1ee7c2940e082736434eaa6b8bedcc6c82e7169e (diff) | |
| parent | ea28abee2877dcd08c14097d4c12c05da1b581bf (diff) | |
| download | rust-f044a84f5daecf313893a5f47b8662d0e12697e8.tar.gz rust-f044a84f5daecf313893a5f47b8662d0e12697e8.zip | |
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
| -rw-r--r-- | library/core/src/result.rs | 3 | ||||
| -rw-r--r-- | library/core/tests/lib.rs | 1 | ||||
| -rw-r--r-- | library/core/tests/result.rs | 23 |
3 files changed, 26 insertions, 1 deletions
diff --git a/library/core/src/result.rs b/library/core/src/result.rs index dda827900d9..75f2c222ba8 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -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), diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index cf669163d3e..6958f07227a 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -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)] diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index 612f083a5c1..1652c1b83de 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -353,6 +353,29 @@ fn result_const() { } #[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)] struct BadNumErr; |
