@@ -769,16 +769,16 @@ pub trait ToPrimitive {
769
769
}
770
770
771
771
macro_rules! impl_to_primitive_int_to_int(
772
- ( $SrcT: ty, $DstT: ty) => (
772
+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
773
773
{
774
774
if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
775
- Some ( * self as $DstT)
775
+ Some ( $slf as $DstT)
776
776
} else {
777
- let n = * self as i64 ;
777
+ let n = $slf as i64 ;
778
778
let min_value: $DstT = Bounded :: min_value( ) ;
779
779
let max_value: $DstT = Bounded :: max_value( ) ;
780
780
if min_value as i64 <= n && n <= max_value as i64 {
781
- Some ( * self as $DstT)
781
+ Some ( $slf as $DstT)
782
782
} else {
783
783
None
784
784
}
@@ -788,12 +788,12 @@ macro_rules! impl_to_primitive_int_to_int(
788
788
)
789
789
790
790
macro_rules! impl_to_primitive_int_to_uint(
791
- ( $SrcT: ty, $DstT: ty) => (
791
+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
792
792
{
793
793
let zero: $SrcT = Zero :: zero( ) ;
794
794
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)
797
797
} else {
798
798
None
799
799
}
@@ -805,26 +805,26 @@ macro_rules! impl_to_primitive_int(
805
805
( $T: ty) => (
806
806
impl ToPrimitive for $T {
807
807
#[ 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 ) }
809
809
#[ 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 ) }
811
811
#[ 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 ) }
813
813
#[ 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 ) }
815
815
#[ 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 ) }
817
817
818
818
#[ 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 ) }
820
820
#[ 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 ) }
822
822
#[ 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 ) }
824
824
#[ 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 ) }
826
826
#[ 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 ) }
828
828
829
829
#[ inline]
830
830
fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
@@ -841,11 +841,11 @@ impl_to_primitive_int!(i32)
841
841
impl_to_primitive_int ! ( i64 )
842
842
843
843
macro_rules! impl_to_primitive_uint_to_int(
844
- ( $DstT: ty) => (
844
+ ( $DstT: ty, $slf : expr ) => (
845
845
{
846
846
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)
849
849
} else {
850
850
None
851
851
}
@@ -854,15 +854,15 @@ macro_rules! impl_to_primitive_uint_to_int(
854
854
)
855
855
856
856
macro_rules! impl_to_primitive_uint_to_uint(
857
- ( $SrcT: ty, $DstT: ty) => (
857
+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
858
858
{
859
859
if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
860
- Some ( * self as $DstT)
860
+ Some ( $slf as $DstT)
861
861
} else {
862
862
let zero: $SrcT = Zero :: zero( ) ;
863
863
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)
866
866
} else {
867
867
None
868
868
}
@@ -875,26 +875,26 @@ macro_rules! impl_to_primitive_uint(
875
875
( $T: ty) => (
876
876
impl ToPrimitive for $T {
877
877
#[ 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 ) }
879
879
#[ 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 ) }
881
881
#[ 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 ) }
883
883
#[ 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 ) }
885
885
#[ 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 ) }
887
887
888
888
#[ 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 ) }
890
890
#[ 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 ) }
892
892
#[ 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 ) }
894
894
#[ 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 ) }
896
896
#[ 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 ) }
898
898
899
899
#[ inline]
900
900
fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
@@ -911,14 +911,14 @@ impl_to_primitive_uint!(u32)
911
911
impl_to_primitive_uint ! ( u64 )
912
912
913
913
macro_rules! impl_to_primitive_float_to_float(
914
- ( $SrcT: ty, $DstT: ty) => (
914
+ ( $SrcT: ty, $DstT: ty, $slf : expr ) => (
915
915
if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
916
- Some ( * self as $DstT)
916
+ Some ( $slf as $DstT)
917
917
} else {
918
- let n = * self as f64 ;
918
+ let n = $slf as f64 ;
919
919
let max_value: $SrcT = Bounded :: max_value( ) ;
920
920
if -max_value as f64 <= n && n <= max_value as f64 {
921
- Some ( * self as $DstT)
921
+ Some ( $slf as $DstT)
922
922
} else {
923
923
None
924
924
}
@@ -952,9 +952,9 @@ macro_rules! impl_to_primitive_float(
952
952
fn to_u64( & self ) -> Option <u64 > { Some ( * self as u64 ) }
953
953
954
954
#[ 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 ) }
956
956
#[ 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 ) }
958
958
}
959
959
)
960
960
)
@@ -1104,38 +1104,38 @@ pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
1104
1104
}
1105
1105
1106
1106
macro_rules! impl_from_primitive(
1107
- ( $T: ty, $to_ty: expr ) => (
1107
+ ( $T: ty, $to_ty: ident ) => (
1108
1108
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( ) }
1123
1123
}
1124
1124
)
1125
1125
)
1126
1126
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)
1139
1139
1140
1140
/// Cast from one machine scalar to another.
1141
1141
///
0 commit comments