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,
@@ -1370,7 +1428,13 @@ impl<T, E> Result<T, E> {
1370
1428
/// ```
1371
1429
#[ inline]
1372
1430
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1373
- pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > {
1431
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1432
+ pub const fn and < U > ( self , res : Result < U , E > ) -> Result < U , E >
1433
+ where
1434
+ T : ~const Destruct ,
1435
+ E : ~const Destruct ,
1436
+ U : ~const Destruct ,
1437
+ {
1374
1438
match self {
1375
1439
Ok ( _) => res,
1376
1440
Err ( e) => Err ( e) ,
@@ -1409,8 +1473,12 @@ impl<T, E> Result<T, E> {
1409
1473
/// ```
1410
1474
#[ inline]
1411
1475
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1476
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1412
1477
#[ rustc_confusables( "flat_map" , "flatmap" ) ]
1413
- pub fn and_then < U , F : FnOnce ( T ) -> Result < U , E > > ( self , op : F ) -> Result < U , E > {
1478
+ pub const fn and_then < U , F > ( self , op : F ) -> Result < U , E >
1479
+ where
1480
+ F : ~const FnOnce ( T ) -> Result < U , E > + ~const Destruct ,
1481
+ {
1414
1482
match self {
1415
1483
Ok ( t) => op ( t) ,
1416
1484
Err ( e) => Err ( e) ,
@@ -1446,7 +1514,13 @@ impl<T, E> Result<T, E> {
1446
1514
/// ```
1447
1515
#[ inline]
1448
1516
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1449
- pub fn or < F > ( self , res : Result < T , F > ) -> Result < T , F > {
1517
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1518
+ pub const fn or < F > ( self , res : Result < T , F > ) -> Result < T , F >
1519
+ where
1520
+ T : ~const Destruct ,
1521
+ E : ~const Destruct ,
1522
+ F : ~const Destruct ,
1523
+ {
1450
1524
match self {
1451
1525
Ok ( v) => Ok ( v) ,
1452
1526
Err ( _) => res,
@@ -1471,7 +1545,11 @@ impl<T, E> Result<T, E> {
1471
1545
/// ```
1472
1546
#[ inline]
1473
1547
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1474
- pub fn or_else < F , O : FnOnce ( E ) -> Result < T , F > > ( self , op : O ) -> Result < T , F > {
1548
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1549
+ pub const fn or_else < F , O > ( self , op : O ) -> Result < T , F >
1550
+ where
1551
+ O : ~const FnOnce ( E ) -> Result < T , F > + ~const Destruct ,
1552
+ {
1475
1553
match self {
1476
1554
Ok ( t) => Ok ( t) ,
1477
1555
Err ( e) => op ( e) ,
@@ -1498,7 +1576,12 @@ impl<T, E> Result<T, E> {
1498
1576
/// ```
1499
1577
#[ inline]
1500
1578
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1501
- pub fn unwrap_or ( self , default : T ) -> T {
1579
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1580
+ pub const fn unwrap_or ( self , default : T ) -> T
1581
+ where
1582
+ T : ~const Destruct ,
1583
+ E : ~const Destruct ,
1584
+ {
1502
1585
match self {
1503
1586
Ok ( t) => t,
1504
1587
Err ( _) => default,
@@ -1519,7 +1602,11 @@ impl<T, E> Result<T, E> {
1519
1602
#[ inline]
1520
1603
#[ track_caller]
1521
1604
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1522
- pub fn unwrap_or_else < F : FnOnce ( E ) -> T > ( self , op : F ) -> T {
1605
+ #[ rustc_const_unstable( feature = "const_result_trait_fn" , issue = "144211" ) ]
1606
+ pub const fn unwrap_or_else < F > ( self , op : F ) -> T
1607
+ where
1608
+ F : ~const FnOnce ( E ) -> T + ~const Destruct ,
1609
+ {
1523
1610
match self {
1524
1611
Ok ( t) => t,
1525
1612
Err ( e) => op ( e) ,
@@ -1762,7 +1849,7 @@ impl<T, E> Result<Result<T, E>, E> {
1762
1849
#[ cold]
1763
1850
#[ track_caller]
1764
1851
fn unwrap_failed ( msg : & str , error : & dyn fmt:: Debug ) -> ! {
1765
- panic ! ( "{msg}: {error:?}" )
1852
+ panic ! ( "{msg}: {error:?}" ) ;
1766
1853
}
1767
1854
1768
1855
// This is a separate function to avoid constructing a `dyn Debug`
@@ -1773,7 +1860,7 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
1773
1860
#[ inline]
1774
1861
#[ cold]
1775
1862
#[ track_caller]
1776
- fn unwrap_failed < T > ( _msg : & str , _error : & T ) -> ! {
1863
+ const fn unwrap_failed < T > ( _msg : & str , _error : & T ) -> ! {
1777
1864
panic ! ( )
1778
1865
}
1779
1866
0 commit comments