From 89c3cbafb8d212468e899660af5b95f3b9df4bc6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 22 Sep 2024 08:40:25 +0200 Subject: [PATCH] make unstable Result::flatten a const fn --- library/core/src/option.rs | 1 + 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 Option> { #[stable(feature = "option_flattening", since = "1.40.0")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn flatten(self) -> Option { + // 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 Result, E> { /// ``` #[inline] #[unstable(feature = "result_flattening", issue = "70142")] - pub fn flatten(self) -> Result { - self.and_then(convert::identity) + #[rustc_const_unstable(feature = "result_flattening", issue = "70142")] + pub const fn flatten(self) -> Result { + // FIXME(const-hack): could be written with `and_then` + match self { + Ok(inner) => inner, + Err(e) => Err(e), + } } }