Skip to content

Commit

Permalink
Rollup merge of rust-lang#130692 - RalfJung:result-flatten, r=Noratrieb
Browse files Browse the repository at this point in the history
make unstable Result::flatten a const fn

This method is still unstable (tracked at rust-lang#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 rust-lang#70142
  • Loading branch information
GuillaumeGomez authored Sep 22, 2024
2 parents f40efa4 + 89c3cba commit c43a9ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}

Expand Down

0 comments on commit c43a9ea

Please sign in to comment.