1
1
macro_rules! int_impl {
2
- ( $SelfT: ty, $ActualT: ident, $UnsignedT: ty, $BITS: expr, $BITS_MINUS_ONE: expr, $Min: expr, $Max: expr,
3
- $rot: expr, $rot_op: expr, $rot_result: expr, $swap_op: expr, $swapped: expr,
4
- $reversed: expr, $le_bytes: expr, $be_bytes: expr,
5
- $to_xe_bytes_doc: expr, $from_xe_bytes_doc: expr,
6
- $bound_condition: expr) => {
2
+ (
3
+ Self = $SelfT: ty,
4
+ ActualT = $ActualT: ident,
5
+ UnsignedT = $UnsignedT: ty,
6
+
7
+ // There are all for use *only* in doc comments.
8
+ // As such, they're all passed as literals -- passing them as a string
9
+ // literal is fine if they need to be multiple code tokens.
10
+ // In non-comments, use the associated constants rather than these.
11
+ BITS = $BITS: literal,
12
+ BITS_MINUS_ONE = $BITS_MINUS_ONE: literal,
13
+ Min = $Min: literal,
14
+ Max = $Max: literal,
15
+ rot = $rot: literal,
16
+ rot_op = $rot_op: literal,
17
+ rot_result = $rot_result: literal,
18
+ swap_op = $swap_op: literal,
19
+ swapped = $swapped: literal,
20
+ reversed = $reversed: literal,
21
+ le_bytes = $le_bytes: literal,
22
+ be_bytes = $be_bytes: literal,
23
+ to_xe_bytes_doc = $to_xe_bytes_doc: expr,
24
+ from_xe_bytes_doc = $from_xe_bytes_doc: expr,
25
+ bound_condition = $bound_condition: literal,
26
+ ) => {
7
27
/// The smallest value that can be represented by this integer type
8
28
#[ doc = concat!( "(−2<sup>" , $BITS_MINUS_ONE, "</sup>" , $bound_condition, ")." ) ]
9
29
///
@@ -15,7 +35,7 @@ macro_rules! int_impl {
15
35
#[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MIN, " , stringify!( $Min) , ");" ) ]
16
36
/// ```
17
37
#[ stable( feature = "assoc_int_consts" , since = "1.43.0" ) ]
18
- pub const MIN : Self = !0 ^ ( ( ! 0 as $UnsignedT ) >> 1 ) as Self ;
38
+ pub const MIN : Self = !Self :: MAX ;
19
39
20
40
/// The largest value that can be represented by this integer type
21
41
#[ doc = concat!( "(2<sup>" , $BITS_MINUS_ONE, "</sup> − 1" , $bound_condition, ")." ) ]
@@ -28,7 +48,7 @@ macro_rules! int_impl {
28
48
#[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::MAX, " , stringify!( $Max) , ");" ) ]
29
49
/// ```
30
50
#[ stable( feature = "assoc_int_consts" , since = "1.43.0" ) ]
31
- pub const MAX : Self = ! Self :: MIN ;
51
+ pub const MAX : Self = ( <$UnsignedT> :: MAX >> 1 ) as Self ;
32
52
33
53
/// The size of this integer type in bits.
34
54
///
@@ -38,7 +58,7 @@ macro_rules! int_impl {
38
58
#[ doc = concat!( "assert_eq!(" , stringify!( $SelfT) , "::BITS, " , stringify!( $BITS) , ");" ) ]
39
59
/// ```
40
60
#[ stable( feature = "int_bits_const" , since = "1.53.0" ) ]
41
- pub const BITS : u32 = $ BITS;
61
+ pub const BITS : u32 = <$UnsignedT> :: BITS ;
42
62
43
63
/// Converts a string slice in a given base to an integer.
44
64
///
@@ -1365,7 +1385,7 @@ macro_rules! int_impl {
1365
1385
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1366
1386
// out of bounds
1367
1387
unsafe {
1368
- self . unchecked_shl( rhs & ( $ BITS - 1 ) )
1388
+ self . unchecked_shl( rhs & ( Self :: BITS - 1 ) )
1369
1389
}
1370
1390
}
1371
1391
@@ -1395,7 +1415,7 @@ macro_rules! int_impl {
1395
1415
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1396
1416
// out of bounds
1397
1417
unsafe {
1398
- self . unchecked_shr( rhs & ( $ BITS - 1 ) )
1418
+ self . unchecked_shr( rhs & ( Self :: BITS - 1 ) )
1399
1419
}
1400
1420
}
1401
1421
@@ -1901,7 +1921,7 @@ macro_rules! int_impl {
1901
1921
without modifying the original"]
1902
1922
#[ inline]
1903
1923
pub const fn overflowing_shl( self , rhs: u32 ) -> ( Self , bool ) {
1904
- ( self . wrapping_shl( rhs) , ( rhs > ( $ BITS - 1 ) ) )
1924
+ ( self . wrapping_shl( rhs) , rhs >= Self :: BITS )
1905
1925
}
1906
1926
1907
1927
/// Shifts self right by `rhs` bits.
@@ -1924,7 +1944,7 @@ macro_rules! int_impl {
1924
1944
without modifying the original"]
1925
1945
#[ inline]
1926
1946
pub const fn overflowing_shr( self , rhs: u32 ) -> ( Self , bool ) {
1927
- ( self . wrapping_shr( rhs) , ( rhs > ( $ BITS - 1 ) ) )
1947
+ ( self . wrapping_shr( rhs) , rhs >= Self :: BITS )
1928
1948
}
1929
1949
1930
1950
/// Computes the absolute value of `self`.
0 commit comments