From 79c2b75e88bdab5fccd7d63750ef11aff2f8eaba Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 14 Feb 2021 22:11:46 -0500 Subject: [PATCH] Make some Option, Result methods unstably const The following functions are now unstably const: - Option::transpose - Option::flatten - Result::transpose --- library/core/src/option.rs | 13 +++++++++---- library/core/src/result.rs | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 14e4e4da3b96d..c5c7922f8192c 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -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}, }; @@ -1275,7 +1275,8 @@ impl Option> { /// ``` #[inline] #[stable(feature = "transpose_result", since = "1.33.0")] - pub fn transpose(self) -> Result, E> { + #[rustc_const_unstable(feature = "const_option", issue = "67441")] + pub const fn transpose(self) -> Result, E> { match self { Some(Ok(x)) => Ok(Some(x)), Some(Err(e)) => Err(e), @@ -1750,7 +1751,11 @@ impl Option> { /// ``` #[inline] #[stable(feature = "option_flattening", since = "1.40.0")] - pub fn flatten(self) -> Option { - self.and_then(convert::identity) + #[rustc_const_unstable(feature = "const_option", issue = "67441")] + pub const fn flatten(self) -> Option { + match self { + Some(inner) => inner, + None => None, + } } } diff --git a/library/core/src/result.rs b/library/core/src/result.rs index a43ba5882edcd..a00cd98ae5c2d 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -1233,7 +1233,8 @@ impl Result, E> { /// ``` #[inline] #[stable(feature = "transpose_result", since = "1.33.0")] - pub fn transpose(self) -> Option> { + #[rustc_const_unstable(feature = "const_result", issue = "82814")] + pub const fn transpose(self) -> Option> { match self { Ok(Some(x)) => Some(Ok(x)), Ok(None) => None,