Skip to content

Commit 04a4952

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

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

src/byteorder.rs

Lines changed: 60 additions & 22 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>;
@@ -241,6 +263,13 @@ macro_rules! impl_ops_traits {
241263
self.get().cmp(&other.get())
242264
}
243265
}
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+
}
244273
};
245274
($name:ident, $native:ident, @signed_integer_floating_point) => {
246275
impl<O: ByteOrder> core::ops::Neg for $name<O> {
@@ -255,31 +284,33 @@ macro_rules! impl_ops_traits {
255284
}
256285
};
257286
($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);
263292
};
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>;
267297

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+
}
274305
}
275-
}
276306

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+
}
281312
}
282-
}
313+
)*
283314
};
284315
// Implement traits in terms of the same trait on the native type, but
285316
// 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.
576607
}
577608
}
578609

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+
579617
impl_fmt_traits!($name, $native, $number_kind);
580618
impl_ops_traits!($name, $native, $number_kind);
581619

0 commit comments

Comments
 (0)