Skip to content

Commit

Permalink
[byteorder] Implement core::ops traits
Browse files Browse the repository at this point in the history
Release 0.7.6

TODO: Tests
  • Loading branch information
joshlf committed Sep 30, 2023
1 parent b083f1f commit 9938747
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -11,7 +11,7 @@
[package]
edition = "2021"
name = "zerocopy"
version = "0.7.5"
version = "0.7.6"
authors = ["Joshua Liebow-Feeser <joshlf@google.com>"]
description = "Utilities for zero-copy parsing and serialization"
license = "BSD-2-Clause"
Expand Down Expand Up @@ -41,7 +41,7 @@ simd-nightly = ["simd"]
__internal_use_only_features_that_work_on_stable = ["alloc", "derive", "simd"]

[dependencies]
zerocopy-derive = { version = "=0.7.5", path = "zerocopy-derive", optional = true }
zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive", optional = true }

[dependencies.byteorder]
version = "1.3"
Expand All @@ -52,7 +52,7 @@ optional = true
# zerocopy-derive remain equal, even if the 'derive' feature isn't used.
# See: https://github.com/matklad/macro-dep-test
[target.'cfg(any())'.dependencies]
zerocopy-derive = { version = "=0.7.5", path = "zerocopy-derive" }
zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive" }

[dev-dependencies]
assert_matches = "1.5"
Expand All @@ -67,4 +67,4 @@ testutil = { path = "testutil" }
# CI test failures.
trybuild = "=1.0.80"
# In tests, unlike in production, zerocopy-derive is not optional
zerocopy-derive = { version = "=0.7.5", path = "zerocopy-derive" }
zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive" }
191 changes: 181 additions & 10 deletions src/byteorder.rs
Expand Up @@ -93,13 +93,12 @@ macro_rules! impl_fmt_traits {
impl_fmt_trait!($name, $native, Display);
};
($name:ident, $native:ident, "unsigned integer") => {
impl_fmt_traits!($name, $native, @all_traits);
impl_fmt_traits!($name, $native, @all_types);
};
($name:ident, $native:ident, "signed integer") => {
impl_fmt_traits!($name, $native, @all_traits);
impl_fmt_traits!($name, $native, @all_types);
};

($name:ident, $native:ident, @all_traits) => {
($name:ident, $native:ident, @all_types) => {
impl_fmt_trait!($name, $native, Display);
impl_fmt_trait!($name, $native, Octal);
impl_fmt_trait!($name, $native, LowerHex);
Expand All @@ -108,6 +107,100 @@ macro_rules! impl_fmt_traits {
};
}

macro_rules! impl_ops_traits {
($name:ident, $native:ident, "floating point number") => {
impl_ops_traits!($name, $native, @all_types);
impl_ops_traits!($name, $native, @signed_integer_floating_point);
};
($name:ident, $native:ident, "unsigned integer") => {
impl_ops_traits!($name, $native, @signed_unsigned_integer);
impl_ops_traits!($name, $native, @all_types);
};
($name:ident, $native:ident, "signed integer") => {
impl_ops_traits!($name, $native, @signed_unsigned_integer);
impl_ops_traits!($name, $native, @signed_integer_floating_point);
impl_ops_traits!($name, $native, @all_types);
};
($name:ident, $native:ident, @signed_unsigned_integer) => {
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<O> core::ops::Not for $name<O> {
type Output = $name<O>;

#[inline(always)]
fn not(self) -> $name<O> {
let self_native: $native = transmute!(self);
transmute!(!self_native)
}
}
};
($name:ident, $native:ident, @signed_integer_floating_point) => {
impl<O> core::ops::Neg for $name<O> {
type Output = $name<O>;

#[inline(always)]
fn neg(self) -> $name<O> {
let self_native: $native = transmute!(self);
transmute!(-self_native)
}
}
};
($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);
};
(@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>;

#[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)
}
}

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);
}
}
};
// Implement traits in terms of the same trait on the native type, but
// without performing a byte order swap. This only works for bitwise
// operations like `&`, `|`, etc.
(@without_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>;

#[inline(always)]
fn $method(self, rhs: $name<O>) -> $name<O> {
let self_native: $native = transmute!(self);
let rhs_native: $native = transmute!(rhs);
let result_native = core::ops::$trait::$method(self_native, rhs_native);
transmute!(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);
}
}
};
}

macro_rules! doc_comment {
($x:expr, $($tt:tt)*) => {
#[doc = $x]
Expand Down Expand Up @@ -347,6 +440,7 @@ example of how it can be used for parsing UDP packets.
}

impl_fmt_traits!($name, $native, $number_kind);
impl_ops_traits!($name, $native, $number_kind);

impl<O: ByteOrder> Debug for $name<O> {
#[inline]
Expand Down Expand Up @@ -678,27 +772,36 @@ mod tests {
impl_traits!(F32, f32, 4, signed, is_nan);
impl_traits!(F64, f64, 8, signed, is_nan);

macro_rules! call_for_all_types {
macro_rules! call_for_unsigned_types {
($fn:ident, $byteorder:ident) => {
$fn::<U16<$byteorder>>();
$fn::<U32<$byteorder>>();
$fn::<U64<$byteorder>>();
$fn::<U128<$byteorder>>();
};
}

macro_rules! call_for_signed_types {
($fn:ident, $byteorder:ident) => {
$fn::<I16<$byteorder>>();
$fn::<I32<$byteorder>>();
$fn::<I64<$byteorder>>();
$fn::<I128<$byteorder>>();
};
}

macro_rules! call_for_float_types {
($fn:ident, $byteorder:ident) => {
$fn::<F32<$byteorder>>();
$fn::<F64<$byteorder>>();
};
}

macro_rules! call_for_unsigned_types {
macro_rules! call_for_all_types {
($fn:ident, $byteorder:ident) => {
$fn::<U16<$byteorder>>();
$fn::<U32<$byteorder>>();
$fn::<U64<$byteorder>>();
$fn::<U128<$byteorder>>();
call_for_unsigned_types!($fn, $byteorder);
call_for_signed_types!($fn, $byteorder);
call_for_float_types!($fn, $byteorder);
};
}

Expand Down Expand Up @@ -834,6 +937,74 @@ mod tests {
call_for_all_types!(test_non_native_endian, NonNativeEndian);
}

#[cfg_attr(test, test)]
#[cfg_attr(kani, kani::proof)]
fn test_ops_impls() {
// Test implementations of traits in `core::ops`. Some of these are
// fairly banal, but some are optimized to perform the operation without
// swapping byte order (namely, bit-wise operations which are identical
// regardless of byte order). These are important to test, and while
// we're testing those anyway, it's trivial to test all of the impls.

fn test<T: ByteOrderType>(
op: impl Fn(T, T) -> T,
op_native: impl Fn(T::Native, T::Native) -> T::Native,
) {
let mut r = SmallRng::seed_from_u64(RNG_SEED);
for _ in 0..RAND_ITERS {
let n0 = T::Native::rand(&mut r);
let n1 = T::Native::rand(&mut r);
let t0 = T::new(n0);
let t1 = T::new(n1);

let n_res = op_native(n0, n1);
let t_res = op(t0, t1);

// For `f32` and `f64`, NaN values are not considered equal to
// themselves. We store `Option<f32>`/`Option<f64>` and store
// NaN as `None` so they can still be compared.
let n_res = (!T::Native::is_nan(n_res)).then(|| n_res);
let t_res = (!T::Native::is_nan(t_res.get())).then(|| t_res.get());
assert_eq!(n_res, t_res);
}
}

macro_rules! test {
($trait:ident, $method:ident, $($call_for_macros:ident),*) => {{
fn t<T: ByteOrderType + core::ops::$trait<Output = T>>()
where
T::Native: core::ops::$trait<Output = T::Native>,
{
test::<T>(
core::ops::$trait::$method,
core::ops::$trait::$method,
);
}

$(
$call_for_macros!(t, NativeEndian);
$call_for_macros!(t, NonNativeEndian);
)*
}};
}

test!(Add, add, call_for_all_types);
test!(Div, div, call_for_all_types);
test!(Mul, mul, call_for_all_types);
test!(Rem, rem, call_for_all_types);
test!(Sub, sub, call_for_all_types);

test!(BitAnd, bitand, call_for_unsigned_types, call_for_signed_types);
test!(BitOr, bitor, call_for_unsigned_types, call_for_signed_types);
test!(BitXor, bitxor, call_for_unsigned_types, call_for_signed_types);
test!(Shl, shl, call_for_unsigned_types, call_for_signed_types);
test!(Shr, shr, call_for_unsigned_types, call_for_signed_types);

// TODO:
// - Not (signed integers, unsigned integers)
// - Neg (signed integers, floating points)
}

#[test]
fn test_debug_impl() {
// Ensure that Debug applies format options to the inner value.
Expand Down
4 changes: 2 additions & 2 deletions zerocopy-derive/Cargo.toml
Expand Up @@ -5,7 +5,7 @@
[package]
edition = "2021"
name = "zerocopy-derive"
version = "0.7.5"
version = "0.7.6"
authors = ["Joshua Liebow-Feeser <joshlf@google.com>"]
description = "Custom derive for traits from the zerocopy crate"
license = "BSD-2-Clause"
Expand All @@ -30,4 +30,4 @@ testutil = { path = "../testutil" }
# sometimes change the output format slightly, so a version mismatch can cause
# CI test failures.
trybuild = "=1.0.80"
zerocopy = { path = "../", features = ["default", "derive"] }
zerocopy = { path = "../", features = ["default", "derive"] }

0 comments on commit 9938747

Please sign in to comment.