Skip to content

Commit 0adc196

Browse files
authored
Rollup merge of #82130 - jhpratt:const-option-result, r=RalfJung
Make some Option, Result methods unstably const The following methods are now unstably const: - Option::transpose - Option::flatten - Result::flatten While some methods for could likely be made `const` in the future, nearly all of them require something to be dropped at compile-time, which isn't currently supported. The functions listed above should have a trivial path to stabilization.
2 parents d1dc166 + 79c2b75 commit 0adc196

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

library/core/src/option.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
151151
use crate::pin::Pin;
152152
use crate::{
153-
convert, fmt, hint, mem,
153+
fmt, hint, mem,
154154
ops::{self, Deref, DerefMut},
155155
};
156156

@@ -1275,7 +1275,8 @@ impl<T, E> Option<Result<T, E>> {
12751275
/// ```
12761276
#[inline]
12771277
#[stable(feature = "transpose_result", since = "1.33.0")]
1278-
pub fn transpose(self) -> Result<Option<T>, E> {
1278+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1279+
pub const fn transpose(self) -> Result<Option<T>, E> {
12791280
match self {
12801281
Some(Ok(x)) => Ok(Some(x)),
12811282
Some(Err(e)) => Err(e),
@@ -1750,7 +1751,11 @@ impl<T> Option<Option<T>> {
17501751
/// ```
17511752
#[inline]
17521753
#[stable(feature = "option_flattening", since = "1.40.0")]
1753-
pub fn flatten(self) -> Option<T> {
1754-
self.and_then(convert::identity)
1754+
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1755+
pub const fn flatten(self) -> Option<T> {
1756+
match self {
1757+
Some(inner) => inner,
1758+
None => None,
1759+
}
17551760
}
17561761
}

library/core/src/result.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,8 @@ impl<T, E> Result<Option<T>, E> {
12331233
/// ```
12341234
#[inline]
12351235
#[stable(feature = "transpose_result", since = "1.33.0")]
1236-
pub fn transpose(self) -> Option<Result<T, E>> {
1236+
#[rustc_const_unstable(feature = "const_result", issue = "82814")]
1237+
pub const fn transpose(self) -> Option<Result<T, E>> {
12371238
match self {
12381239
Ok(Some(x)) => Some(Ok(x)),
12391240
Ok(None) => None,

0 commit comments

Comments
 (0)