about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-09-22 19:19:16 +0200
committerGitHub <noreply@github.com>2024-09-22 19:19:16 +0200
commitc43a9ea146c299d5dcc6fefb86a186d110845229 (patch)
tree924fc3247016b86fd7d41047f71cd73db0991c3f
parentf40efa44ab3027e627a527c46af5a40a2cb8e2b0 (diff)
parent89c3cbafb8d212468e899660af5b95f3b9df4bc6 (diff)
downloadrust-c43a9ea146c299d5dcc6fefb86a186d110845229.tar.gz
rust-c43a9ea146c299d5dcc6fefb86a186d110845229.zip
Rollup merge of #130692 - RalfJung:result-flatten, r=Noratrieb
make unstable Result::flatten a const fn

This method is still unstable (tracked at https://github.com/rust-lang/rust/issues/70142), but there's no reason I can see for it not to be const -- after all, `Option::flatten` is const. So let's make the `Result` one `const` as well, under the same feature gate.

Cc https://github.com/rust-lang/rust/issues/70142
-rw-r--r--library/core/src/option.rs1
-rw-r--r--library/core/src/result.rs9
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),
+        }
     }
 }