577
577
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
578
578
579
579
use crate :: iter:: { self , FusedIterator , TrustedLen } ;
580
+ use crate :: marker:: Destruct ;
580
581
use crate :: ops:: { self , ControlFlow , Deref , DerefMut } ;
581
582
use crate :: panicking:: { panic, panic_display} ;
582
583
use crate :: pin:: Pin ;
@@ -649,7 +650,8 @@ impl<T> Option<T> {
649
650
#[ must_use]
650
651
#[ inline]
651
652
#[ 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 Destruct + ~const FnOnce ( T ) -> bool ) -> bool {
653
655
match self {
654
656
None => false ,
655
657
Some ( x) => f ( x) ,
@@ -697,7 +699,8 @@ impl<T> Option<T> {
697
699
#[ must_use]
698
700
#[ inline]
699
701
#[ 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 Destruct + ~const FnOnce ( T ) -> bool ) -> bool {
701
704
match self {
702
705
None => true ,
703
706
Some ( x) => f ( x) ,
@@ -1023,7 +1026,12 @@ impl<T> Option<T> {
1023
1026
/// ```
1024
1027
#[ inline]
1025
1028
#[ 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
+ {
1027
1035
match self {
1028
1036
Some ( x) => x,
1029
1037
None => default,
@@ -1042,9 +1050,10 @@ impl<T> Option<T> {
1042
1050
#[ inline]
1043
1051
#[ track_caller]
1044
1052
#[ 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
1046
1055
where
1047
- F : FnOnce ( ) -> T ,
1056
+ F : ~ const Destruct + ~ const FnOnce ( ) -> T ,
1048
1057
{
1049
1058
match self {
1050
1059
Some ( x) => x,
@@ -1073,9 +1082,10 @@ impl<T> Option<T> {
1073
1082
/// [`FromStr`]: crate::str::FromStr
1074
1083
#[ inline]
1075
1084
#[ 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
1077
1087
where
1078
- T : Default ,
1088
+ T : ~ const Default ,
1079
1089
{
1080
1090
match self {
1081
1091
Some ( x) => x,
@@ -1139,9 +1149,10 @@ impl<T> Option<T> {
1139
1149
/// ```
1140
1150
#[ inline]
1141
1151
#[ 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 >
1143
1154
where
1144
- F : FnOnce ( T ) -> U ,
1155
+ F : ~ const Destruct + ~ const FnOnce ( T ) -> U ,
1145
1156
{
1146
1157
match self {
1147
1158
Some ( x) => Some ( f ( x) ) ,
@@ -1169,7 +1180,11 @@ impl<T> Option<T> {
1169
1180
/// ```
1170
1181
#[ inline]
1171
1182
#[ 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 Destruct + ~const FnOnce ( & T ) ,
1187
+ {
1173
1188
if let Some ( ref x) = self {
1174
1189
f ( x) ;
1175
1190
}
@@ -1198,9 +1213,10 @@ impl<T> Option<T> {
1198
1213
#[ inline]
1199
1214
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1200
1215
#[ 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 : ~const Destruct , F > ( self , default : U , f : F ) -> U
1202
1218
where
1203
- F : FnOnce ( T ) -> U ,
1219
+ F : ~ const Destruct + ~ const FnOnce ( T ) -> U ,
1204
1220
{
1205
1221
match self {
1206
1222
Some ( t) => f ( t) ,
@@ -1243,10 +1259,11 @@ impl<T> Option<T> {
1243
1259
/// ```
1244
1260
#[ inline]
1245
1261
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1246
- pub fn map_or_else < U , D , F > ( self , default : D , f : F ) -> U
1262
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1263
+ pub const fn map_or_else < U , D , F > ( self , default : D , f : F ) -> U
1247
1264
where
1248
- D : FnOnce ( ) -> U ,
1249
- F : FnOnce ( T ) -> U ,
1265
+ D : ~ const Destruct + ~ const FnOnce ( ) -> U ,
1266
+ F : ~ const Destruct + ~ const FnOnce ( T ) -> U ,
1250
1267
{
1251
1268
match self {
1252
1269
Some ( t) => f ( t) ,
@@ -1273,10 +1290,11 @@ impl<T> Option<T> {
1273
1290
/// [default value]: Default::default
1274
1291
#[ inline]
1275
1292
#[ unstable( feature = "result_option_map_or_default" , issue = "138099" ) ]
1276
- pub fn map_or_default < U , F > ( self , f : F ) -> U
1293
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1294
+ pub const fn map_or_default < U , F > ( self , f : F ) -> U
1277
1295
where
1278
- U : Default ,
1279
- F : FnOnce ( T ) -> U ,
1296
+ U : ~ const Default ,
1297
+ F : ~ const Destruct + ~ const FnOnce ( T ) -> U ,
1280
1298
{
1281
1299
match self {
1282
1300
Some ( t) => f ( t) ,
@@ -1307,7 +1325,8 @@ impl<T> Option<T> {
1307
1325
/// ```
1308
1326
#[ inline]
1309
1327
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1310
- pub fn ok_or < E > ( self , err : E ) -> Result < T , E > {
1328
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1329
+ pub const fn ok_or < E : ~const Destruct > ( self , err : E ) -> Result < T , E > {
1311
1330
match self {
1312
1331
Some ( v) => Ok ( v) ,
1313
1332
None => Err ( err) ,
@@ -1332,9 +1351,10 @@ impl<T> Option<T> {
1332
1351
/// ```
1333
1352
#[ inline]
1334
1353
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1335
- pub fn ok_or_else < E , F > ( self , err : F ) -> Result < T , E >
1354
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1355
+ pub const fn ok_or_else < E , F > ( self , err : F ) -> Result < T , E >
1336
1356
where
1337
- F : FnOnce ( ) -> E ,
1357
+ F : ~ const Destruct + ~ const FnOnce ( ) -> E ,
1338
1358
{
1339
1359
match self {
1340
1360
Some ( v) => Ok ( v) ,
@@ -1463,7 +1483,12 @@ impl<T> Option<T> {
1463
1483
/// ```
1464
1484
#[ inline]
1465
1485
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1466
- pub fn and < U > ( self , optb : Option < U > ) -> Option < U > {
1486
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1487
+ pub const fn and < U > ( self , optb : Option < U > ) -> Option < U >
1488
+ where
1489
+ T : ~const Destruct ,
1490
+ U : ~const Destruct ,
1491
+ {
1467
1492
match self {
1468
1493
Some ( _) => optb,
1469
1494
None => None ,
@@ -1502,9 +1527,10 @@ impl<T> Option<T> {
1502
1527
#[ inline]
1503
1528
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1504
1529
#[ rustc_confusables( "flat_map" , "flatmap" ) ]
1505
- pub fn and_then < U , F > ( self , f : F ) -> Option < U >
1530
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1531
+ pub const fn and_then < U , F > ( self , f : F ) -> Option < U >
1506
1532
where
1507
- F : FnOnce ( T ) -> Option < U > ,
1533
+ F : ~ const Destruct + ~ const FnOnce ( T ) -> Option < U > ,
1508
1534
{
1509
1535
match self {
1510
1536
Some ( x) => f ( x) ,
@@ -1538,9 +1564,11 @@ impl<T> Option<T> {
1538
1564
/// [`Some(t)`]: Some
1539
1565
#[ inline]
1540
1566
#[ stable( feature = "option_filter" , since = "1.27.0" ) ]
1541
- pub fn filter < P > ( self , predicate : P ) -> Self
1567
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1568
+ pub const fn filter < P > ( self , predicate : P ) -> Self
1542
1569
where
1543
- P : FnOnce ( & T ) -> bool ,
1570
+ P : ~const Destruct + ~const FnOnce ( & T ) -> bool ,
1571
+ T : ~const Destruct ,
1544
1572
{
1545
1573
if let Some ( x) = self {
1546
1574
if predicate ( & x) {
@@ -1579,7 +1607,11 @@ impl<T> Option<T> {
1579
1607
/// ```
1580
1608
#[ inline]
1581
1609
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1582
- pub fn or ( self , optb : Option < T > ) -> Option < T > {
1610
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1611
+ pub const fn or ( self , optb : Option < T > ) -> Option < T >
1612
+ where
1613
+ T : ~const Destruct ,
1614
+ {
1583
1615
match self {
1584
1616
x @ Some ( _) => x,
1585
1617
None => optb,
@@ -1601,9 +1633,13 @@ impl<T> Option<T> {
1601
1633
/// ```
1602
1634
#[ inline]
1603
1635
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1604
- pub fn or_else < F > ( self , f : F ) -> Option < T >
1636
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1637
+ pub const fn or_else < F > ( self , f : F ) -> Option < T >
1605
1638
where
1606
- F : FnOnce ( ) -> Option < T > ,
1639
+ F : ~const Destruct + ~const FnOnce ( ) -> Option < T > ,
1640
+ //FIXME(const_hack): this `T: ~const Destruct` is unnecessary, but even precise live drops can't tell
1641
+ // no value of type `T` gets dropped here
1642
+ T : ~const Destruct ,
1607
1643
{
1608
1644
match self {
1609
1645
x @ Some ( _) => x,
@@ -1634,7 +1670,11 @@ impl<T> Option<T> {
1634
1670
/// ```
1635
1671
#[ inline]
1636
1672
#[ stable( feature = "option_xor" , since = "1.37.0" ) ]
1637
- pub fn xor ( self , optb : Option < T > ) -> Option < T > {
1673
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1674
+ pub const fn xor ( self , optb : Option < T > ) -> Option < T >
1675
+ where
1676
+ T : ~const Destruct ,
1677
+ {
1638
1678
match ( self , optb) {
1639
1679
( a @ Some ( _) , None ) => a,
1640
1680
( None , b @ Some ( _) ) => b,
@@ -1668,7 +1708,11 @@ impl<T> Option<T> {
1668
1708
#[ must_use = "if you intended to set a value, consider assignment instead" ]
1669
1709
#[ inline]
1670
1710
#[ stable( feature = "option_insert" , since = "1.53.0" ) ]
1671
- pub fn insert ( & mut self , value : T ) -> & mut T {
1711
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1712
+ pub const fn insert ( & mut self , value : T ) -> & mut T
1713
+ where
1714
+ T : ~const Destruct ,
1715
+ {
1672
1716
* self = Some ( value) ;
1673
1717
1674
1718
// SAFETY: the code above just filled the option
@@ -1720,9 +1764,10 @@ impl<T> Option<T> {
1720
1764
/// ```
1721
1765
#[ inline]
1722
1766
#[ stable( feature = "option_get_or_insert_default" , since = "1.83.0" ) ]
1723
- pub fn get_or_insert_default ( & mut self ) -> & mut T
1767
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1768
+ pub const fn get_or_insert_default ( & mut self ) -> & mut T
1724
1769
where
1725
- T : Default ,
1770
+ T : ~ const Default + ~ const Destruct ,
1726
1771
{
1727
1772
self . get_or_insert_with ( T :: default)
1728
1773
}
@@ -1746,9 +1791,11 @@ impl<T> Option<T> {
1746
1791
/// ```
1747
1792
#[ inline]
1748
1793
#[ stable( feature = "option_entry" , since = "1.20.0" ) ]
1749
- pub fn get_or_insert_with < F > ( & mut self , f : F ) -> & mut T
1794
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1795
+ pub const fn get_or_insert_with < F > ( & mut self , f : F ) -> & mut T
1750
1796
where
1751
- F : FnOnce ( ) -> T ,
1797
+ F : ~const Destruct + ~const FnOnce ( ) -> T ,
1798
+ T : ~const Destruct ,
1752
1799
{
1753
1800
if let None = self {
1754
1801
* self = Some ( f ( ) ) ;
@@ -1812,9 +1859,10 @@ impl<T> Option<T> {
1812
1859
/// ```
1813
1860
#[ inline]
1814
1861
#[ stable( feature = "option_take_if" , since = "1.80.0" ) ]
1815
- pub fn take_if < P > ( & mut self , predicate : P ) -> Option < T >
1862
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1863
+ pub const fn take_if < P > ( & mut self , predicate : P ) -> Option < T >
1816
1864
where
1817
- P : FnOnce ( & mut T ) -> bool ,
1865
+ P : ~ const Destruct + ~ const FnOnce ( & mut T ) -> bool ,
1818
1866
{
1819
1867
if self . as_mut ( ) . map_or ( false , predicate) { self . take ( ) } else { None }
1820
1868
}
@@ -1859,7 +1907,12 @@ impl<T> Option<T> {
1859
1907
/// assert_eq!(x.zip(z), None);
1860
1908
/// ```
1861
1909
#[ stable( feature = "option_zip_option" , since = "1.46.0" ) ]
1862
- pub fn zip < U > ( self , other : Option < U > ) -> Option < ( T , U ) > {
1910
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1911
+ pub const fn zip < U > ( self , other : Option < U > ) -> Option < ( T , U ) >
1912
+ where
1913
+ T : ~const Destruct ,
1914
+ U : ~const Destruct ,
1915
+ {
1863
1916
match ( self , other) {
1864
1917
( Some ( a) , Some ( b) ) => Some ( ( a, b) ) ,
1865
1918
_ => None ,
@@ -1895,9 +1948,12 @@ impl<T> Option<T> {
1895
1948
/// assert_eq!(x.zip_with(None, Point::new), None);
1896
1949
/// ```
1897
1950
#[ unstable( feature = "option_zip" , issue = "70086" ) ]
1898
- pub fn zip_with < U , F , R > ( self , other : Option < U > , f : F ) -> Option < R >
1951
+ #[ rustc_const_unstable( feature = "const_option_ops" , issue = "143956" ) ]
1952
+ pub const fn zip_with < U , F , R > ( self , other : Option < U > , f : F ) -> Option < R >
1899
1953
where
1900
- F : FnOnce ( T , U ) -> R ,
1954
+ F : ~const Destruct + ~const FnOnce ( T , U ) -> R ,
1955
+ T : ~const Destruct ,
1956
+ U : ~const Destruct ,
1901
1957
{
1902
1958
match ( self , other) {
1903
1959
( Some ( a) , Some ( b) ) => Some ( f ( a, b) ) ,
0 commit comments