Skip to content

Commit 70488cf

Browse files
committed
Make Option.transpose and Option.flatten const
1 parent 5909f54 commit 70488cf

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/libcore/option.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,20 @@ impl<T, E> Option<Result<T, E>> {
13961396
/// ```
13971397
#[inline]
13981398
#[stable(feature = "transpose_result", since = "1.33.0")]
1399+
#[rustc_const_unstable(feature = "const_option_match")]
1400+
#[cfg(not(bootstrap))]
1401+
pub const fn transpose(self) -> Result<Option<T>, E> {
1402+
match self {
1403+
Some(Ok(x)) => Ok(Some(x)),
1404+
Some(Err(e)) => Err(e),
1405+
None => Ok(None),
1406+
}
1407+
}
1408+
1409+
/// No docs for bootstrap.
1410+
#[inline]
1411+
#[stable(feature = "transpose_result", since = "1.33.0")]
1412+
#[cfg(bootstrap)]
13991413
pub fn transpose(self) -> Result<Option<T>, E> {
14001414
match self {
14011415
Some(Ok(x)) => Ok(Some(x)),
@@ -1834,6 +1848,16 @@ impl<T> Option<Option<T>> {
18341848
/// ```
18351849
#[inline]
18361850
#[stable(feature = "option_flattening", since = "1.40.0")]
1851+
#[rustc_const_unstable(feature = "const_option_match")]
1852+
#[cfg(not(bootstrap))]
1853+
pub const fn flatten(self) -> Option<T> {
1854+
self.and_then(convert::identity)
1855+
}
1856+
1857+
/// No docs for bootstrap.
1858+
#[inline]
1859+
#[stable(feature = "option_flattening", since = "1.40.0")]
1860+
#[cfg(bootstrap)]
18371861
pub fn flatten(self) -> Option<T> {
18381862
self.and_then(convert::identity)
18391863
}

src/test/ui/consts/const-option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,12 @@ assert_same_const! {
117117
const XOR_B: Option<i32> = SOME.xor(None);
118118
const XOR_C: Option<i32> = NONE.xor(Some(1));
119119
const XOR_D: Option<i32> = NONE.xor(None);
120+
121+
const TRANSPOSE_A: Result<Option<i32>, bool> = Some(Ok(2)).transpose();
122+
const TRANSPOSE_B: Result<Option<i32>, bool> = Some(Err(false)).transpose();
123+
const TRANSPOSE_C: Result<Option<i32>, bool> = None.transpose();
124+
125+
const FLATTEN_A: Option<i32> = Some(Some(2)).flatten();
126+
const FLATTEN_B: Option<i32> = Some(None).flatten();
127+
const FLATTEN_C: Option<i32> = None.flatten();
120128
}

0 commit comments

Comments
 (0)