534
534
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
535
535
536
536
use crate :: iter:: { self , FusedIterator , TrustedLen } ;
537
+ use crate :: marker:: Destruct ;
537
538
use crate :: ops:: { self , ControlFlow , Deref , DerefMut } ;
538
539
use crate :: { convert, fmt, hint} ;
539
540
@@ -606,7 +607,13 @@ impl<T, E> Result<T, E> {
606
607
#[ must_use]
607
608
#[ inline]
608
609
#[ stable( feature = "is_some_and" , since = "1.70.0" ) ]
609
- pub fn is_ok_and ( self , f : impl FnOnce ( T ) -> bool ) -> bool {
610
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
611
+ pub const fn is_ok_and < F > ( self , f : F ) -> bool
612
+ where
613
+ F : ~const FnOnce ( T ) -> bool + ~const Destruct ,
614
+ T : ~const Destruct ,
615
+ E : ~const Destruct ,
616
+ {
610
617
match self {
611
618
Err ( _) => false ,
612
619
Ok ( x) => f ( x) ,
@@ -655,7 +662,13 @@ impl<T, E> Result<T, E> {
655
662
#[ must_use]
656
663
#[ inline]
657
664
#[ stable( feature = "is_some_and" , since = "1.70.0" ) ]
658
- pub fn is_err_and ( self , f : impl FnOnce ( E ) -> bool ) -> bool {
665
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
666
+ pub const fn is_err_and < F > ( self , f : F ) -> bool
667
+ where
668
+ F : ~const FnOnce ( E ) -> bool + ~const Destruct ,
669
+ E : ~const Destruct ,
670
+ T : ~const Destruct ,
671
+ {
659
672
match self {
660
673
Ok ( _) => false ,
661
674
Err ( e) => f ( e) ,
@@ -682,8 +695,13 @@ impl<T, E> Result<T, E> {
682
695
/// ```
683
696
#[ inline]
684
697
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
698
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
685
699
#[ rustc_diagnostic_item = "result_ok_method" ]
686
- pub fn ok ( self ) -> Option < T > {
700
+ pub const fn ok ( self ) -> Option < T >
701
+ where
702
+ T : ~const Destruct ,
703
+ E : ~const Destruct ,
704
+ {
687
705
match self {
688
706
Ok ( x) => Some ( x) ,
689
707
Err ( _) => None ,
@@ -706,7 +724,12 @@ impl<T, E> Result<T, E> {
706
724
/// ```
707
725
#[ inline]
708
726
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
709
- pub fn err ( self ) -> Option < E > {
727
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
728
+ pub const fn err ( self ) -> Option < E >
729
+ where
730
+ T : ~const Destruct ,
731
+ E : ~const Destruct ,
732
+ {
710
733
match self {
711
734
Ok ( _) => None ,
712
735
Err ( x) => Some ( x) ,
@@ -796,7 +819,11 @@ impl<T, E> Result<T, E> {
796
819
/// ```
797
820
#[ inline]
798
821
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
799
- pub fn map < U , F : FnOnce ( T ) -> U > ( self , op : F ) -> Result < U , E > {
822
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
823
+ pub const fn map < U , F > ( self , op : F ) -> Result < U , E >
824
+ where
825
+ F : ~const FnOnce ( T ) -> U + ~const Destruct ,
826
+ {
800
827
match self {
801
828
Ok ( t) => Ok ( op ( t) ) ,
802
829
Err ( e) => Err ( e) ,
@@ -823,8 +850,15 @@ impl<T, E> Result<T, E> {
823
850
/// ```
824
851
#[ inline]
825
852
#[ stable( feature = "result_map_or" , since = "1.41.0" ) ]
853
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
826
854
#[ must_use = "if you don't need the returned value, use `if let` instead" ]
827
- pub fn map_or < U , F : FnOnce ( T ) -> U > ( self , default : U , f : F ) -> U {
855
+ pub const fn map_or < U , F > ( self , default : U , f : F ) -> U
856
+ where
857
+ F : ~const FnOnce ( T ) -> U + ~const Destruct ,
858
+ T : ~const Destruct ,
859
+ E : ~const Destruct ,
860
+ U : ~const Destruct ,
861
+ {
828
862
match self {
829
863
Ok ( t) => f ( t) ,
830
864
Err ( _) => default,
@@ -851,7 +885,12 @@ impl<T, E> Result<T, E> {
851
885
/// ```
852
886
#[ inline]
853
887
#[ stable( feature = "result_map_or_else" , since = "1.41.0" ) ]
854
- pub fn map_or_else < U , D : FnOnce ( E ) -> U , F : FnOnce ( T ) -> U > ( self , default : D , f : F ) -> U {
888
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
889
+ pub const fn map_or_else < U , D , F > ( self , default : D , f : F ) -> U
890
+ where
891
+ D : ~const FnOnce ( E ) -> U + ~const Destruct ,
892
+ F : ~const FnOnce ( T ) -> U + ~const Destruct ,
893
+ {
855
894
match self {
856
895
Ok ( t) => f ( t) ,
857
896
Err ( e) => default ( e) ,
@@ -877,10 +916,13 @@ impl<T, E> Result<T, E> {
877
916
/// [default value]: Default::default
878
917
#[ inline]
879
918
#[ unstable( feature = "result_option_map_or_default" , issue = "138099" ) ]
880
- pub fn map_or_default < U , F > ( self , f : F ) -> U
919
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
920
+ pub const fn map_or_default < U , F > ( self , f : F ) -> U
881
921
where
882
- U : Default ,
883
- F : FnOnce ( T ) -> U ,
922
+ F : ~const FnOnce ( T ) -> U + ~const Destruct ,
923
+ U : ~const Default ,
924
+ T : ~const Destruct ,
925
+ E : ~const Destruct ,
884
926
{
885
927
match self {
886
928
Ok ( t) => f ( t) ,
@@ -908,7 +950,11 @@ impl<T, E> Result<T, E> {
908
950
/// ```
909
951
#[ inline]
910
952
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
911
- pub fn map_err < F , O : FnOnce ( E ) -> F > ( self , op : O ) -> Result < T , F > {
953
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
954
+ pub const fn map_err < F , O > ( self , op : O ) -> Result < T , F >
955
+ where
956
+ O : ~const FnOnce ( E ) -> F + ~const Destruct ,
957
+ {
912
958
match self {
913
959
Ok ( t) => Ok ( t) ,
914
960
Err ( e) => Err ( op ( e) ) ,
@@ -930,7 +976,11 @@ impl<T, E> Result<T, E> {
930
976
/// ```
931
977
#[ inline]
932
978
#[ stable( feature = "result_option_inspect" , since = "1.76.0" ) ]
933
- pub fn inspect < F : FnOnce ( & T ) > ( self , f : F ) -> Self {
979
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
980
+ pub const fn inspect < F > ( self , f : F ) -> Self
981
+ where
982
+ F : ~const FnOnce ( & T ) + ~const Destruct ,
983
+ {
934
984
if let Ok ( ref t) = self {
935
985
f ( t) ;
936
986
}
@@ -954,7 +1004,11 @@ impl<T, E> Result<T, E> {
954
1004
/// ```
955
1005
#[ inline]
956
1006
#[ stable( feature = "result_option_inspect" , since = "1.76.0" ) ]
957
- pub fn inspect_err < F : FnOnce ( & E ) > ( self , f : F ) -> Self {
1007
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1008
+ pub const fn inspect_err < F > ( self , f : F ) -> Self
1009
+ where
1010
+ F : ~const FnOnce ( & E ) + ~const Destruct ,
1011
+ {
958
1012
if let Err ( ref e) = self {
959
1013
f ( e) ;
960
1014
}
@@ -1033,7 +1087,8 @@ impl<T, E> Result<T, E> {
1033
1087
/// ```
1034
1088
#[ inline]
1035
1089
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1036
- pub fn iter ( & self ) -> Iter < ' _ , T > {
1090
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1091
+ pub const fn iter ( & self ) -> Iter < ' _ , T > {
1037
1092
Iter { inner : self . as_ref ( ) . ok ( ) }
1038
1093
}
1039
1094
@@ -1056,7 +1111,8 @@ impl<T, E> Result<T, E> {
1056
1111
/// ```
1057
1112
#[ inline]
1058
1113
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1059
- pub fn iter_mut ( & mut self ) -> IterMut < ' _ , T > {
1114
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1115
+ pub const fn iter_mut ( & mut self ) -> IterMut < ' _ , T > {
1060
1116
IterMut { inner : self . as_mut ( ) . ok ( ) }
1061
1117
}
1062
1118
@@ -1195,9 +1251,11 @@ impl<T, E> Result<T, E> {
1195
1251
/// [`FromStr`]: crate::str::FromStr
1196
1252
#[ inline]
1197
1253
#[ stable( feature = "result_unwrap_or_default" , since = "1.16.0" ) ]
1198
- pub fn unwrap_or_default ( self ) -> T
1254
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1255
+ pub const fn unwrap_or_default ( self ) -> T
1199
1256
where
1200
- T : Default ,
1257
+ T : ~const Default + ~const Destruct ,
1258
+ E : ~const Destruct ,
1201
1259
{
1202
1260
match self {
1203
1261
Ok ( x) => x,
@@ -1366,7 +1424,13 @@ impl<T, E> Result<T, E> {
1366
1424
/// ```
1367
1425
#[ inline]
1368
1426
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1369
- pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > {
1427
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1428
+ pub const fn and < U > ( self , res : Result < U , E > ) -> Result < U , E >
1429
+ where
1430
+ T : ~const Destruct ,
1431
+ E : ~const Destruct ,
1432
+ U : ~const Destruct ,
1433
+ {
1370
1434
match self {
1371
1435
Ok ( _) => res,
1372
1436
Err ( e) => Err ( e) ,
@@ -1405,8 +1469,12 @@ impl<T, E> Result<T, E> {
1405
1469
/// ```
1406
1470
#[ inline]
1407
1471
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1472
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1408
1473
#[ rustc_confusables( "flat_map" , "flatmap" ) ]
1409
- pub fn and_then < U , F : FnOnce ( T ) -> Result < U , E > > ( self , op : F ) -> Result < U , E > {
1474
+ pub const fn and_then < U , F > ( self , op : F ) -> Result < U , E >
1475
+ where
1476
+ F : ~const FnOnce ( T ) -> Result < U , E > + ~const Destruct ,
1477
+ {
1410
1478
match self {
1411
1479
Ok ( t) => op ( t) ,
1412
1480
Err ( e) => Err ( e) ,
@@ -1442,7 +1510,13 @@ impl<T, E> Result<T, E> {
1442
1510
/// ```
1443
1511
#[ inline]
1444
1512
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1445
- pub fn or < F > ( self , res : Result < T , F > ) -> Result < T , F > {
1513
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1514
+ pub const fn or < F > ( self , res : Result < T , F > ) -> Result < T , F >
1515
+ where
1516
+ T : ~const Destruct ,
1517
+ E : ~const Destruct ,
1518
+ F : ~const Destruct ,
1519
+ {
1446
1520
match self {
1447
1521
Ok ( v) => Ok ( v) ,
1448
1522
Err ( _) => res,
@@ -1467,7 +1541,11 @@ impl<T, E> Result<T, E> {
1467
1541
/// ```
1468
1542
#[ inline]
1469
1543
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1470
- pub fn or_else < F , O : FnOnce ( E ) -> Result < T , F > > ( self , op : O ) -> Result < T , F > {
1544
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1545
+ pub const fn or_else < F , O > ( self , op : O ) -> Result < T , F >
1546
+ where
1547
+ O : ~const FnOnce ( E ) -> Result < T , F > + ~const Destruct ,
1548
+ {
1471
1549
match self {
1472
1550
Ok ( t) => Ok ( t) ,
1473
1551
Err ( e) => op ( e) ,
@@ -1494,7 +1572,12 @@ impl<T, E> Result<T, E> {
1494
1572
/// ```
1495
1573
#[ inline]
1496
1574
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1497
- pub fn unwrap_or ( self , default : T ) -> T {
1575
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1576
+ pub const fn unwrap_or ( self , default : T ) -> T
1577
+ where
1578
+ T : ~const Destruct ,
1579
+ E : ~const Destruct ,
1580
+ {
1498
1581
match self {
1499
1582
Ok ( t) => t,
1500
1583
Err ( _) => default,
@@ -1515,7 +1598,11 @@ impl<T, E> Result<T, E> {
1515
1598
#[ inline]
1516
1599
#[ track_caller]
1517
1600
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1518
- pub fn unwrap_or_else < F : FnOnce ( E ) -> T > ( self , op : F ) -> T {
1601
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1602
+ pub const fn unwrap_or_else < F > ( self , op : F ) -> T
1603
+ where
1604
+ F : ~const FnOnce ( E ) -> T + ~const Destruct ,
1605
+ {
1519
1606
match self {
1520
1607
Ok ( t) => t,
1521
1608
Err ( e) => op ( e) ,
@@ -1758,7 +1845,7 @@ impl<T, E> Result<Result<T, E>, E> {
1758
1845
#[ cold]
1759
1846
#[ track_caller]
1760
1847
fn unwrap_failed ( msg : & str , error : & dyn fmt:: Debug ) -> ! {
1761
- panic ! ( "{msg}: {error:?}" )
1848
+ panic ! ( "{msg}: {error:?}" ) ;
1762
1849
}
1763
1850
1764
1851
// This is a separate function to avoid constructing a `dyn Debug`
@@ -1769,7 +1856,7 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
1769
1856
#[ inline]
1770
1857
#[ cold]
1771
1858
#[ track_caller]
1772
- fn unwrap_failed < T > ( _msg : & str , _error : & T ) -> ! {
1859
+ const fn unwrap_failed < T > ( _msg : & str , _error : & T ) -> ! {
1773
1860
panic ! ( )
1774
1861
}
1775
1862
0 commit comments