@@ -171,6 +171,28 @@ macro_rules! impl_fmt_trait {
171
171
} ;
172
172
}
173
173
174
+ trait GetFallback : Sized {
175
+ #[ inline( always) ]
176
+ fn get ( self ) -> Self {
177
+ self
178
+ }
179
+ }
180
+
181
+ impl GetFallback for f32 { }
182
+ impl GetFallback for f64 { }
183
+ impl GetFallback for i8 { }
184
+ impl GetFallback for i16 { }
185
+ impl GetFallback for i32 { }
186
+ impl GetFallback for i64 { }
187
+ impl GetFallback for i128 { }
188
+ impl GetFallback for isize { }
189
+ impl GetFallback for u8 { }
190
+ impl GetFallback for u16 { }
191
+ impl GetFallback for u32 { }
192
+ impl GetFallback for u64 { }
193
+ impl GetFallback for u128 { }
194
+ impl GetFallback for usize { }
195
+
174
196
macro_rules! impl_fmt_traits {
175
197
( $name: ident, $native: ident, "floating point number" ) => {
176
198
impl_fmt_trait!( $name, $native, Display ) ;
@@ -215,8 +237,8 @@ macro_rules! impl_ops_traits {
215
237
impl_ops_traits!( @without_byteorder_swap $name, $native, BitAnd , bitand, BitAndAssign , bitand_assign) ;
216
238
impl_ops_traits!( @without_byteorder_swap $name, $native, BitOr , bitor, BitOrAssign , bitor_assign) ;
217
239
impl_ops_traits!( @without_byteorder_swap $name, $native, BitXor , bitxor, BitXorAssign , bitxor_assign) ;
218
- impl_ops_traits!( @with_byteorder_swap $name, $native, Shl , shl, ShlAssign , shl_assign) ;
219
- impl_ops_traits!( @with_byteorder_swap $name, $native, Shr , shr, ShrAssign , shr_assign) ;
240
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Shl , < { Self } > , shl, ShlAssign , shl_assign) ;
241
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Shr , < { Self } > , shr, ShrAssign , shr_assign) ;
220
242
221
243
impl <O > core:: ops:: Not for $name<O > {
222
244
type Output = $name<O >;
@@ -241,6 +263,13 @@ macro_rules! impl_ops_traits {
241
263
self . get( ) . cmp( & other. get( ) )
242
264
}
243
265
}
266
+
267
+ impl <O : ByteOrder > PartialOrd <$native> for $name<O > {
268
+ #[ inline( always) ]
269
+ fn partial_cmp( & self , other: & $native) -> Option <Ordering > {
270
+ self . get( ) . partial_cmp( other)
271
+ }
272
+ }
244
273
} ;
245
274
( $name: ident, $native: ident, @signed_integer_floating_point) => {
246
275
impl <O : ByteOrder > core:: ops:: Neg for $name<O > {
@@ -255,31 +284,33 @@ macro_rules! impl_ops_traits {
255
284
}
256
285
} ;
257
286
( $name: ident, $native: ident, @all_types) => {
258
- impl_ops_traits!( @with_byteorder_swap $name, $native, Add , add, AddAssign , add_assign) ;
259
- impl_ops_traits!( @with_byteorder_swap $name, $native, Div , div, DivAssign , div_assign) ;
260
- impl_ops_traits!( @with_byteorder_swap $name, $native, Mul , mul, MulAssign , mul_assign) ;
261
- impl_ops_traits!( @with_byteorder_swap $name, $native, Rem , rem, RemAssign , rem_assign) ;
262
- impl_ops_traits!( @with_byteorder_swap $name, $native, Sub , sub, SubAssign , sub_assign) ;
287
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Add , < { Self , $native } > , add, AddAssign , add_assign) ;
288
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Div , < { Self , $native } > , div, DivAssign , div_assign) ;
289
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Mul , < { Self , $native } > , mul, MulAssign , mul_assign) ;
290
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Rem , < { Self , $native } > , rem, RemAssign , rem_assign) ;
291
+ impl_ops_traits!( @with_byteorder_swap $name, $native, Sub , < { Self , $native } > , sub, SubAssign , sub_assign) ;
263
292
} ;
264
- ( @with_byteorder_swap $name: ident, $native: ident, $trait: ident, $method: ident, $trait_assign: ident, $method_assign: ident) => {
265
- impl <O : ByteOrder > core:: ops:: $trait for $name<O > {
266
- type Output = $name<O >;
293
+ ( @with_byteorder_swap $name: ident, $native: ident, $trait: ident, <{ $( $rhs: ident) ,* } >, $method: ident, $trait_assign: ident, $method_assign: ident) => {
294
+ $(
295
+ impl <O : ByteOrder > core:: ops:: $trait<$rhs> for $name<O > {
296
+ type Output = $name<O >;
267
297
268
- #[ inline( always) ]
269
- fn $method( self , rhs: $name<O >) -> $name<O > {
270
- let self_native: $native = self . get( ) ;
271
- let rhs_native: $native = rhs. get( ) ;
272
- let result_native = core:: ops:: $trait:: $method( self_native, rhs_native) ;
273
- $name:: <O >:: new( result_native)
298
+ #[ inline( always) ]
299
+ fn $method( self , rhs: $rhs) -> $name<O > {
300
+ let self_native: $native = self . get( ) ;
301
+ let rhs_native: $native = rhs. get( ) ;
302
+ let result_native = core:: ops:: $trait:: $method( self_native, rhs_native) ;
303
+ $name:: <O >:: new( result_native)
304
+ }
274
305
}
275
- }
276
306
277
- impl <O : ByteOrder > core:: ops:: $trait_assign for $name<O > {
278
- #[ inline( always) ]
279
- fn $method_assign( & mut self , rhs: $name<O >) {
280
- * self = core:: ops:: $trait:: $method( * self , rhs) ;
307
+ impl <O : ByteOrder > core:: ops:: $trait_assign<$rhs> for $name<O > {
308
+ #[ inline( always) ]
309
+ fn $method_assign( & mut self , rhs: $rhs) {
310
+ * self = core:: ops:: $trait:: $method( * self , rhs) ;
311
+ }
281
312
}
282
- }
313
+ ) *
283
314
} ;
284
315
// Implement traits in terms of the same trait on the native type, but
285
316
// without performing a byte order swap. This only works for bitwise
@@ -576,6 +607,13 @@ example of how it can be used for parsing UDP packets.
576
607
}
577
608
}
578
609
610
+ impl <O : ByteOrder > PartialEq <$native> for $name<O > {
611
+ #[ inline( always) ]
612
+ fn eq( & self , other: & $native) -> bool {
613
+ self . get( ) . eq( other)
614
+ }
615
+ }
616
+
579
617
impl_fmt_traits!( $name, $native, $number_kind) ;
580
618
impl_ops_traits!( $name, $native, $number_kind) ;
581
619
0 commit comments