@@ -53,6 +53,8 @@ pub mod flt2dec;
5353/// `x + T::ZERO == x`.
5454#[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
5555pub trait Zero {
56+ fn zero ( ) -> Self ;
57+
5658 /// The "zero" (usually, additive identity) for this type.
5759 const ZERO : Self ;
5860}
@@ -63,16 +65,22 @@ pub trait Zero {
6365/// `x * T::ONE == x`.
6466#[ unstable( feature = "zero_one" , reason = "unsure of placement" ) ]
6567pub trait One {
68+ fn one ( ) -> Self ;
69+
6670 /// The "one" (usually, multiplicative identity) for this type.
6771 const ONE : Self ;
6872}
6973
7074macro_rules! zero_one_impl {
7175 ( $( $t: ty) * ) => ( $(
7276 impl Zero for $t {
77+ fn zero( ) -> Self { Self :: ZERO }
78+
7379 const ZERO : $t = 0 ;
7480 }
7581 impl One for $t {
82+ fn one( ) -> Self { Self :: ONE }
83+
7684 const ONE : $t = 1 ;
7785 }
7886 ) * )
@@ -82,9 +90,13 @@ zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
8290macro_rules! zero_one_impl_float {
8391 ( $( $t: ty) * ) => ( $(
8492 impl Zero for $t {
93+ fn zero( ) -> Self { Self :: ZERO }
94+
8595 const ZERO : $t = 0.0 ;
8696 }
8797 impl One for $t {
98+ fn one( ) -> Self { Self :: ONE }
99+
88100 const ONE : $t = 1.0 ;
89101 }
90102 ) * )
@@ -409,7 +421,7 @@ macro_rules! int_impl {
409421 pub fn saturating_add( self , other: Self ) -> Self {
410422 match self . checked_add( other) {
411423 Some ( x) => x,
412- None if other >= Self :: ZERO => Self :: max_value( ) ,
424+ None if other >= Self :: zero ( ) => Self :: max_value( ) ,
413425 None => Self :: min_value( ) ,
414426 }
415427 }
@@ -421,7 +433,7 @@ macro_rules! int_impl {
421433 pub fn saturating_sub( self , other: Self ) -> Self {
422434 match self . checked_sub( other) {
423435 Some ( x) => x,
424- None if other >= Self :: ZERO => Self :: min_value( ) ,
436+ None if other >= Self :: zero ( ) => Self :: min_value( ) ,
425437 None => Self :: max_value( ) ,
426438 }
427439 }
@@ -529,7 +541,7 @@ macro_rules! int_impl {
529541 #[ inline]
530542 pub fn pow( self , mut exp: u32 ) -> Self {
531543 let mut base = self ;
532- let mut acc = Self :: ONE ;
544+ let mut acc = Self :: one ( ) ;
533545
534546 let mut prev_base = self ;
535547 let mut base_oflo = false ;
@@ -979,7 +991,7 @@ macro_rules! uint_impl {
979991 pub fn saturating_add( self , other: Self ) -> Self {
980992 match self . checked_add( other) {
981993 Some ( x) => x,
982- None if other >= Self :: ZERO => Self :: max_value( ) ,
994+ None if other >= Self :: zero ( ) => Self :: max_value( ) ,
983995 None => Self :: min_value( ) ,
984996 }
985997 }
@@ -991,7 +1003,7 @@ macro_rules! uint_impl {
9911003 pub fn saturating_sub( self , other: Self ) -> Self {
9921004 match self . checked_sub( other) {
9931005 Some ( x) => x,
994- None if other >= Self :: ZERO => Self :: min_value( ) ,
1006+ None if other >= Self :: zero ( ) => Self :: min_value( ) ,
9951007 None => Self :: max_value( ) ,
9961008 }
9971009 }
@@ -1097,7 +1109,7 @@ macro_rules! uint_impl {
10971109 #[ inline]
10981110 pub fn pow( self , mut exp: u32 ) -> Self {
10991111 let mut base = self ;
1100- let mut acc = Self :: ONE ;
1112+ let mut acc = Self :: one ( ) ;
11011113
11021114 let mut prev_base = self ;
11031115 let mut base_oflo = false ;
@@ -1125,8 +1137,8 @@ macro_rules! uint_impl {
11251137 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11261138 #[ inline]
11271139 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 ( ) )
11301142 }
11311143
11321144 /// Returns the smallest power of two greater than or equal to `self`.
@@ -1135,7 +1147,7 @@ macro_rules! uint_impl {
11351147 #[ inline]
11361148 pub fn next_power_of_two( self ) -> Self {
11371149 let bits = size_of:: <Self >( ) * 8 ;
1138- let one = Self :: ONE ;
1150+ let one = Self :: one ( ) ;
11391151 one << ( ( bits - self . wrapping_sub( one) . leading_zeros( ) as usize ) % bits)
11401152 }
11411153
0 commit comments