@@ -882,17 +882,38 @@ $EndFeature, "
882
882
```" ) ,
883
883
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
884
884
#[ inline]
885
+ #[ cfg( stage0) ]
885
886
pub fn saturating_add( self , rhs: Self ) -> Self {
886
- #[ cfg( stage0) ]
887
887
match self . checked_add( rhs) {
888
888
Some ( x) => x,
889
889
None if rhs >= 0 => Self :: max_value( ) ,
890
890
None => Self :: min_value( ) ,
891
891
}
892
- #[ cfg( not( stage0) ) ]
893
- {
894
- intrinsics:: saturating_add( self , rhs)
895
- }
892
+ }
893
+
894
+ }
895
+
896
+ doc_comment! {
897
+ concat!( "Saturating integer addition. Computes `self + rhs`, saturating at the numeric
898
+ bounds instead of overflowing.
899
+
900
+ # Examples
901
+
902
+ Basic usage:
903
+
904
+ ```
905
+ " , $Feature, "assert_eq!(100" , stringify!( $SelfT) , ".saturating_add(1), 101);
906
+ assert_eq!(" , stringify!( $SelfT) , "::max_value().saturating_add(100), " , stringify!( $SelfT) ,
907
+ "::max_value());" ,
908
+ $EndFeature, "
909
+ ```" ) ,
910
+
911
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
912
+ #[ rustc_const_unstable( feature = "const_saturating_int_methods" ) ]
913
+ #[ inline]
914
+ #[ cfg( not( stage0) ) ]
915
+ pub const fn saturating_add( self , rhs: Self ) -> Self {
916
+ intrinsics:: saturating_add( self , rhs)
896
917
}
897
918
}
898
919
@@ -912,17 +933,36 @@ $EndFeature, "
912
933
```" ) ,
913
934
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
914
935
#[ inline]
936
+ #[ cfg( stage0) ]
915
937
pub fn saturating_sub( self , rhs: Self ) -> Self {
916
- #[ cfg( stage0) ]
917
938
match self . checked_sub( rhs) {
918
939
Some ( x) => x,
919
940
None if rhs >= 0 => Self :: min_value( ) ,
920
941
None => Self :: max_value( ) ,
921
942
}
922
- #[ cfg( not( stage0) ) ]
923
- {
924
- intrinsics:: saturating_sub( self , rhs)
925
- }
943
+ }
944
+ }
945
+
946
+ doc_comment! {
947
+ concat!( "Saturating integer subtraction. Computes `self - rhs`, saturating at the
948
+ numeric bounds instead of overflowing.
949
+
950
+ # Examples
951
+
952
+ Basic usage:
953
+
954
+ ```
955
+ " , $Feature, "assert_eq!(100" , stringify!( $SelfT) , ".saturating_sub(127), -27);
956
+ assert_eq!(" , stringify!( $SelfT) , "::min_value().saturating_sub(100), " , stringify!( $SelfT) ,
957
+ "::min_value());" ,
958
+ $EndFeature, "
959
+ ```" ) ,
960
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
961
+ #[ rustc_const_unstable( feature = "const_saturating_int_methods" ) ]
962
+ #[ inline]
963
+ #[ cfg( not( stage0) ) ]
964
+ pub const fn saturating_sub( self , rhs: Self ) -> Self {
965
+ intrinsics:: saturating_sub( self , rhs)
926
966
}
927
967
}
928
968
@@ -2753,16 +2793,34 @@ assert_eq!(200u8.saturating_add(127), 255);", $EndFeature, "
2753
2793
```" ) ,
2754
2794
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2755
2795
#[ inline]
2796
+ #[ cfg( stage0) ]
2756
2797
pub fn saturating_add( self , rhs: Self ) -> Self {
2757
- #[ cfg( stage0) ]
2758
2798
match self . checked_add( rhs) {
2759
2799
Some ( x) => x,
2760
2800
None => Self :: max_value( ) ,
2761
2801
}
2762
- #[ cfg( not( stage0) ) ]
2763
- {
2764
- intrinsics:: saturating_add( self , rhs)
2765
- }
2802
+ }
2803
+ }
2804
+
2805
+ doc_comment! {
2806
+ concat!( "Saturating integer addition. Computes `self + rhs`, saturating at
2807
+ the numeric bounds instead of overflowing.
2808
+
2809
+ # Examples
2810
+
2811
+ Basic usage:
2812
+
2813
+ ```
2814
+ " , $Feature, "assert_eq!(100" , stringify!( $SelfT) , ".saturating_add(1), 101);
2815
+ assert_eq!(200u8.saturating_add(127), 255);" , $EndFeature, "
2816
+ ```" ) ,
2817
+
2818
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2819
+ #[ rustc_const_unstable( feature = "const_saturating_int_methods" ) ]
2820
+ #[ inline]
2821
+ #[ cfg( not( stage0) ) ]
2822
+ pub const fn saturating_add( self , rhs: Self ) -> Self {
2823
+ intrinsics:: saturating_add( self , rhs)
2766
2824
}
2767
2825
}
2768
2826
@@ -2780,16 +2838,33 @@ assert_eq!(13", stringify!($SelfT), ".saturating_sub(127), 0);", $EndFeature, "
2780
2838
```" ) ,
2781
2839
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2782
2840
#[ inline]
2841
+ #[ cfg( stage0) ]
2783
2842
pub fn saturating_sub( self , rhs: Self ) -> Self {
2784
- #[ cfg( stage0) ]
2785
2843
match self . checked_sub( rhs) {
2786
2844
Some ( x) => x,
2787
2845
None => Self :: min_value( ) ,
2788
2846
}
2789
- #[ cfg( not( stage0) ) ]
2790
- {
2791
- intrinsics:: saturating_sub( self , rhs)
2792
- }
2847
+ }
2848
+ }
2849
+
2850
+ doc_comment! {
2851
+ concat!( "Saturating integer subtraction. Computes `self - rhs`, saturating
2852
+ at the numeric bounds instead of overflowing.
2853
+
2854
+ # Examples
2855
+
2856
+ Basic usage:
2857
+
2858
+ ```
2859
+ " , $Feature, "assert_eq!(100" , stringify!( $SelfT) , ".saturating_sub(27), 73);
2860
+ assert_eq!(13" , stringify!( $SelfT) , ".saturating_sub(127), 0);" , $EndFeature, "
2861
+ ```" ) ,
2862
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2863
+ #[ rustc_const_unstable( feature = "const_saturating_int_methods" ) ]
2864
+ #[ inline]
2865
+ #[ cfg( not( stage0) ) ]
2866
+ pub const fn saturating_sub( self , rhs: Self ) -> Self {
2867
+ intrinsics:: saturating_sub( self , rhs)
2793
2868
}
2794
2869
}
2795
2870
0 commit comments