Skip to content

Commit

Permalink
fixed-hash: remove byteorder feature (#872)
Browse files Browse the repository at this point in the history
* fixed-hash: remove byteorder features

* fix fixed-hash part of CI

* fix tests

* update readme
  • Loading branch information
koushiro authored Oct 10, 2024
1 parent 366a95c commit 85f813a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
5 changes: 2 additions & 3 deletions fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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

Expand Down
11 changes: 3 additions & 8 deletions fixed-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand All @@ -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`
4 changes: 2 additions & 2 deletions fixed-hash/benches/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))))
Expand Down
183 changes: 74 additions & 109 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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.
///
Expand All @@ -354,14 +387,11 @@ macro_rules! impl_byteorder_for_fixed_hash {
&self[(Self::len_bytes() - n)..]
}

fn to_low_u64_with_byteorder<B>(&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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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<B>(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::<Self>()];
bytes[(Self::len_bytes() - capped)..].copy_from_slice(&buf[..capped]);
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}
}
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions fixed-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 85f813a

Please sign in to comment.