@@ -50,34 +50,30 @@ pub mod flt2dec;
5050/// Types that have a "zero" value.
5151///
5252/// 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" ) ]
5655pub trait Zero {
5756 /// The "zero" (usually, additive identity) for this type.
58- fn zero ( ) -> Self ;
57+ const ZERO : Self ;
5958}
6059
6160/// Types that have a "one" value.
6261///
6362/// 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" ) ]
6765pub trait One {
6866 /// The "one" (usually, multiplicative identity) for this type.
69- fn one ( ) -> Self ;
67+ const ONE : Self ;
7068}
7169
7270macro_rules! zero_one_impl {
7371 ( $( $t: ty) * ) => ( $(
7472 impl Zero for $t {
75- #[ inline]
76- fn zero( ) -> Self { 0 }
73+ const ZERO : $t = 0 ;
7774 }
7875 impl One for $t {
79- #[ inline]
80- fn one( ) -> Self { 1 }
76+ const ONE : $t = 1 ;
8177 }
8278 ) * )
8379}
@@ -86,12 +82,10 @@ zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
8682macro_rules! zero_one_impl_float {
8783 ( $( $t: ty) * ) => ( $(
8884 impl Zero for $t {
89- #[ inline]
90- fn zero( ) -> Self { 0.0 }
85+ const ZERO : $t = 0.0 ;
9186 }
9287 impl One for $t {
93- #[ inline]
94- fn one( ) -> Self { 1.0 }
88+ const ONE : $t = 1.0 ;
9589 }
9690 ) * )
9791}
@@ -415,7 +409,7 @@ macro_rules! int_impl {
415409 pub fn saturating_add( self , other: Self ) -> Self {
416410 match self . checked_add( other) {
417411 Some ( x) => x,
418- None if other >= Self :: zero ( ) => Self :: max_value( ) ,
412+ None if other >= Self :: ZERO => Self :: max_value( ) ,
419413 None => Self :: min_value( ) ,
420414 }
421415 }
@@ -427,7 +421,7 @@ macro_rules! int_impl {
427421 pub fn saturating_sub( self , other: Self ) -> Self {
428422 match self . checked_sub( other) {
429423 Some ( x) => x,
430- None if other >= Self :: zero ( ) => Self :: min_value( ) ,
424+ None if other >= Self :: ZERO => Self :: min_value( ) ,
431425 None => Self :: max_value( ) ,
432426 }
433427 }
@@ -535,7 +529,7 @@ macro_rules! int_impl {
535529 #[ inline]
536530 pub fn pow( self , mut exp: u32 ) -> Self {
537531 let mut base = self ;
538- let mut acc = Self :: one ( ) ;
532+ let mut acc = Self :: ONE ;
539533
540534 let mut prev_base = self ;
541535 let mut base_oflo = false ;
@@ -985,7 +979,7 @@ macro_rules! uint_impl {
985979 pub fn saturating_add( self , other: Self ) -> Self {
986980 match self . checked_add( other) {
987981 Some ( x) => x,
988- None if other >= Self :: zero ( ) => Self :: max_value( ) ,
982+ None if other >= Self :: ZERO => Self :: max_value( ) ,
989983 None => Self :: min_value( ) ,
990984 }
991985 }
@@ -997,7 +991,7 @@ macro_rules! uint_impl {
997991 pub fn saturating_sub( self , other: Self ) -> Self {
998992 match self . checked_sub( other) {
999993 Some ( x) => x,
1000- None if other >= Self :: zero ( ) => Self :: min_value( ) ,
994+ None if other >= Self :: ZERO => Self :: min_value( ) ,
1001995 None => Self :: max_value( ) ,
1002996 }
1003997 }
@@ -1103,7 +1097,7 @@ macro_rules! uint_impl {
11031097 #[ inline]
11041098 pub fn pow( self , mut exp: u32 ) -> Self {
11051099 let mut base = self ;
1106- let mut acc = Self :: one ( ) ;
1100+ let mut acc = Self :: ONE ;
11071101
11081102 let mut prev_base = self ;
11091103 let mut base_oflo = false ;
@@ -1131,8 +1125,8 @@ macro_rules! uint_impl {
11311125 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11321126 #[ inline]
11331127 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 )
11361130 }
11371131
11381132 /// Returns the smallest power of two greater than or equal to `self`.
@@ -1141,7 +1135,7 @@ macro_rules! uint_impl {
11411135 #[ inline]
11421136 pub fn next_power_of_two( self ) -> Self {
11431137 let bits = size_of:: <Self >( ) * 8 ;
1144- let one: Self = Self :: one ( ) ;
1138+ let one = Self :: ONE ;
11451139 one << ( ( bits - self . wrapping_sub( one) . leading_zeros( ) as usize ) % bits)
11461140 }
11471141
0 commit comments