577577#![ stable( feature = "rust1" , since = "1.0.0" ) ]
578578
579579use crate :: iter:: { self , FusedIterator , TrustedLen } ;
580+ use crate :: marker:: Destruct ;
580581use crate :: ops:: { self , ControlFlow , Deref , DerefMut } ;
581582use crate :: panicking:: { panic, panic_display} ;
582583use crate :: pin:: Pin ;
@@ -649,7 +650,8 @@ impl<T> Option<T> {
649650 #[ must_use]
650651 #[ inline]
651652 #[ stable( feature = "is_some_and" , since = "1.70.0" ) ]
652- pub fn is_some_and ( self , f : impl FnOnce ( T ) -> bool ) -> bool {
653+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
654+ pub const fn is_some_and ( self , f : impl ~const FnOnce ( T ) -> bool + ~const Destruct ) -> bool {
653655 match self {
654656 None => false ,
655657 Some ( x) => f ( x) ,
@@ -697,7 +699,8 @@ impl<T> Option<T> {
697699 #[ must_use]
698700 #[ inline]
699701 #[ stable( feature = "is_none_or" , since = "1.82.0" ) ]
700- pub fn is_none_or ( self , f : impl FnOnce ( T ) -> bool ) -> bool {
702+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
703+ pub const fn is_none_or ( self , f : impl ~const FnOnce ( T ) -> bool + ~const Destruct ) -> bool {
701704 match self {
702705 None => true ,
703706 Some ( x) => f ( x) ,
@@ -1023,7 +1026,12 @@ impl<T> Option<T> {
10231026 /// ```
10241027 #[ inline]
10251028 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1026- pub fn unwrap_or ( self , default : T ) -> T {
1029+ #[ rustc_allow_const_fn_unstable( const_precise_live_drops) ]
1030+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1031+ pub const fn unwrap_or ( self , default : T ) -> T
1032+ where
1033+ T : ~const Destruct ,
1034+ {
10271035 match self {
10281036 Some ( x) => x,
10291037 None => default,
@@ -1042,9 +1050,10 @@ impl<T> Option<T> {
10421050 #[ inline]
10431051 #[ track_caller]
10441052 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1045- pub fn unwrap_or_else < F > ( self , f : F ) -> T
1053+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1054+ pub const fn unwrap_or_else < F > ( self , f : F ) -> T
10461055 where
1047- F : FnOnce ( ) -> T ,
1056+ F : ~ const FnOnce ( ) -> T + ~ const Destruct ,
10481057 {
10491058 match self {
10501059 Some ( x) => x,
@@ -1073,9 +1082,10 @@ impl<T> Option<T> {
10731082 /// [`FromStr`]: crate::str::FromStr
10741083 #[ inline]
10751084 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1076- pub fn unwrap_or_default ( self ) -> T
1085+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1086+ pub const fn unwrap_or_default ( self ) -> T
10771087 where
1078- T : Default ,
1088+ T : ~ const Default ,
10791089 {
10801090 match self {
10811091 Some ( x) => x,
@@ -1139,9 +1149,10 @@ impl<T> Option<T> {
11391149 /// ```
11401150 #[ inline]
11411151 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1142- pub fn map < U , F > ( self , f : F ) -> Option < U >
1152+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1153+ pub const fn map < U , F > ( self , f : F ) -> Option < U >
11431154 where
1144- F : FnOnce ( T ) -> U ,
1155+ F : ~ const FnOnce ( T ) -> U + ~ const Destruct ,
11451156 {
11461157 match self {
11471158 Some ( x) => Some ( f ( x) ) ,
@@ -1169,7 +1180,11 @@ impl<T> Option<T> {
11691180 /// ```
11701181 #[ inline]
11711182 #[ stable( feature = "result_option_inspect" , since = "1.76.0" ) ]
1172- pub fn inspect < F : FnOnce ( & T ) > ( self , f : F ) -> Self {
1183+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1184+ pub const fn inspect < F > ( self , f : F ) -> Self
1185+ where
1186+ F : ~const FnOnce ( & T ) + ~const Destruct ,
1187+ {
11731188 if let Some ( ref x) = self {
11741189 f ( x) ;
11751190 }
@@ -1198,9 +1213,11 @@ impl<T> Option<T> {
11981213 #[ inline]
11991214 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
12001215 #[ must_use = "if you don't need the returned value, use `if let` instead" ]
1201- pub fn map_or < U , F > ( self , default : U , f : F ) -> U
1216+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1217+ pub const fn map_or < U , F > ( self , default : U , f : F ) -> U
12021218 where
1203- F : FnOnce ( T ) -> U ,
1219+ F : ~const FnOnce ( T ) -> U + ~const Destruct ,
1220+ U : ~const Destruct ,
12041221 {
12051222 match self {
12061223 Some ( t) => f ( t) ,
@@ -1243,10 +1260,11 @@ impl<T> Option<T> {
12431260 /// ```
12441261 #[ inline]
12451262 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1246- pub fn map_or_else < U , D , F > ( self , default : D , f : F ) -> U
1263+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1264+ pub const fn map_or_else < U , D , F > ( self , default : D , f : F ) -> U
12471265 where
1248- D : FnOnce ( ) -> U ,
1249- F : FnOnce ( T ) -> U ,
1266+ D : ~ const FnOnce ( ) -> U + ~ const Destruct ,
1267+ F : ~ const FnOnce ( T ) -> U + ~ const Destruct ,
12501268 {
12511269 match self {
12521270 Some ( t) => f ( t) ,
@@ -1273,10 +1291,11 @@ impl<T> Option<T> {
12731291 /// [default value]: Default::default
12741292 #[ inline]
12751293 #[ unstable( feature = "result_option_map_or_default" , issue = "138099" ) ]
1276- pub fn map_or_default < U , F > ( self , f : F ) -> U
1294+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1295+ pub const fn map_or_default < U , F > ( self , f : F ) -> U
12771296 where
1278- U : Default ,
1279- F : FnOnce ( T ) -> U ,
1297+ U : ~ const Default ,
1298+ F : ~ const FnOnce ( T ) -> U + ~ const Destruct ,
12801299 {
12811300 match self {
12821301 Some ( t) => f ( t) ,
@@ -1307,7 +1326,8 @@ impl<T> Option<T> {
13071326 /// ```
13081327 #[ inline]
13091328 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1310- pub fn ok_or < E > ( self , err : E ) -> Result < T , E > {
1329+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1330+ pub const fn ok_or < E : ~const Destruct > ( self , err : E ) -> Result < T , E > {
13111331 match self {
13121332 Some ( v) => Ok ( v) ,
13131333 None => Err ( err) ,
@@ -1332,9 +1352,10 @@ impl<T> Option<T> {
13321352 /// ```
13331353 #[ inline]
13341354 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1335- pub fn ok_or_else < E , F > ( self , err : F ) -> Result < T , E >
1355+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1356+ pub const fn ok_or_else < E , F > ( self , err : F ) -> Result < T , E >
13361357 where
1337- F : FnOnce ( ) -> E ,
1358+ F : ~ const FnOnce ( ) -> E + ~ const Destruct ,
13381359 {
13391360 match self {
13401361 Some ( v) => Ok ( v) ,
@@ -1463,7 +1484,12 @@ impl<T> Option<T> {
14631484 /// ```
14641485 #[ inline]
14651486 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1466- pub fn and < U > ( self , optb : Option < U > ) -> Option < U > {
1487+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1488+ pub const fn and < U > ( self , optb : Option < U > ) -> Option < U >
1489+ where
1490+ T : ~const Destruct ,
1491+ U : ~const Destruct ,
1492+ {
14671493 match self {
14681494 Some ( _) => optb,
14691495 None => None ,
@@ -1502,9 +1528,10 @@ impl<T> Option<T> {
15021528 #[ inline]
15031529 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15041530 #[ rustc_confusables( "flat_map" , "flatmap" ) ]
1505- pub fn and_then < U , F > ( self , f : F ) -> Option < U >
1531+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1532+ pub const fn and_then < U , F > ( self , f : F ) -> Option < U >
15061533 where
1507- F : FnOnce ( T ) -> Option < U > ,
1534+ F : ~ const FnOnce ( T ) -> Option < U > + ~ const Destruct ,
15081535 {
15091536 match self {
15101537 Some ( x) => f ( x) ,
@@ -1538,9 +1565,11 @@ impl<T> Option<T> {
15381565 /// [`Some(t)`]: Some
15391566 #[ inline]
15401567 #[ stable( feature = "option_filter" , since = "1.27.0" ) ]
1541- pub fn filter < P > ( self , predicate : P ) -> Self
1568+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1569+ pub const fn filter < P > ( self , predicate : P ) -> Self
15421570 where
1543- P : FnOnce ( & T ) -> bool ,
1571+ P : ~const FnOnce ( & T ) -> bool + ~const Destruct ,
1572+ T : ~const Destruct ,
15441573 {
15451574 if let Some ( x) = self {
15461575 if predicate ( & x) {
@@ -1579,7 +1608,11 @@ impl<T> Option<T> {
15791608 /// ```
15801609 #[ inline]
15811610 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1582- pub fn or ( self , optb : Option < T > ) -> Option < T > {
1611+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1612+ pub const fn or ( self , optb : Option < T > ) -> Option < T >
1613+ where
1614+ T : ~const Destruct ,
1615+ {
15831616 match self {
15841617 x @ Some ( _) => x,
15851618 None => optb,
@@ -1601,9 +1634,13 @@ impl<T> Option<T> {
16011634 /// ```
16021635 #[ inline]
16031636 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1604- pub fn or_else < F > ( self , f : F ) -> Option < T >
1637+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1638+ pub const fn or_else < F > ( self , f : F ) -> Option < T >
16051639 where
1606- F : FnOnce ( ) -> Option < T > ,
1640+ F : ~const FnOnce ( ) -> Option < T > + ~const Destruct ,
1641+ //FIXME(const_hack): this `T: ~const Destruct` is unnecessary, but even precise live drops can't tell
1642+ // no value of type `T` gets dropped here
1643+ T : ~const Destruct ,
16071644 {
16081645 match self {
16091646 x @ Some ( _) => x,
@@ -1634,7 +1671,11 @@ impl<T> Option<T> {
16341671 /// ```
16351672 #[ inline]
16361673 #[ stable( feature = "option_xor" , since = "1.37.0" ) ]
1637- pub fn xor ( self , optb : Option < T > ) -> Option < T > {
1674+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1675+ pub const fn xor ( self , optb : Option < T > ) -> Option < T >
1676+ where
1677+ T : ~const Destruct ,
1678+ {
16381679 match ( self , optb) {
16391680 ( a @ Some ( _) , None ) => a,
16401681 ( None , b @ Some ( _) ) => b,
@@ -1668,7 +1709,11 @@ impl<T> Option<T> {
16681709 #[ must_use = "if you intended to set a value, consider assignment instead" ]
16691710 #[ inline]
16701711 #[ stable( feature = "option_insert" , since = "1.53.0" ) ]
1671- pub fn insert ( & mut self , value : T ) -> & mut T {
1712+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1713+ pub const fn insert ( & mut self , value : T ) -> & mut T
1714+ where
1715+ T : ~const Destruct ,
1716+ {
16721717 * self = Some ( value) ;
16731718
16741719 // SAFETY: the code above just filled the option
@@ -1720,9 +1765,10 @@ impl<T> Option<T> {
17201765 /// ```
17211766 #[ inline]
17221767 #[ stable( feature = "option_get_or_insert_default" , since = "1.83.0" ) ]
1723- pub fn get_or_insert_default ( & mut self ) -> & mut T
1768+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1769+ pub const fn get_or_insert_default ( & mut self ) -> & mut T
17241770 where
1725- T : Default ,
1771+ T : ~ const Default + ~ const Destruct ,
17261772 {
17271773 self . get_or_insert_with ( T :: default)
17281774 }
@@ -1746,9 +1792,11 @@ impl<T> Option<T> {
17461792 /// ```
17471793 #[ inline]
17481794 #[ stable( feature = "option_entry" , since = "1.20.0" ) ]
1749- pub fn get_or_insert_with < F > ( & mut self , f : F ) -> & mut T
1795+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1796+ pub const fn get_or_insert_with < F > ( & mut self , f : F ) -> & mut T
17501797 where
1751- F : FnOnce ( ) -> T ,
1798+ F : ~const FnOnce ( ) -> T + ~const Destruct ,
1799+ T : ~const Destruct ,
17521800 {
17531801 if let None = self {
17541802 * self = Some ( f ( ) ) ;
@@ -1812,9 +1860,10 @@ impl<T> Option<T> {
18121860 /// ```
18131861 #[ inline]
18141862 #[ stable( feature = "option_take_if" , since = "1.80.0" ) ]
1815- pub fn take_if < P > ( & mut self , predicate : P ) -> Option < T >
1863+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1864+ pub const fn take_if < P > ( & mut self , predicate : P ) -> Option < T >
18161865 where
1817- P : FnOnce ( & mut T ) -> bool ,
1866+ P : ~ const FnOnce ( & mut T ) -> bool + ~ const Destruct ,
18181867 {
18191868 if self . as_mut ( ) . map_or ( false , predicate) { self . take ( ) } else { None }
18201869 }
@@ -1859,7 +1908,12 @@ impl<T> Option<T> {
18591908 /// assert_eq!(x.zip(z), None);
18601909 /// ```
18611910 #[ stable( feature = "option_zip_option" , since = "1.46.0" ) ]
1862- pub fn zip < U > ( self , other : Option < U > ) -> Option < ( T , U ) > {
1911+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1912+ pub const fn zip < U > ( self , other : Option < U > ) -> Option < ( T , U ) >
1913+ where
1914+ T : ~const Destruct ,
1915+ U : ~const Destruct ,
1916+ {
18631917 match ( self , other) {
18641918 ( Some ( a) , Some ( b) ) => Some ( ( a, b) ) ,
18651919 _ => None ,
@@ -1895,9 +1949,12 @@ impl<T> Option<T> {
18951949 /// assert_eq!(x.zip_with(None, Point::new), None);
18961950 /// ```
18971951 #[ unstable( feature = "option_zip" , issue = "70086" ) ]
1898- pub fn zip_with < U , F , R > ( self , other : Option < U > , f : F ) -> Option < R >
1952+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1953+ pub const fn zip_with < U , F , R > ( self , other : Option < U > , f : F ) -> Option < R >
18991954 where
1900- F : FnOnce ( T , U ) -> R ,
1955+ F : ~const FnOnce ( T , U ) -> R + ~const Destruct ,
1956+ T : ~const Destruct ,
1957+ U : ~const Destruct ,
19011958 {
19021959 match ( self , other) {
19031960 ( Some ( a) , Some ( b) ) => Some ( f ( a, b) ) ,
0 commit comments