Skip to content

Commit 2eb5b78

Browse files
committed
[byteorder] Implement additional ops for native RHSs
1 parent 73b15e5 commit 2eb5b78

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

src/byteorder.rs

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ macro_rules! impl_fmt_trait {
171171
};
172172
}
173173

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+
174196
macro_rules! impl_fmt_traits {
175197
($name:ident, $native:ident, "floating point number") => {
176198
impl_fmt_trait!($name, $native, Display);
@@ -215,8 +237,8 @@ macro_rules! impl_ops_traits {
215237
impl_ops_traits!(@without_byteorder_swap $name, $native, BitAnd, bitand, BitAndAssign, bitand_assign);
216238
impl_ops_traits!(@without_byteorder_swap $name, $native, BitOr, bitor, BitOrAssign, bitor_assign);
217239
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);
220242

221243
impl<O> core::ops::Not for $name<O> {
222244
type Output = $name<O>;
@@ -235,7 +257,7 @@ macro_rules! impl_ops_traits {
235257
}
236258
}
237259

238-
impl<O: ByteOrder> Ord for $name<O> {
260+
impl<O: ByteOrder> Ord<$native> for $name<O> {
239261
#[inline(always)]
240262
fn cmp(&self, other: &Self) -> Ordering {
241263
self.get().cmp(&other.get())
@@ -255,31 +277,33 @@ macro_rules! impl_ops_traits {
255277
}
256278
};
257279
($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);
280+
impl_ops_traits!(@with_byteorder_swap $name, $native, Add, <{Self, $native}>, add, AddAssign, add_assign);
281+
impl_ops_traits!(@with_byteorder_swap $name, $native, Div, <{Self, $native}>, div, DivAssign, div_assign);
282+
impl_ops_traits!(@with_byteorder_swap $name, $native, Mul, <{Self, $native}>, mul, MulAssign, mul_assign);
283+
impl_ops_traits!(@with_byteorder_swap $name, $native, Rem, <{Self, $native}>, rem, RemAssign, rem_assign);
284+
impl_ops_traits!(@with_byteorder_swap $name, $native, Sub, <{Self, $native}>, sub, SubAssign, sub_assign);
263285
};
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>;
286+
(@with_byteorder_swap $name:ident, $native:ident, $trait:ident, <{$($rhs:ident),*}>, $method:ident, $trait_assign:ident, $method_assign:ident) => {
287+
$(
288+
impl<O: ByteOrder> core::ops::$trait<$rhs> for $name<O> {
289+
type Output = $name<O>;
267290

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)
291+
#[inline(always)]
292+
fn $method(self, rhs: $rhs) -> $name<O> {
293+
let self_native: $native = self.get();
294+
let rhs_native: $native = rhs.get();
295+
let result_native = core::ops::$trait::$method(self_native, rhs_native);
296+
$name::<O>::new(result_native)
297+
}
274298
}
275-
}
276299

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);
300+
impl<O: ByteOrder> core::ops::$trait_assign<$rhs> for $name<O> {
301+
#[inline(always)]
302+
fn $method_assign(&mut self, rhs: $rhs) {
303+
*self = core::ops::$trait::$method(*self, rhs);
304+
}
281305
}
282-
}
306+
)*
283307
};
284308
// Implement traits in terms of the same trait on the native type, but
285309
// without performing a byte order swap. This only works for bitwise

0 commit comments

Comments
 (0)