diff --git a/src/byteorder.rs b/src/byteorder.rs index adcfb3a6079..fd38cc3e02b 100644 --- a/src/byteorder.rs +++ b/src/byteorder.rs @@ -171,6 +171,28 @@ macro_rules! impl_fmt_trait { }; } +trait GetFallback: Sized { + #[inline(always)] + fn get(self) -> Self { + self + } +} + +impl GetFallback for f32 {} +impl GetFallback for f64 {} +impl GetFallback for i8 {} +impl GetFallback for i16 {} +impl GetFallback for i32 {} +impl GetFallback for i64 {} +impl GetFallback for i128 {} +impl GetFallback for isize {} +impl GetFallback for u8 {} +impl GetFallback for u16 {} +impl GetFallback for u32 {} +impl GetFallback for u64 {} +impl GetFallback for u128 {} +impl GetFallback for usize {} + macro_rules! impl_fmt_traits { ($name:ident, $native:ident, "floating point number") => { impl_fmt_trait!($name, $native, Display); @@ -215,8 +237,8 @@ macro_rules! impl_ops_traits { impl_ops_traits!(@without_byteorder_swap $name, $native, BitAnd, bitand, BitAndAssign, bitand_assign); impl_ops_traits!(@without_byteorder_swap $name, $native, BitOr, bitor, BitOrAssign, bitor_assign); impl_ops_traits!(@without_byteorder_swap $name, $native, BitXor, bitxor, BitXorAssign, bitxor_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Shl, shl, ShlAssign, shl_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Shr, shr, ShrAssign, shr_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Shl, <{Self}>, shl, ShlAssign, shl_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Shr, <{Self}>, shr, ShrAssign, shr_assign); impl core::ops::Not for $name { type Output = $name; @@ -235,7 +257,7 @@ macro_rules! impl_ops_traits { } } - impl Ord for $name { + impl Ord<$native> for $name { #[inline(always)] fn cmp(&self, other: &Self) -> Ordering { self.get().cmp(&other.get()) @@ -255,31 +277,33 @@ macro_rules! impl_ops_traits { } }; ($name:ident, $native:ident, @all_types) => { - impl_ops_traits!(@with_byteorder_swap $name, $native, Add, add, AddAssign, add_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Div, div, DivAssign, div_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Mul, mul, MulAssign, mul_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Rem, rem, RemAssign, rem_assign); - impl_ops_traits!(@with_byteorder_swap $name, $native, Sub, sub, SubAssign, sub_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Add, <{Self, $native}>, add, AddAssign, add_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Div, <{Self, $native}>, div, DivAssign, div_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Mul, <{Self, $native}>, mul, MulAssign, mul_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Rem, <{Self, $native}>, rem, RemAssign, rem_assign); + impl_ops_traits!(@with_byteorder_swap $name, $native, Sub, <{Self, $native}>, sub, SubAssign, sub_assign); }; - (@with_byteorder_swap $name:ident, $native:ident, $trait:ident, $method:ident, $trait_assign:ident, $method_assign:ident) => { - impl core::ops::$trait for $name { - type Output = $name; + (@with_byteorder_swap $name:ident, $native:ident, $trait:ident, <{$($rhs:ident),*}>, $method:ident, $trait_assign:ident, $method_assign:ident) => { + $( + impl core::ops::$trait<$rhs> for $name { + type Output = $name; - #[inline(always)] - fn $method(self, rhs: $name) -> $name { - let self_native: $native = self.get(); - let rhs_native: $native = rhs.get(); - let result_native = core::ops::$trait::$method(self_native, rhs_native); - $name::::new(result_native) + #[inline(always)] + fn $method(self, rhs: $rhs) -> $name { + let self_native: $native = self.get(); + let rhs_native: $native = rhs.get(); + let result_native = core::ops::$trait::$method(self_native, rhs_native); + $name::::new(result_native) + } } - } - impl core::ops::$trait_assign for $name { - #[inline(always)] - fn $method_assign(&mut self, rhs: $name) { - *self = core::ops::$trait::$method(*self, rhs); + impl core::ops::$trait_assign<$rhs> for $name { + #[inline(always)] + fn $method_assign(&mut self, rhs: $rhs) { + *self = core::ops::$trait::$method(*self, rhs); + } } - } + )* }; // Implement traits in terms of the same trait on the native type, but // without performing a byte order swap. This only works for bitwise