Skip to content

Commit

Permalink
Make some Option, Result methods unstably const
Browse files Browse the repository at this point in the history
The following functions are now unstably const:

- Option::transpose
- Option::flatten
- Result::transpose
  • Loading branch information
jhpratt committed Mar 6, 2021
1 parent 097bc6a commit 79c2b75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
use crate::pin::Pin;
use crate::{
convert, fmt, hint, mem,
fmt, hint, mem,
ops::{self, Deref, DerefMut},
};

Expand Down Expand Up @@ -1275,7 +1275,8 @@ impl<T, E> Option<Result<T, E>> {
/// ```
#[inline]
#[stable(feature = "transpose_result", since = "1.33.0")]
pub fn transpose(self) -> Result<Option<T>, E> {
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn transpose(self) -> Result<Option<T>, E> {
match self {
Some(Ok(x)) => Ok(Some(x)),
Some(Err(e)) => Err(e),
Expand Down Expand Up @@ -1750,7 +1751,11 @@ impl<T> Option<Option<T>> {
/// ```
#[inline]
#[stable(feature = "option_flattening", since = "1.40.0")]
pub fn flatten(self) -> Option<T> {
self.and_then(convert::identity)
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
pub const fn flatten(self) -> Option<T> {
match self {
Some(inner) => inner,
None => None,
}
}
}
3 changes: 2 additions & 1 deletion library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,8 @@ impl<T, E> Result<Option<T>, E> {
/// ```
#[inline]
#[stable(feature = "transpose_result", since = "1.33.0")]
pub fn transpose(self) -> Option<Result<T, E>> {
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
pub const fn transpose(self) -> Option<Result<T, E>> {
match self {
Ok(Some(x)) => Some(Ok(x)),
Ok(None) => None,
Expand Down

0 comments on commit 79c2b75

Please sign in to comment.