From 2d2cba0b26da06745aa06486c0479b6227b38f07 Mon Sep 17 00:00:00 2001 From: Flier Lu Date: Fri, 1 Mar 2019 18:24:20 +0800 Subject: [PATCH] extract int_to_from_bytes_impl macro --- src/int.rs | 97 +++++++----------------------------------------- src/ops/bytes.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 84 deletions(-) diff --git a/src/int.rs b/src/int.rs index ab9853c..d7bda34 100644 --- a/src/int.rs +++ b/src/int.rs @@ -1,4 +1,3 @@ -use core::mem::transmute; use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr}; use bounds::Bounded; @@ -284,7 +283,7 @@ pub trait PrimInt: } macro_rules! prim_int_impl { - ($T:ty, $S:ty, $U:ty, $L:expr) => { + ($T:ty, $S:ty, $U:ty) => { impl PrimInt for $T { #[inline] fn count_ones(self) -> u32 { @@ -366,91 +365,21 @@ macro_rules! prim_int_impl { <$T>::pow(self, exp) } } - - #[cfg(feature = "int_to_from_bytes")] - impl IntToFromBytes for $T { - type Bytes = [u8; $L]; - - #[inline] - fn to_be_bytes(self) -> Self::Bytes { - <$T>::to_be_bytes(self) - } - - #[inline] - fn to_le_bytes(self) -> Self::Bytes { - <$T>::to_le_bytes(self) - } - - #[inline] - fn to_ne_bytes(self) -> Self::Bytes { - <$T>::to_ne_bytes(self) - } - - #[inline] - fn from_be_bytes(bytes: Self::Bytes) -> Self { - <$T>::from_be_bytes(bytes) - } - - #[inline] - fn from_le_bytes(bytes: Self::Bytes) -> Self { - <$T>::from_le_bytes(bytes) - } - - #[inline] - fn from_ne_bytes(bytes: Self::Bytes) -> Self { - <$T>::from_ne_bytes(bytes) - } - } - - #[cfg(not(feature = "int_to_from_bytes"))] - impl IntToFromBytes for $T { - type Bytes = [u8; $L]; - - #[inline] - fn to_be_bytes(self) -> Self::Bytes { - <$T>::to_ne_bytes(<$T>::to_be(self)) - } - - #[inline] - fn to_le_bytes(self) -> Self::Bytes { - <$T>::to_ne_bytes(<$T>::to_le(self)) - } - - #[inline] - fn to_ne_bytes(self) -> Self::Bytes { - unsafe { transmute(self) } - } - - #[inline] - fn from_be_bytes(bytes: Self::Bytes) -> Self { - Self::from_be(Self::from_ne_bytes(bytes)) - } - - #[inline] - fn from_le_bytes(bytes: Self::Bytes) -> Self { - Self::from_le(Self::from_ne_bytes(bytes)) - } - - #[inline] - fn from_ne_bytes(bytes: Self::Bytes) -> Self { - unsafe { transmute(bytes) } - } - } }; } // prim_int_impl!(type, signed, unsigned); -prim_int_impl!(u8, i8, u8, 1); -prim_int_impl!(u16, i16, u16, 2); -prim_int_impl!(u32, i32, u32, 4); -prim_int_impl!(u64, i64, u64, 8); +prim_int_impl!(u8, i8, u8); +prim_int_impl!(u16, i16, u16); +prim_int_impl!(u32, i32, u32); +prim_int_impl!(u64, i64, u64); #[cfg(has_i128)] -prim_int_impl!(u128, i128, u128, 16); -prim_int_impl!(usize, isize, usize, 8); -prim_int_impl!(i8, i8, u8, 1); -prim_int_impl!(i16, i16, u16, 2); -prim_int_impl!(i32, i32, u32, 4); -prim_int_impl!(i64, i64, u64, 8); +prim_int_impl!(u128, i128, u128); +prim_int_impl!(usize, isize, usize); +prim_int_impl!(i8, i8, u8); +prim_int_impl!(i16, i16, u16); +prim_int_impl!(i32, i32, u32); +prim_int_impl!(i64, i64, u64); #[cfg(has_i128)] -prim_int_impl!(i128, i128, u128, 16); -prim_int_impl!(isize, isize, usize, 8); +prim_int_impl!(i128, i128, u128); +prim_int_impl!(isize, isize, usize); diff --git a/src/ops/bytes.rs b/src/ops/bytes.rs index b07d3e0..802f0cf 100644 --- a/src/ops/bytes.rs +++ b/src/ops/bytes.rs @@ -1,3 +1,5 @@ +use core::mem::transmute; + pub trait IntToFromBytes { type Bytes; @@ -93,3 +95,93 @@ pub trait IntToFromBytes { /// ``` fn from_ne_bytes(bytes: Self::Bytes) -> Self; } + +macro_rules! int_to_from_bytes_impl { + ($T:ty, $L:expr) => { + #[cfg(feature = "int_to_from_bytes")] + impl IntToFromBytes for $T { + type Bytes = [u8; $L]; + + #[inline] + fn to_be_bytes(self) -> Self::Bytes { + <$T>::to_be_bytes(self) + } + + #[inline] + fn to_le_bytes(self) -> Self::Bytes { + <$T>::to_le_bytes(self) + } + + #[inline] + fn to_ne_bytes(self) -> Self::Bytes { + <$T>::to_ne_bytes(self) + } + + #[inline] + fn from_be_bytes(bytes: Self::Bytes) -> Self { + <$T>::from_be_bytes(bytes) + } + + #[inline] + fn from_le_bytes(bytes: Self::Bytes) -> Self { + <$T>::from_le_bytes(bytes) + } + + #[inline] + fn from_ne_bytes(bytes: Self::Bytes) -> Self { + <$T>::from_ne_bytes(bytes) + } + } + + #[cfg(not(feature = "int_to_from_bytes"))] + impl IntToFromBytes for $T { + type Bytes = [u8; $L]; + + #[inline] + fn to_be_bytes(self) -> Self::Bytes { + <$T>::to_ne_bytes(<$T>::to_be(self)) + } + + #[inline] + fn to_le_bytes(self) -> Self::Bytes { + <$T>::to_ne_bytes(<$T>::to_le(self)) + } + + #[inline] + fn to_ne_bytes(self) -> Self::Bytes { + unsafe { transmute(self) } + } + + #[inline] + fn from_be_bytes(bytes: Self::Bytes) -> Self { + Self::from_be(Self::from_ne_bytes(bytes)) + } + + #[inline] + fn from_le_bytes(bytes: Self::Bytes) -> Self { + Self::from_le(Self::from_ne_bytes(bytes)) + } + + #[inline] + fn from_ne_bytes(bytes: Self::Bytes) -> Self { + unsafe { transmute(bytes) } + } + } + }; +} + +// int_to_from_bytes_impl!(type, signed, unsigned); +int_to_from_bytes_impl!(u8, 1); +int_to_from_bytes_impl!(u16, 2); +int_to_from_bytes_impl!(u32, 4); +int_to_from_bytes_impl!(u64, 8); +#[cfg(has_i128)] +int_to_from_bytes_impl!(u128, 16); +int_to_from_bytes_impl!(usize, 8); +int_to_from_bytes_impl!(i8, 1); +int_to_from_bytes_impl!(i16, 2); +int_to_from_bytes_impl!(i32, 4); +int_to_from_bytes_impl!(i64, 8); +#[cfg(has_i128)] +int_to_from_bytes_impl!(i128, 16); +int_to_from_bytes_impl!(isize, 8);