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
This commit is contained in:
Guillaume Gomez 2024-09-22 19:19:16 +02:00 committed by GitHub
commit c43a9ea146
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -2538,6 +2538,7 @@ impl<T> Option<Option<T>> {
#[stable(feature = "option_flattening", since = "1.40.0")] #[stable(feature = "option_flattening", since = "1.40.0")]
#[rustc_const_unstable(feature = "const_option", issue = "67441")] #[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn flatten(self) -> Option<T> { pub const fn flatten(self) -> Option<T> {
// FIXME(const-hack): could be written with `and_then`
match self { match self {
Some(inner) => inner, Some(inner) => inner,
None => None, None => None,

View File

@ -1676,8 +1676,13 @@ impl<T, E> Result<Result<T, E>, E> {
/// ``` /// ```
#[inline] #[inline]
#[unstable(feature = "result_flattening", issue = "70142")] #[unstable(feature = "result_flattening", issue = "70142")]
pub fn flatten(self) -> Result<T, E> { #[rustc_const_unstable(feature = "result_flattening", issue = "70142")]
self.and_then(convert::identity) 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),
}
} }
} }