@@ -769,16 +769,16 @@ pub trait ToPrimitive {
769769}
770770
771771macro_rules! impl_to_primitive_int_to_int(
772- ( $SrcT: ty, $DstT: ty) => (
772+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
773773 {
774774 if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
775- Some ( * self as $DstT)
775+ Some ( $slf as $DstT)
776776 } else {
777- let n = * self as i64 ;
777+ let n = $slf as i64 ;
778778 let min_value: $DstT = Bounded :: min_value( ) ;
779779 let max_value: $DstT = Bounded :: max_value( ) ;
780780 if min_value as i64 <= n && n <= max_value as i64 {
781- Some ( * self as $DstT)
781+ Some ( $slf as $DstT)
782782 } else {
783783 None
784784 }
@@ -788,12 +788,12 @@ macro_rules! impl_to_primitive_int_to_int(
788788)
789789
790790macro_rules! impl_to_primitive_int_to_uint(
791- ( $SrcT: ty, $DstT: ty) => (
791+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
792792 {
793793 let zero: $SrcT = Zero :: zero( ) ;
794794 let max_value: $DstT = Bounded :: max_value( ) ;
795- if zero <= * self && * self as u64 <= max_value as u64 {
796- Some ( * self as $DstT)
795+ if zero <= $slf && $slf as u64 <= max_value as u64 {
796+ Some ( $slf as $DstT)
797797 } else {
798798 None
799799 }
@@ -805,26 +805,26 @@ macro_rules! impl_to_primitive_int(
805805 ( $T: ty) => (
806806 impl ToPrimitive for $T {
807807 #[ inline]
808- fn to_int( & self ) -> Option <int> { impl_to_primitive_int_to_int!( $T, int) }
808+ fn to_int( & self ) -> Option <int> { impl_to_primitive_int_to_int!( $T, int, * self ) }
809809 #[ inline]
810- fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_int_to_int!( $T, i8 ) }
810+ fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_int_to_int!( $T, i8 , * self ) }
811811 #[ inline]
812- fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_int_to_int!( $T, i16 ) }
812+ fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_int_to_int!( $T, i16 , * self ) }
813813 #[ inline]
814- fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_int_to_int!( $T, i32 ) }
814+ fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_int_to_int!( $T, i32 , * self ) }
815815 #[ inline]
816- fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_int_to_int!( $T, i64 ) }
816+ fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_int_to_int!( $T, i64 , * self ) }
817817
818818 #[ inline]
819- fn to_uint( & self ) -> Option <uint> { impl_to_primitive_int_to_uint!( $T, uint) }
819+ fn to_uint( & self ) -> Option <uint> { impl_to_primitive_int_to_uint!( $T, uint, * self ) }
820820 #[ inline]
821- fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_int_to_uint!( $T, u8 ) }
821+ fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_int_to_uint!( $T, u8 , * self ) }
822822 #[ inline]
823- fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_int_to_uint!( $T, u16 ) }
823+ fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_int_to_uint!( $T, u16 , * self ) }
824824 #[ inline]
825- fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_int_to_uint!( $T, u32 ) }
825+ fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_int_to_uint!( $T, u32 , * self ) }
826826 #[ inline]
827- fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_int_to_uint!( $T, u64 ) }
827+ fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_int_to_uint!( $T, u64 , * self ) }
828828
829829 #[ inline]
830830 fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
@@ -841,11 +841,11 @@ impl_to_primitive_int!(i32)
841841impl_to_primitive_int ! ( i64 )
842842
843843macro_rules! impl_to_primitive_uint_to_int(
844- ( $DstT: ty) => (
844+ ( $DstT: ty, $slf : expr ) => (
845845 {
846846 let max_value: $DstT = Bounded :: max_value( ) ;
847- if * self as u64 <= max_value as u64 {
848- Some ( * self as $DstT)
847+ if $slf as u64 <= max_value as u64 {
848+ Some ( $slf as $DstT)
849849 } else {
850850 None
851851 }
@@ -854,15 +854,15 @@ macro_rules! impl_to_primitive_uint_to_int(
854854)
855855
856856macro_rules! impl_to_primitive_uint_to_uint(
857- ( $SrcT: ty, $DstT: ty) => (
857+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
858858 {
859859 if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
860- Some ( * self as $DstT)
860+ Some ( $slf as $DstT)
861861 } else {
862862 let zero: $SrcT = Zero :: zero( ) ;
863863 let max_value: $DstT = Bounded :: max_value( ) ;
864- if zero <= * self && * self as u64 <= max_value as u64 {
865- Some ( * self as $DstT)
864+ if zero <= $slf && $slf as u64 <= max_value as u64 {
865+ Some ( $slf as $DstT)
866866 } else {
867867 None
868868 }
@@ -875,26 +875,26 @@ macro_rules! impl_to_primitive_uint(
875875 ( $T: ty) => (
876876 impl ToPrimitive for $T {
877877 #[ inline]
878- fn to_int( & self ) -> Option <int> { impl_to_primitive_uint_to_int!( int) }
878+ fn to_int( & self ) -> Option <int> { impl_to_primitive_uint_to_int!( int, * self ) }
879879 #[ inline]
880- fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_uint_to_int!( i8 ) }
880+ fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_uint_to_int!( i8 , * self ) }
881881 #[ inline]
882- fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_uint_to_int!( i16 ) }
882+ fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_uint_to_int!( i16 , * self ) }
883883 #[ inline]
884- fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_uint_to_int!( i32 ) }
884+ fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_uint_to_int!( i32 , * self ) }
885885 #[ inline]
886- fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_uint_to_int!( i64 ) }
886+ fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_uint_to_int!( i64 , * self ) }
887887
888888 #[ inline]
889- fn to_uint( & self ) -> Option <uint> { impl_to_primitive_uint_to_uint!( $T, uint) }
889+ fn to_uint( & self ) -> Option <uint> { impl_to_primitive_uint_to_uint!( $T, uint, * self ) }
890890 #[ inline]
891- fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_uint_to_uint!( $T, u8 ) }
891+ fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_uint_to_uint!( $T, u8 , * self ) }
892892 #[ inline]
893- fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_uint_to_uint!( $T, u16 ) }
893+ fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_uint_to_uint!( $T, u16 , * self ) }
894894 #[ inline]
895- fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_uint_to_uint!( $T, u32 ) }
895+ fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_uint_to_uint!( $T, u32 , * self ) }
896896 #[ inline]
897- fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_uint_to_uint!( $T, u64 ) }
897+ fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_uint_to_uint!( $T, u64 , * self ) }
898898
899899 #[ inline]
900900 fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
@@ -911,14 +911,14 @@ impl_to_primitive_uint!(u32)
911911impl_to_primitive_uint ! ( u64 )
912912
913913macro_rules! impl_to_primitive_float_to_float(
914- ( $SrcT: ty, $DstT: ty) => (
914+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
915915 if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
916- Some ( * self as $DstT)
916+ Some ( $slf as $DstT)
917917 } else {
918- let n = * self as f64 ;
918+ let n = $slf as f64 ;
919919 let max_value: $SrcT = Bounded :: max_value( ) ;
920920 if -max_value as f64 <= n && n <= max_value as f64 {
921- Some ( * self as $DstT)
921+ Some ( $slf as $DstT)
922922 } else {
923923 None
924924 }
@@ -952,9 +952,9 @@ macro_rules! impl_to_primitive_float(
952952 fn to_u64( & self ) -> Option <u64 > { Some ( * self as u64 ) }
953953
954954 #[ inline]
955- fn to_f32( & self ) -> Option <f32 > { impl_to_primitive_float_to_float!( $T, f32 ) }
955+ fn to_f32( & self ) -> Option <f32 > { impl_to_primitive_float_to_float!( $T, f32 , * self ) }
956956 #[ inline]
957- fn to_f64( & self ) -> Option <f64 > { impl_to_primitive_float_to_float!( $T, f64 ) }
957+ fn to_f64( & self ) -> Option <f64 > { impl_to_primitive_float_to_float!( $T, f64 , * self ) }
958958 }
959959 )
960960)
@@ -1104,38 +1104,38 @@ pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
11041104}
11051105
11061106macro_rules! impl_from_primitive(
1107- ( $T: ty, $to_ty: expr ) => (
1107+ ( $T: ty, $to_ty: ident ) => (
11081108 impl FromPrimitive for $T {
1109- #[ inline] fn from_int( n: int) -> Option <$T> { $to_ty }
1110- #[ inline] fn from_i8( n: i8 ) -> Option <$T> { $to_ty }
1111- #[ inline] fn from_i16( n: i16 ) -> Option <$T> { $to_ty }
1112- #[ inline] fn from_i32( n: i32 ) -> Option <$T> { $to_ty }
1113- #[ inline] fn from_i64( n: i64 ) -> Option <$T> { $to_ty }
1114-
1115- #[ inline] fn from_uint( n: uint) -> Option <$T> { $to_ty }
1116- #[ inline] fn from_u8( n: u8 ) -> Option <$T> { $to_ty }
1117- #[ inline] fn from_u16( n: u16 ) -> Option <$T> { $to_ty }
1118- #[ inline] fn from_u32( n: u32 ) -> Option <$T> { $to_ty }
1119- #[ inline] fn from_u64( n: u64 ) -> Option <$T> { $to_ty }
1120-
1121- #[ inline] fn from_f32( n: f32 ) -> Option <$T> { $to_ty }
1122- #[ inline] fn from_f64( n: f64 ) -> Option <$T> { $to_ty }
1109+ #[ inline] fn from_int( n: int) -> Option <$T> { n . $to_ty( ) }
1110+ #[ inline] fn from_i8( n: i8 ) -> Option <$T> { n . $to_ty( ) }
1111+ #[ inline] fn from_i16( n: i16 ) -> Option <$T> { n . $to_ty( ) }
1112+ #[ inline] fn from_i32( n: i32 ) -> Option <$T> { n . $to_ty( ) }
1113+ #[ inline] fn from_i64( n: i64 ) -> Option <$T> { n . $to_ty( ) }
1114+
1115+ #[ inline] fn from_uint( n: uint) -> Option <$T> { n . $to_ty( ) }
1116+ #[ inline] fn from_u8( n: u8 ) -> Option <$T> { n . $to_ty( ) }
1117+ #[ inline] fn from_u16( n: u16 ) -> Option <$T> { n . $to_ty( ) }
1118+ #[ inline] fn from_u32( n: u32 ) -> Option <$T> { n . $to_ty( ) }
1119+ #[ inline] fn from_u64( n: u64 ) -> Option <$T> { n . $to_ty( ) }
1120+
1121+ #[ inline] fn from_f32( n: f32 ) -> Option <$T> { n . $to_ty( ) }
1122+ #[ inline] fn from_f64( n: f64 ) -> Option <$T> { n . $to_ty( ) }
11231123 }
11241124 )
11251125)
11261126
1127- impl_from_primitive ! ( int, n . to_int( ) )
1128- impl_from_primitive ! ( i8 , n . to_i8( ) )
1129- impl_from_primitive ! ( i16 , n . to_i16( ) )
1130- impl_from_primitive ! ( i32 , n . to_i32( ) )
1131- impl_from_primitive ! ( i64 , n . to_i64( ) )
1132- impl_from_primitive ! ( uint, n . to_uint( ) )
1133- impl_from_primitive ! ( u8 , n . to_u8( ) )
1134- impl_from_primitive ! ( u16 , n . to_u16( ) )
1135- impl_from_primitive ! ( u32 , n . to_u32( ) )
1136- impl_from_primitive ! ( u64 , n . to_u64( ) )
1137- impl_from_primitive ! ( f32 , n . to_f32( ) )
1138- impl_from_primitive ! ( f64 , n . to_f64( ) )
1127+ impl_from_primitive ! ( int, to_int)
1128+ impl_from_primitive ! ( i8 , to_i8)
1129+ impl_from_primitive ! ( i16 , to_i16)
1130+ impl_from_primitive ! ( i32 , to_i32)
1131+ impl_from_primitive ! ( i64 , to_i64)
1132+ impl_from_primitive ! ( uint, to_uint)
1133+ impl_from_primitive ! ( u8 , to_u8)
1134+ impl_from_primitive ! ( u16 , to_u16)
1135+ impl_from_primitive ! ( u32 , to_u32)
1136+ impl_from_primitive ! ( u64 , to_u64)
1137+ impl_from_primitive ! ( f32 , to_f32)
1138+ impl_from_primitive ! ( f64 , to_f64)
11391139
11401140/// Cast from one machine scalar to another.
11411141///
0 commit comments