@@ -53,6 +53,8 @@ pub mod flt2dec;
53
53
/// `x + T::ZERO == x`.
54
54
#[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
55
55
pub trait Zero {
56
+ fn zero ( ) -> Self ;
57
+
56
58
/// The "zero" (usually, additive identity) for this type.
57
59
const ZERO : Self ;
58
60
}
@@ -63,16 +65,22 @@ pub trait Zero {
63
65
/// `x * T::ONE == x`.
64
66
#[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
65
67
pub trait One {
68
+ fn one ( ) -> Self ;
69
+
66
70
/// The "one" (usually, multiplicative identity) for this type.
67
71
const ONE : Self ;
68
72
}
69
73
70
74
macro_rules! zero_one_impl {
71
75
( $( $t: ty) * ) => ( $(
72
76
impl Zero for $t {
77
+ fn zero( ) -> Self { Self :: ZERO }
78
+
73
79
const ZERO : $t = 0 ;
74
80
}
75
81
impl One for $t {
82
+ fn one( ) -> Self { Self :: ONE }
83
+
76
84
const ONE : $t = 1 ;
77
85
}
78
86
) * )
@@ -82,9 +90,13 @@ zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
82
90
macro_rules! zero_one_impl_float {
83
91
( $( $t: ty) * ) => ( $(
84
92
impl Zero for $t {
93
+ fn zero( ) -> Self { Self :: ZERO }
94
+
85
95
const ZERO : $t = 0.0 ;
86
96
}
87
97
impl One for $t {
98
+ fn one( ) -> Self { Self :: ONE }
99
+
88
100
const ONE : $t = 1.0 ;
89
101
}
90
102
) * )
@@ -409,7 +421,7 @@ macro_rules! int_impl {
409
421
pub fn saturating_add( self , other: Self ) -> Self {
410
422
match self . checked_add( other) {
411
423
Some ( x) => x,
412
- None if other >= Self :: ZERO => Self :: max_value( ) ,
424
+ None if other >= Self :: zero ( ) => Self :: max_value( ) ,
413
425
None => Self :: min_value( ) ,
414
426
}
415
427
}
@@ -421,7 +433,7 @@ macro_rules! int_impl {
421
433
pub fn saturating_sub( self , other: Self ) -> Self {
422
434
match self . checked_sub( other) {
423
435
Some ( x) => x,
424
- None if other >= Self :: ZERO => Self :: min_value( ) ,
436
+ None if other >= Self :: zero ( ) => Self :: min_value( ) ,
425
437
None => Self :: max_value( ) ,
426
438
}
427
439
}
@@ -529,7 +541,7 @@ macro_rules! int_impl {
529
541
#[ inline]
530
542
pub fn pow( self , mut exp: u32 ) -> Self {
531
543
let mut base = self ;
532
- let mut acc = Self :: ONE ;
544
+ let mut acc = Self :: one ( ) ;
533
545
534
546
let mut prev_base = self ;
535
547
let mut base_oflo = false ;
@@ -979,7 +991,7 @@ macro_rules! uint_impl {
979
991
pub fn saturating_add( self , other: Self ) -> Self {
980
992
match self . checked_add( other) {
981
993
Some ( x) => x,
982
- None if other >= Self :: ZERO => Self :: max_value( ) ,
994
+ None if other >= Self :: zero ( ) => Self :: max_value( ) ,
983
995
None => Self :: min_value( ) ,
984
996
}
985
997
}
@@ -991,7 +1003,7 @@ macro_rules! uint_impl {
991
1003
pub fn saturating_sub( self , other: Self ) -> Self {
992
1004
match self . checked_sub( other) {
993
1005
Some ( x) => x,
994
- None if other >= Self :: ZERO => Self :: min_value( ) ,
1006
+ None if other >= Self :: zero ( ) => Self :: min_value( ) ,
995
1007
None => Self :: max_value( ) ,
996
1008
}
997
1009
}
@@ -1097,7 +1109,7 @@ macro_rules! uint_impl {
1097
1109
#[ inline]
1098
1110
pub fn pow( self , mut exp: u32 ) -> Self {
1099
1111
let mut base = self ;
1100
- let mut acc = Self :: ONE ;
1112
+ let mut acc = Self :: one ( ) ;
1101
1113
1102
1114
let mut prev_base = self ;
1103
1115
let mut base_oflo = false ;
@@ -1125,8 +1137,8 @@ macro_rules! uint_impl {
1125
1137
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1126
1138
#[ inline]
1127
1139
pub fn is_power_of_two( self ) -> bool {
1128
- ( self . wrapping_sub( Self :: ONE ) ) & self == Self :: ZERO &&
1129
- !( self == Self :: ZERO )
1140
+ ( self . wrapping_sub( Self :: one ( ) ) ) & self == Self :: zero ( ) &&
1141
+ !( self == Self :: zero ( ) )
1130
1142
}
1131
1143
1132
1144
/// Returns the smallest power of two greater than or equal to `self`.
@@ -1135,7 +1147,7 @@ macro_rules! uint_impl {
1135
1147
#[ inline]
1136
1148
pub fn next_power_of_two( self ) -> Self {
1137
1149
let bits = size_of:: <Self >( ) * 8 ;
1138
- let one = Self :: ONE ;
1150
+ let one = Self :: one ( ) ;
1139
1151
one << ( ( bits - self . wrapping_sub( one) . leading_zeros( ) as usize ) % bits)
1140
1152
}
1141
1153
0 commit comments