@@ -4,8 +4,6 @@ use core::{f32, f64};
4
4
use core:: mem:: size_of;
5
5
use core:: num:: Wrapping ;
6
6
7
- use identities:: Zero ;
8
- use bounds:: Bounded ;
9
7
use float:: FloatCore ;
10
8
11
9
/// A generic trait for converting a value to a number.
@@ -79,62 +77,52 @@ pub trait ToPrimitive {
79
77
}
80
78
81
79
macro_rules! impl_to_primitive_int_to_int {
82
- ( $SrcT: ty, $DstT: ty, $slf: expr) => (
83
- {
84
- if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
85
- Some ( $slf as $DstT)
80
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
81
+ #[ inline]
82
+ fn $method( & self ) -> Option <$DstT> {
83
+ let min = $DstT:: MIN as $SrcT;
84
+ let max = $DstT:: MAX as $SrcT;
85
+ if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) || ( min <= * self && * self <= max) {
86
+ Some ( * self as $DstT)
86
87
} else {
87
- let n = $slf as i64 ;
88
- let min_value: $DstT = Bounded :: min_value( ) ;
89
- let max_value: $DstT = Bounded :: max_value( ) ;
90
- if min_value as i64 <= n && n <= max_value as i64 {
91
- Some ( $slf as $DstT)
92
- } else {
93
- None
94
- }
88
+ None
95
89
}
96
90
}
97
- )
91
+ ) * }
98
92
}
99
93
100
94
macro_rules! impl_to_primitive_int_to_uint {
101
- ( $SrcT: ty , $DstT : ty , $slf : expr ) => (
102
- {
103
- let zero : $SrcT = Zero :: zero ( ) ;
104
- let max_value : $DstT = Bounded :: max_value ( ) ;
105
- if zero <= $slf && $slf as u64 <= max_value as u64 {
106
- Some ( $slf as $DstT)
95
+ ( $SrcT: ident : $ ( fn $method : ident -> $DstT : ident ; ) * ) => { $ (
96
+ # [ inline ]
97
+ fn $method ( & self ) -> Option <$DstT> {
98
+ let max = $DstT :: MAX as u64 ;
99
+ if 0 <= * self && ( size_of :: <$SrcT> ( ) < size_of :: <$DstT> ( ) || * self as u64 <= max ) {
100
+ Some ( * self as $DstT)
107
101
} else {
108
102
None
109
103
}
110
104
}
111
- )
105
+ ) * }
112
106
}
113
107
114
108
macro_rules! impl_to_primitive_int {
115
- ( $T: ty ) => (
109
+ ( $T: ident ) => (
116
110
impl ToPrimitive for $T {
117
- #[ inline]
118
- fn to_isize( & self ) -> Option <isize > { impl_to_primitive_int_to_int!( $T, isize , * self ) }
119
- #[ inline]
120
- fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_int_to_int!( $T, i8 , * self ) }
121
- #[ inline]
122
- fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_int_to_int!( $T, i16 , * self ) }
123
- #[ inline]
124
- fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_int_to_int!( $T, i32 , * self ) }
125
- #[ inline]
126
- fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_int_to_int!( $T, i64 , * self ) }
111
+ impl_to_primitive_int_to_int! { $T:
112
+ fn to_isize -> isize ;
113
+ fn to_i8 -> i8 ;
114
+ fn to_i16 -> i16 ;
115
+ fn to_i32 -> i32 ;
116
+ fn to_i64 -> i64 ;
117
+ }
127
118
128
- #[ inline]
129
- fn to_usize( & self ) -> Option <usize > { impl_to_primitive_int_to_uint!( $T, usize , * self ) }
130
- #[ inline]
131
- fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_int_to_uint!( $T, u8 , * self ) }
132
- #[ inline]
133
- fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_int_to_uint!( $T, u16 , * self ) }
134
- #[ inline]
135
- fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_int_to_uint!( $T, u32 , * self ) }
136
- #[ inline]
137
- fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_int_to_uint!( $T, u64 , * self ) }
119
+ impl_to_primitive_int_to_uint! { $T:
120
+ fn to_usize -> usize ;
121
+ fn to_u8 -> u8 ;
122
+ fn to_u16 -> u16 ;
123
+ fn to_u32 -> u32 ;
124
+ fn to_u64 -> u64 ;
125
+ }
138
126
139
127
#[ inline]
140
128
fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
@@ -151,62 +139,51 @@ impl_to_primitive_int!(i32);
151
139
impl_to_primitive_int ! ( i64 ) ;
152
140
153
141
macro_rules! impl_to_primitive_uint_to_int {
154
- ( $DstT: ty, $slf: expr) => (
155
- {
156
- let max_value: $DstT = Bounded :: max_value( ) ;
157
- if $slf as u64 <= max_value as u64 {
158
- Some ( $slf as $DstT)
142
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
143
+ #[ inline]
144
+ fn $method( & self ) -> Option <$DstT> {
145
+ let max = $DstT:: MAX as u64 ;
146
+ if size_of:: <$SrcT>( ) < size_of:: <$DstT>( ) || * self as u64 <= max {
147
+ Some ( * self as $DstT)
159
148
} else {
160
149
None
161
150
}
162
151
}
163
- )
152
+ ) * }
164
153
}
165
154
166
155
macro_rules! impl_to_primitive_uint_to_uint {
167
- ( $SrcT: ty, $DstT: ty, $slf: expr) => (
168
- {
169
- if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) {
170
- Some ( $slf as $DstT)
156
+ ( $SrcT: ident : $( fn $method: ident -> $DstT: ident ; ) * ) => { $(
157
+ #[ inline]
158
+ fn $method( & self ) -> Option <$DstT> {
159
+ let max = $DstT:: MAX as $SrcT;
160
+ if size_of:: <$SrcT>( ) <= size_of:: <$DstT>( ) || * self <= max {
161
+ Some ( * self as $DstT)
171
162
} else {
172
- let zero: $SrcT = Zero :: zero( ) ;
173
- let max_value: $DstT = Bounded :: max_value( ) ;
174
- if zero <= $slf && $slf as u64 <= max_value as u64 {
175
- Some ( $slf as $DstT)
176
- } else {
177
- None
178
- }
163
+ None
179
164
}
180
165
}
181
- )
166
+ ) * }
182
167
}
183
168
184
169
macro_rules! impl_to_primitive_uint {
185
- ( $T: ty ) => (
170
+ ( $T: ident ) => (
186
171
impl ToPrimitive for $T {
187
- #[ inline]
188
- fn to_isize( & self ) -> Option <isize > { impl_to_primitive_uint_to_int!( isize , * self ) }
189
- #[ inline]
190
- fn to_i8( & self ) -> Option <i8 > { impl_to_primitive_uint_to_int!( i8 , * self ) }
191
- #[ inline]
192
- fn to_i16( & self ) -> Option <i16 > { impl_to_primitive_uint_to_int!( i16 , * self ) }
193
- #[ inline]
194
- fn to_i32( & self ) -> Option <i32 > { impl_to_primitive_uint_to_int!( i32 , * self ) }
195
- #[ inline]
196
- fn to_i64( & self ) -> Option <i64 > { impl_to_primitive_uint_to_int!( i64 , * self ) }
172
+ impl_to_primitive_uint_to_int! { $T:
173
+ fn to_isize -> isize ;
174
+ fn to_i8 -> i8 ;
175
+ fn to_i16 -> i16 ;
176
+ fn to_i32 -> i32 ;
177
+ fn to_i64 -> i64 ;
178
+ }
197
179
198
- #[ inline]
199
- fn to_usize( & self ) -> Option <usize > {
200
- impl_to_primitive_uint_to_uint!( $T, usize , * self )
180
+ impl_to_primitive_uint_to_uint! { $T:
181
+ fn to_usize -> usize ;
182
+ fn to_u8 -> u8 ;
183
+ fn to_u16 -> u16 ;
184
+ fn to_u32 -> u32 ;
185
+ fn to_u64 -> u64 ;
201
186
}
202
- #[ inline]
203
- fn to_u8( & self ) -> Option <u8 > { impl_to_primitive_uint_to_uint!( $T, u8 , * self ) }
204
- #[ inline]
205
- fn to_u16( & self ) -> Option <u16 > { impl_to_primitive_uint_to_uint!( $T, u16 , * self ) }
206
- #[ inline]
207
- fn to_u32( & self ) -> Option <u32 > { impl_to_primitive_uint_to_uint!( $T, u32 , * self ) }
208
- #[ inline]
209
- fn to_u64( & self ) -> Option <u64 > { impl_to_primitive_uint_to_uint!( $T, u64 , * self ) }
210
187
211
188
#[ inline]
212
189
fn to_f32( & self ) -> Option <f32 > { Some ( * self as f32 ) }
0 commit comments