diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b6e26ad82..f12e38087 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: args: -p uint --all-features - name: Test fixed-hash no_std - run: cargo test -p fixed-hash --no-default-features --features='byteorder,rustc-hex' + run: cargo test -p fixed-hash --no-default-features --features='rustc-hex' - name: Test fixed-hash all-features uses: actions-rs/cargo@v1 diff --git a/ethereum-types/Cargo.toml b/ethereum-types/Cargo.toml index 81efabaaf..110d338f1 100644 --- a/ethereum-types/Cargo.toml +++ b/ethereum-types/Cargo.toml @@ -10,9 +10,9 @@ rust-version = "1.60.0" [dependencies] ethbloom = { path = "../ethbloom", version = "0.14", optional = true, default-features = false } -fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false, features = ["byteorder", "rustc-hex"] } +fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false, features = ["rustc-hex"] } uint-crate = { path = "../uint", package = "uint", version = "0.10", default-features = false } -primitive-types = { path = "../primitive-types", version = "0.13", features = ["byteorder", "rustc-hex"], default-features = false } +primitive-types = { path = "../primitive-types", version = "0.13", features = ["rustc-hex"], default-features = false } impl-serde = { path = "../primitive-types/impls/serde", version = "0.5.0", default-features = false, optional = true } impl-rlp = { path = "../primitive-types/impls/rlp", version = "0.4", default-features = false, optional = true } impl-codec = { version = "0.7.0", path = "../primitive-types/impls/codec", default-features = false, optional = true } diff --git a/fixed-hash/Cargo.toml b/fixed-hash/Cargo.toml index b1023533b..161b1cef3 100644 --- a/fixed-hash/Cargo.toml +++ b/fixed-hash/Cargo.toml @@ -15,7 +15,6 @@ rust-version = "1.60" features = ["quickcheck", "api-dummy"] [dependencies] -byteorder = { version = "1.4.2", optional = true, default-features = false } quickcheck = { version = "1", optional = true } rand = { version = "0.8.0", optional = true, default-features = false } rustc-hex = { version = "2.0.1", optional = true, default-features = false } @@ -28,8 +27,8 @@ criterion = "0.5.1" rand = { version = "0.8.0", default-features = false, features = ["std_rng"] } [features] -default = ["std", "rand", "rustc-hex", "byteorder"] -std = ["rustc-hex/std", "rand?/std", "byteorder/std"] +default = ["std", "rand", "rustc-hex"] +std = ["rustc-hex/std", "rand?/std"] api-dummy = [] # Feature used by docs.rs to display documentation of hash types diff --git a/fixed-hash/README.md b/fixed-hash/README.md index 1974bea8f..7f38bc728 100644 --- a/fixed-hash/README.md +++ b/fixed-hash/README.md @@ -39,7 +39,7 @@ construct_fixed_hash!{ ## Features -By default this is an standard library depending crate. +By default this is an standard library depending crate. For a `#[no_std]` environment use it as follows: ``` @@ -52,17 +52,12 @@ fixed-hash = { version = "0.3", default-features = false } - Using this feature enables the following features - `rustc-hex/std` - `rand/std` - - `byteorder/std` - - Enabled by default. -- `libc`: Use `libc` for implementations of `PartialEq` and `Ord`. - Enabled by default. - `rand`: Provide API based on the `rand` crate. - Enabled by default. -- `byteorder`: Provide API based on the `byteorder` crate. - - Enabled by default. - `quickcheck`: Provide `quickcheck` implementation for hash types. - Disabled by default. -- `api-dummy`: Generate a dummy hash type for API documentation. - - Enabled by default at `docs.rs` - `arbitrary`: Allow for creation of a hash from random unstructured input. - Disabled by default. +- `api-dummy`: Generate a dummy hash type for API documentation. + - Enabled by default at `docs.rs` diff --git a/fixed-hash/benches/cmp.rs b/fixed-hash/benches/cmp.rs index fc5551e1c..38633f9f4 100644 --- a/fixed-hash/benches/cmp.rs +++ b/fixed-hash/benches/cmp.rs @@ -14,7 +14,7 @@ use fixed_hash::construct_fixed_hash; construct_fixed_hash! { pub struct H256(32); } -criterion_group!(cmp, eq_equal, eq_nonequal, compare,); +criterion_group!(cmp, eq_equal, eq_nonequal, compare); criterion_main!(cmp); fn eq_equal(c: &mut Criterion) { @@ -26,7 +26,7 @@ fn eq_equal(c: &mut Criterion) { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0x2D, 0x6D, 0x19, 0x40, 0x84, 0xC2, 0xDE, 0x36, 0xE0, 0xDA, 0xBF, 0xCE, 0x45, 0xD0, 0x46, 0xB3, 0x7D, 0x11, 0x06, ]), - H256([u8::max_value(); 32]), + H256([u8::MAX; 32]), ] { group.bench_with_input(BenchmarkId::from_parameter(input), &input, |b, x| { b.iter(|| black_box(x.eq(black_box(x)))) diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index a5c1ed41c..4cf63487e 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -262,8 +262,23 @@ macro_rules! construct_fixed_hash { } impl $crate::core_::cmp::PartialOrd for $name { + #[inline] fn partial_cmp(&self, other: &Self) -> Option<$crate::core_::cmp::Ordering> { - Some(self.cmp(other)) + self.as_bytes().partial_cmp(other.as_bytes()) + } + } + + impl $crate::core_::cmp::Ord for $name { + #[inline] + fn cmp(&self, other: &Self) -> $crate::core_::cmp::Ordering { + self.as_bytes().cmp(other.as_bytes()) + } + } + + impl $crate::core_::default::Default for $name { + #[inline] + fn default() -> Self { + Self::zero() } } @@ -295,53 +310,71 @@ macro_rules! construct_fixed_hash { } } - impl $crate::core_::default::Default for $name { - #[inline] - fn default() -> Self { - Self::zero() - } - } - - impl_ops_for_hash!($name, BitOr, bitor, BitOrAssign, bitor_assign, |, |=); - impl_ops_for_hash!($name, BitAnd, bitand, BitAndAssign, bitand_assign, &, &=); - impl_ops_for_hash!($name, BitXor, bitxor, BitXorAssign, bitxor_assign, ^, ^=); + impl_bit_ops_for_fixed_hash!($name, BitOr, bitor, BitOrAssign, bitor_assign, |, |=); + impl_bit_ops_for_fixed_hash!($name, BitAnd, bitand, BitAndAssign, bitand_assign, &, &=); + impl_bit_ops_for_fixed_hash!($name, BitXor, bitxor, BitXorAssign, bitxor_assign, ^, ^=); impl_byteorder_for_fixed_hash!($name); + impl_rand_for_fixed_hash!($name); - impl_cmp_for_fixed_hash!($name); impl_rustc_hex_for_fixed_hash!($name); impl_quickcheck_for_fixed_hash!($name); impl_arbitrary_for_fixed_hash!($name); } } -// Implementation for disabled byteorder crate support. -// -// # Note -// -// Feature guarded macro definitions instead of feature guarded impl blocks -// to work around the problems of introducing `byteorder` crate feature in -// a user crate. -#[cfg(not(feature = "byteorder"))] #[macro_export] #[doc(hidden)] -macro_rules! impl_byteorder_for_fixed_hash { - ( $name:ident ) => {}; +macro_rules! impl_bit_ops_for_fixed_hash { + ( + $impl_for:ident, + $ops_trait_name:ident, + $ops_fn_name:ident, + $ops_assign_trait_name:ident, + $ops_assign_fn_name:ident, + $ops_tok:tt, + $ops_assign_tok:tt + ) => { + impl<'r> $crate::core_::ops::$ops_assign_trait_name<&'r $impl_for> for $impl_for { + fn $ops_assign_fn_name(&mut self, rhs: &'r $impl_for) { + for (lhs, rhs) in self.as_bytes_mut().iter_mut().zip(rhs.as_bytes()) { + *lhs $ops_assign_tok rhs; + } + } + } + + impl $crate::core_::ops::$ops_assign_trait_name<$impl_for> for $impl_for { + #[inline] + fn $ops_assign_fn_name(&mut self, rhs: $impl_for) { + *self $ops_assign_tok &rhs; + } + } + + impl<'l, 'r> $crate::core_::ops::$ops_trait_name<&'r $impl_for> for &'l $impl_for { + type Output = $impl_for; + + fn $ops_fn_name(self, rhs: &'r $impl_for) -> Self::Output { + let mut ret = self.clone(); + ret $ops_assign_tok rhs; + ret + } + } + + impl $crate::core_::ops::$ops_trait_name<$impl_for> for $impl_for { + type Output = $impl_for; + + #[inline] + fn $ops_fn_name(self, rhs: Self) -> Self::Output { + &self $ops_tok &rhs + } + } + }; } -// Implementation for enabled byteorder crate support. -// -// # Note -// -// Feature guarded macro definitions instead of feature guarded impl blocks -// to work around the problems of introducing `byteorder` crate feature in -// a user crate. -#[cfg(feature = "byteorder")] #[macro_export] #[doc(hidden)] macro_rules! impl_byteorder_for_fixed_hash { ( $name:ident ) => { - /// Utilities using the `byteorder` crate. impl $name { /// Returns the least significant `n` bytes as slice. /// @@ -354,14 +387,11 @@ macro_rules! impl_byteorder_for_fixed_hash { &self[(Self::len_bytes() - n)..] } - fn to_low_u64_with_byteorder(&self) -> u64 - where - B: $crate::byteorder::ByteOrder, - { + fn to_low_u64_with_fn(&self, from_bytes: fn([u8; 8]) -> u64) -> u64 { let mut buf = [0x0; 8]; let capped = $crate::core_::cmp::min(Self::len_bytes(), 8); buf[(8 - capped)..].copy_from_slice(self.least_significant_bytes(capped)); - B::read_u64(&buf) + from_bytes(buf) } /// Returns the lowest 8 bytes interpreted as big-endian. @@ -372,7 +402,7 @@ macro_rules! impl_byteorder_for_fixed_hash { /// are interpreted as being zero. #[inline] pub fn to_low_u64_be(&self) -> u64 { - self.to_low_u64_with_byteorder::<$crate::byteorder::BigEndian>() + self.to_low_u64_with_fn(u64::from_be_bytes) } /// Returns the lowest 8 bytes interpreted as little-endian. @@ -383,7 +413,7 @@ macro_rules! impl_byteorder_for_fixed_hash { /// are interpreted as being zero. #[inline] pub fn to_low_u64_le(&self) -> u64 { - self.to_low_u64_with_byteorder::<$crate::byteorder::LittleEndian>() + self.to_low_u64_with_fn(u64::from_le_bytes) } /// Returns the lowest 8 bytes interpreted as native-endian. @@ -394,15 +424,11 @@ macro_rules! impl_byteorder_for_fixed_hash { /// are interpreted as being zero. #[inline] pub fn to_low_u64_ne(&self) -> u64 { - self.to_low_u64_with_byteorder::<$crate::byteorder::NativeEndian>() + self.to_low_u64_with_fn(u64::from_ne_bytes) } - fn from_low_u64_with_byteorder(val: u64) -> Self - where - B: $crate::byteorder::ByteOrder, - { - let mut buf = [0x0; 8]; - B::write_u64(&mut buf, val); + fn from_low_u64_with_fn(val: u64, to_bytes: fn(u64) -> [u8; 8]) -> Self { + let buf = to_bytes(val); let capped = $crate::core_::cmp::min(Self::len_bytes(), 8); let mut bytes = [0x0; $crate::core_::mem::size_of::()]; bytes[(Self::len_bytes() - capped)..].copy_from_slice(&buf[..capped]); @@ -418,7 +444,7 @@ macro_rules! impl_byteorder_for_fixed_hash { /// if the hash type has less than 8 bytes. #[inline] pub fn from_low_u64_be(val: u64) -> Self { - Self::from_low_u64_with_byteorder::<$crate::byteorder::BigEndian>(val) + Self::from_low_u64_with_fn(val, u64::to_be_bytes) } /// Creates a new hash type from the given `u64` value. @@ -430,7 +456,7 @@ macro_rules! impl_byteorder_for_fixed_hash { /// if the hash type has less than 8 bytes. #[inline] pub fn from_low_u64_le(val: u64) -> Self { - Self::from_low_u64_with_byteorder::<$crate::byteorder::LittleEndian>(val) + Self::from_low_u64_with_fn(val, u64::to_le_bytes) } /// Creates a new hash type from the given `u64` value. @@ -442,7 +468,7 @@ macro_rules! impl_byteorder_for_fixed_hash { /// if the hash type has less than 8 bytes. #[inline] pub fn from_low_u64_ne(val: u64) -> Self { - Self::from_low_u64_with_byteorder::<$crate::byteorder::NativeEndian>(val) + Self::from_low_u64_with_fn(val, u64::to_ne_bytes) } } }; @@ -523,19 +549,6 @@ macro_rules! impl_rand_for_fixed_hash { }; } -#[macro_export] -#[doc(hidden)] -macro_rules! impl_cmp_for_fixed_hash { - ( $name:ident ) => { - impl $crate::core_::cmp::Ord for $name { - #[inline] - fn cmp(&self, other: &Self) -> $crate::core_::cmp::Ordering { - self.as_bytes().cmp(other.as_bytes()) - } - } - }; -} - // Implementation for disabled rustc-hex crate support. // // # Note @@ -662,54 +675,6 @@ macro_rules! impl_arbitrary_for_fixed_hash { }; } -#[macro_export] -#[doc(hidden)] -macro_rules! impl_ops_for_hash { - ( - $impl_for:ident, - $ops_trait_name:ident, - $ops_fn_name:ident, - $ops_assign_trait_name:ident, - $ops_assign_fn_name:ident, - $ops_tok:tt, - $ops_assign_tok:tt - ) => { - impl<'r> $crate::core_::ops::$ops_assign_trait_name<&'r $impl_for> for $impl_for { - fn $ops_assign_fn_name(&mut self, rhs: &'r $impl_for) { - for (lhs, rhs) in self.as_bytes_mut().iter_mut().zip(rhs.as_bytes()) { - *lhs $ops_assign_tok rhs; - } - } - } - - impl $crate::core_::ops::$ops_assign_trait_name<$impl_for> for $impl_for { - #[inline] - fn $ops_assign_fn_name(&mut self, rhs: $impl_for) { - *self $ops_assign_tok &rhs; - } - } - - impl<'l, 'r> $crate::core_::ops::$ops_trait_name<&'r $impl_for> for &'l $impl_for { - type Output = $impl_for; - - fn $ops_fn_name(self, rhs: &'r $impl_for) -> Self::Output { - let mut ret = self.clone(); - ret $ops_assign_tok rhs; - ret - } - } - - impl $crate::core_::ops::$ops_trait_name<$impl_for> for $impl_for { - type Output = $impl_for; - - #[inline] - fn $ops_fn_name(self, rhs: Self) -> Self::Output { - &self $ops_tok &rhs - } - } - }; -} - /// Implements lossy conversions between the given types. /// /// # Note diff --git a/fixed-hash/src/lib.rs b/fixed-hash/src/lib.rs index 228f551e0..5f365e997 100644 --- a/fixed-hash/src/lib.rs +++ b/fixed-hash/src/lib.rs @@ -30,10 +30,6 @@ pub use static_assertions; #[doc(hidden)] pub use static_assertions::const_assert; -#[cfg(feature = "byteorder")] -#[doc(hidden)] -pub use byteorder; - #[cfg(feature = "rustc-hex")] #[doc(hidden)] pub use rustc_hex; diff --git a/fixed-hash/src/tests.rs b/fixed-hash/src/tests.rs index 3ba8a6508..a0975462f 100644 --- a/fixed-hash/src/tests.rs +++ b/fixed-hash/src/tests.rs @@ -155,7 +155,6 @@ mod is_zero { } } -#[cfg(feature = "byteorder")] mod to_low_u64 { use super::*; @@ -195,7 +194,6 @@ mod to_low_u64 { } } -#[cfg(feature = "byteorder")] mod from_low_u64 { use super::*; @@ -328,8 +326,8 @@ fn from_h256_to_h160_lossy() { assert_eq!(h160, expected); } -#[cfg(all(feature = "std", feature = "byteorder"))] #[test] +#[cfg(feature = "std")] fn display_and_debug() { fn test_for(x: u64, hex: &'static str, display: &'static str) { let hash = H64::from_low_u64_be(x); diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 34ddca0bb..7e11ae8df 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -28,7 +28,6 @@ jsonschema = { version = "0.17", default-features = false } default = ["std", "rand"] std = ["uint/std", "fixed-hash/std", "impl-codec?/std"] rand = ["fixed-hash/rand"] -byteorder = ["fixed-hash/byteorder"] rustc-hex = ["fixed-hash/rustc-hex"] serde = ["std", "impl-serde", "impl-serde/std"] json-schema = ["dep:schemars"]