diff options
| author | Ralf Jung <post@ralfj.de> | 2024-09-22 08:40:25 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-09-22 08:40:25 +0200 |
| commit | 89c3cbafb8d212468e899660af5b95f3b9df4bc6 (patch) | |
| tree | 7e2e15cf9b8c1aabb4dec6c2605dcbad2c6a91cf | |
| parent | 6ce376774c0bc46ac8be247bca93ff5a1287a8fc (diff) | |
| download | rust-89c3cbafb8d212468e899660af5b95f3b9df4bc6.tar.gz rust-89c3cbafb8d212468e899660af5b95f3b9df4bc6.zip | |
make unstable Result::flatten a const fn
| -rw-r--r-- | library/core/src/option.rs | 1 | ||||
| -rw-r--r-- | library/core/src/result.rs | 9 |
2 files changed, 8 insertions, 2 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 5ba13969605..30c667e2494 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -2538,6 +2538,7 @@ impl<T> Option<Option<T>> { #[stable(feature = "option_flattening", since = "1.40.0")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn flatten(self) -> Option<T> { + // FIXME(const-hack): could be written with `and_then` match self { Some(inner) => inner, None => None, diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 9edd58259ba..610edae48d3 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1676,8 +1676,13 @@ impl<T, E> Result<Result<T, E>, E> { /// ``` #[inline] #[unstable(feature = "result_flattening", issue = "70142")] - pub fn flatten(self) -> Result<T, E> { - self.and_then(convert::identity) + #[rustc_const_unstable(feature = "result_flattening", issue = "70142")] + pub const fn flatten(self) -> Result<T, E> { + // FIXME(const-hack): could be written with `and_then` + match self { + Ok(inner) => inner, + Err(e) => Err(e), + } } } |
