Skip to content

Commit

Permalink
[byteorder] Implement additional ops for native RHSs
Browse files Browse the repository at this point in the history
  • Loading branch information
jswrenn committed Sep 23, 2024
1 parent 73b15e5 commit 2eb5b78
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<O> core::ops::Not for $name<O> {
type Output = $name<O>;
Expand All @@ -235,7 +257,7 @@ macro_rules! impl_ops_traits {
}
}

impl<O: ByteOrder> Ord for $name<O> {
impl<O: ByteOrder> Ord<$native> for $name<O> {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.get().cmp(&other.get())
Expand All @@ -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<O: ByteOrder> core::ops::$trait for $name<O> {
type Output = $name<O>;
(@with_byteorder_swap $name:ident, $native:ident, $trait:ident, <{$($rhs:ident),*}>, $method:ident, $trait_assign:ident, $method_assign:ident) => {
$(
impl<O: ByteOrder> core::ops::$trait<$rhs> for $name<O> {
type Output = $name<O>;

#[inline(always)]
fn $method(self, rhs: $name<O>) -> $name<O> {
let self_native: $native = self.get();
let rhs_native: $native = rhs.get();
let result_native = core::ops::$trait::$method(self_native, rhs_native);
$name::<O>::new(result_native)
#[inline(always)]
fn $method(self, rhs: $rhs) -> $name<O> {
let self_native: $native = self.get();
let rhs_native: $native = rhs.get();
let result_native = core::ops::$trait::$method(self_native, rhs_native);
$name::<O>::new(result_native)
}
}
}

impl<O: ByteOrder> core::ops::$trait_assign for $name<O> {
#[inline(always)]
fn $method_assign(&mut self, rhs: $name<O>) {
*self = core::ops::$trait::$method(*self, rhs);
impl<O: ByteOrder> core::ops::$trait_assign<$rhs> for $name<O> {
#[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
Expand Down

0 comments on commit 2eb5b78

Please sign in to comment.