@@ -50,34 +50,30 @@ pub mod flt2dec;
50
50
/// Types that have a "zero" value.
51
51
///
52
52
/// This trait is intended for use in conjunction with `Add`, as an identity:
53
- /// `x + T::zero() == x`.
54
- #[ unstable( feature = "zero_one" ,
55
- reason = "unsure of placement, wants to use associated constants" ) ]
53
+ /// `x + T::ZERO == x`.
54
+ #[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
56
55
pub trait Zero {
57
56
/// The "zero" (usually, additive identity) for this type.
58
- fn zero ( ) -> Self ;
57
+ const ZERO : Self ;
59
58
}
60
59
61
60
/// Types that have a "one" value.
62
61
///
63
62
/// This trait is intended for use in conjunction with `Mul`, as an identity:
64
- /// `x * T::one() == x`.
65
- #[ unstable( feature = "zero_one" ,
66
- reason = "unsure of placement, wants to use associated constants" ) ]
63
+ /// `x * T::ONE == x`.
64
+ #[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
67
65
pub trait One {
68
66
/// The "one" (usually, multiplicative identity) for this type.
69
- fn one ( ) -> Self ;
67
+ const ONE : Self ;
70
68
}
71
69
72
70
macro_rules! zero_one_impl {
73
71
( $( $t: ty) * ) => ( $(
74
72
impl Zero for $t {
75
- #[ inline]
76
- fn zero( ) -> Self { 0 }
73
+ const ZERO : $t = 0 ;
77
74
}
78
75
impl One for $t {
79
- #[ inline]
80
- fn one( ) -> Self { 1 }
76
+ const ONE : $t = 1 ;
81
77
}
82
78
) * )
83
79
}
@@ -86,12 +82,10 @@ zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
86
82
macro_rules! zero_one_impl_float {
87
83
( $( $t: ty) * ) => ( $(
88
84
impl Zero for $t {
89
- #[ inline]
90
- fn zero( ) -> Self { 0.0 }
85
+ const ZERO : $t = 0.0 ;
91
86
}
92
87
impl One for $t {
93
- #[ inline]
94
- fn one( ) -> Self { 1.0 }
88
+ const ONE : $t = 1.0 ;
95
89
}
96
90
) * )
97
91
}
@@ -415,7 +409,7 @@ macro_rules! int_impl {
415
409
pub fn saturating_add( self , other: Self ) -> Self {
416
410
match self . checked_add( other) {
417
411
Some ( x) => x,
418
- None if other >= Self :: zero ( ) => Self :: max_value( ) ,
412
+ None if other >= Self :: ZERO => Self :: max_value( ) ,
419
413
None => Self :: min_value( ) ,
420
414
}
421
415
}
@@ -427,7 +421,7 @@ macro_rules! int_impl {
427
421
pub fn saturating_sub( self , other: Self ) -> Self {
428
422
match self . checked_sub( other) {
429
423
Some ( x) => x,
430
- None if other >= Self :: zero ( ) => Self :: min_value( ) ,
424
+ None if other >= Self :: ZERO => Self :: min_value( ) ,
431
425
None => Self :: max_value( ) ,
432
426
}
433
427
}
@@ -535,7 +529,7 @@ macro_rules! int_impl {
535
529
#[ inline]
536
530
pub fn pow( self , mut exp: u32 ) -> Self {
537
531
let mut base = self ;
538
- let mut acc = Self :: one ( ) ;
532
+ let mut acc = Self :: ONE ;
539
533
540
534
let mut prev_base = self ;
541
535
let mut base_oflo = false ;
@@ -985,7 +979,7 @@ macro_rules! uint_impl {
985
979
pub fn saturating_add( self , other: Self ) -> Self {
986
980
match self . checked_add( other) {
987
981
Some ( x) => x,
988
- None if other >= Self :: zero ( ) => Self :: max_value( ) ,
982
+ None if other >= Self :: ZERO => Self :: max_value( ) ,
989
983
None => Self :: min_value( ) ,
990
984
}
991
985
}
@@ -997,7 +991,7 @@ macro_rules! uint_impl {
997
991
pub fn saturating_sub( self , other: Self ) -> Self {
998
992
match self . checked_sub( other) {
999
993
Some ( x) => x,
1000
- None if other >= Self :: zero ( ) => Self :: min_value( ) ,
994
+ None if other >= Self :: ZERO => Self :: min_value( ) ,
1001
995
None => Self :: max_value( ) ,
1002
996
}
1003
997
}
@@ -1103,7 +1097,7 @@ macro_rules! uint_impl {
1103
1097
#[ inline]
1104
1098
pub fn pow( self , mut exp: u32 ) -> Self {
1105
1099
let mut base = self ;
1106
- let mut acc = Self :: one ( ) ;
1100
+ let mut acc = Self :: ONE ;
1107
1101
1108
1102
let mut prev_base = self ;
1109
1103
let mut base_oflo = false ;
@@ -1131,8 +1125,8 @@ macro_rules! uint_impl {
1131
1125
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1132
1126
#[ inline]
1133
1127
pub fn is_power_of_two( self ) -> bool {
1134
- ( self . wrapping_sub( Self :: one ( ) ) ) & self == Self :: zero ( ) &&
1135
- !( self == Self :: zero ( ) )
1128
+ ( self . wrapping_sub( Self :: ONE ) ) & self == Self :: ZERO &&
1129
+ !( self == Self :: ZERO )
1136
1130
}
1137
1131
1138
1132
/// Returns the smallest power of two greater than or equal to `self`.
@@ -1141,7 +1135,7 @@ macro_rules! uint_impl {
1141
1135
#[ inline]
1142
1136
pub fn next_power_of_two( self ) -> Self {
1143
1137
let bits = size_of:: <Self >( ) * 8 ;
1144
- let one: Self = Self :: one ( ) ;
1138
+ let one = Self :: ONE ;
1145
1139
one << ( ( bits - self . wrapping_sub( one) . leading_zeros( ) as usize ) % bits)
1146
1140
}
1147
1141
0 commit comments