Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed-hash: remove rustc-hex feature #873

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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='rustc-hex'
run: cargo test -p fixed-hash --no-default-features

- name: Test fixed-hash all-features
uses: actions-rs/cargo@v1
Expand Down
5 changes: 2 additions & 3 deletions ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ documentation = "https://docs.rs/ethbloom"
homepage = "https://github.com/paritytech/parity-common"
repository = "https://github.com/paritytech/parity-common"
edition = "2021"
rust-version = "1.56.1"
rust-version = "1.64.0"

[dependencies]
tiny-keccak = { version = "2.0", features = ["keccak"] }
Expand All @@ -25,10 +25,9 @@ rand = "0.8.0"
hex-literal = "0.4.1"

[features]
default = ["std", "rlp", "serialize", "rustc-hex"]
default = ["std", "rlp", "serialize"]
std = ["fixed-hash/std", "crunchy/std"]
serialize = ["impl-serde"]
rustc-hex = ["fixed-hash/rustc-hex"]
arbitrary = ["fixed-hash/arbitrary"]
rlp = ["impl-rlp"]
codec = ["impl-codec", "scale-info"]
Expand Down
10 changes: 5 additions & 5 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ license = "MIT OR Apache-2.0"
homepage = "https://github.com/paritytech/parity-common"
description = "Ethereum types"
edition = "2021"
rust-version = "1.60.0"
rust-version = "1.64.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 = ["rustc-hex"] }
ethbloom = { path = "../ethbloom", version = "0.14", default-features = false, optional = true }
fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false }
uint-crate = { path = "../uint", package = "uint", version = "0.10", default-features = false }
primitive-types = { path = "../primitive-types", version = "0.13", features = ["rustc-hex"], default-features = false }
primitive-types = { path = "../primitive-types", version = "0.13", 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 }
impl-codec = { path = "../primitive-types/impls/codec", version = "0.7.0", default-features = false, optional = true }
scale-info = { version = ">=1.0, <3", features = ["derive"], default-features = false, optional = true }

[dev-dependencies]
Expand Down
20 changes: 12 additions & 8 deletions fixed-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ description = "Macros to define custom fixed-size hash types"
documentation = "https://docs.rs/fixed-hash/"
readme = "README.md"
edition = "2021"
rust-version = "1.60"
rust-version = "1.64.0"

[package.metadata.docs.rs]
features = ["quickcheck", "api-dummy"]
features = ["arbitrary", "quickcheck", "api-dummy"]

[dependencies]
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 }
const-hex = { version = "1.13", default-features = false }
rand = { version = "0.8.0", default-features = false, optional = true }
static_assertions = "1.0.0"

arbitrary = { version = "1.0", optional = true }
quickcheck = { version = "1", optional = true }

[dev-dependencies]
rand_xorshift = "0.3.0"
criterion = "0.5.1"
rand = { version = "0.8.0", default-features = false, features = ["std_rng"] }
rand_xorshift = "0.3.0"

[features]
default = ["std", "rand", "rustc-hex"]
std = ["rustc-hex/std", "rand?/std"]
default = ["std", "rand"]
std = ["const-hex/std", "rand?/std"]
rand = ["dep:rand"]
arbitrary = ["dep:arbitrary"]
quickcheck = ["dep:quickcheck"]

api-dummy = [] # Feature used by docs.rs to display documentation of hash types

Expand Down
1 change: 0 additions & 1 deletion fixed-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fixed-hash = { version = "0.3", default-features = false }

- `std`: Use the standard library instead of the core library.
- Using this feature enables the following features
- `rustc-hex/std`
- `rand/std`
- Enabled by default.
- `rand`: Provide API based on the `rand` crate.
Expand Down
94 changes: 29 additions & 65 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ macro_rules! construct_fixed_hash {
}
}

$crate::impl_to_from_low_u64_for_fixed_hash!($name);

impl $crate::core_::fmt::Debug for $name {
fn fmt(&self, f: &mut $crate::core_::fmt::Formatter) -> $crate::core_::fmt::Result {
$crate::core_::write!(f, "{:#x}", self)
Expand Down Expand Up @@ -288,6 +290,26 @@ macro_rules! construct_fixed_hash {
}
}

impl $crate::core_::str::FromStr for $name {
type Err = $crate::const_hex::FromHexError;

/// Creates a hash type instance from the given string.
///
/// # Note
///
/// The given input string is interpreted in big endian.
///
/// # Errors
///
/// - When encountering invalid non hex-digits
/// - Upon empty string input or invalid input length in general
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::const_hex::FromHexError> {
let mut result = Self::zero();
$crate::const_hex::decode_to_slice(input, result.as_bytes_mut())?;
Ok(result)
}
}

impl<I> $crate::core_::ops::Index<I> for $name
where
I: $crate::core_::slice::SliceIndex<[u8]>
Expand All @@ -310,16 +332,13 @@ macro_rules! construct_fixed_hash {
}
}

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);
$crate::impl_bit_ops_for_fixed_hash!($name, BitOr, bitor, BitOrAssign, bitor_assign, |, |=);
$crate::impl_bit_ops_for_fixed_hash!($name, BitAnd, bitand, BitAndAssign, bitand_assign, &, &=);
$crate::impl_bit_ops_for_fixed_hash!($name, BitXor, bitxor, BitXorAssign, bitxor_assign, ^, ^=);

impl_rand_for_fixed_hash!($name);
impl_rustc_hex_for_fixed_hash!($name);
impl_quickcheck_for_fixed_hash!($name);
impl_arbitrary_for_fixed_hash!($name);
$crate::impl_rand_for_fixed_hash!($name);
$crate::impl_quickcheck_for_fixed_hash!($name);
$crate::impl_arbitrary_for_fixed_hash!($name);
}
}

Expand Down Expand Up @@ -373,7 +392,7 @@ macro_rules! impl_bit_ops_for_fixed_hash {

#[macro_export]
#[doc(hidden)]
macro_rules! impl_byteorder_for_fixed_hash {
macro_rules! impl_to_from_low_u64_for_fixed_hash {
( $name:ident ) => {
impl $name {
/// Returns the least significant `n` bytes as slice.
Expand Down Expand Up @@ -549,61 +568,6 @@ macro_rules! impl_rand_for_fixed_hash {
};
}

// Implementation for disabled rustc-hex crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rustc-hex` crate feature in
// a user crate.
#[cfg(not(feature = "rustc-hex"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rustc_hex_for_fixed_hash {
( $name:ident ) => {};
}

// Implementation for enabled rustc-hex crate support.
//
// # Note
//
// Feature guarded macro definitions instead of feature guarded impl blocks
// to work around the problems of introducing `rustc-hex` crate feature in
// a user crate.
#[cfg(feature = "rustc-hex")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_rustc_hex_for_fixed_hash {
( $name:ident ) => {
impl $crate::core_::str::FromStr for $name {
type Err = $crate::rustc_hex::FromHexError;

/// Creates a hash type instance from the given string.
///
/// # Note
///
/// The given input string is interpreted in big endian.
///
/// # Errors
///
/// - When encountering invalid non hex-digits
/// - Upon empty string input or invalid input length in general
fn from_str(input: &str) -> $crate::core_::result::Result<$name, $crate::rustc_hex::FromHexError> {
let input = input.strip_prefix("0x").unwrap_or(input);
let mut iter = $crate::rustc_hex::FromHexIter::new(input);
let mut result = Self::zero();
for byte in result.as_mut() {
*byte = iter.next().ok_or(Self::Err::InvalidHexLength)??;
}
if iter.next().is_some() {
return Err(Self::Err::InvalidHexLength)
}
Ok(result)
}
}
};
}

// Implementation for disabled quickcheck crate support.
//
// # Note
Expand Down
3 changes: 1 addition & 2 deletions fixed-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ pub use static_assertions;
#[doc(hidden)]
pub use static_assertions::const_assert;

#[cfg(feature = "rustc-hex")]
#[doc(hidden)]
pub use rustc_hex;
pub use const_hex;

#[cfg(feature = "rand")]
#[doc(hidden)]
Expand Down
38 changes: 16 additions & 22 deletions fixed-hash/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,48 +235,42 @@ mod from_low_u64 {
}
}

#[cfg(feature = "rand")]
mod rand {
use super::*;
use ::rand::{rngs::StdRng, SeedableRng};

#[test]
fn random() {
let mut rng = StdRng::seed_from_u64(123);
assert_eq!(H32::random_using(&mut rng), H32::from([0xeb, 0x96, 0xaf, 0x1c]));
}
}

#[cfg(feature = "rustc-hex")]
mod from_str {
use super::*;

#[test]
fn valid() {
use crate::core_::str::FromStr;

assert_eq!(
H64::from_str("0123456789ABCDEF").unwrap(),
"0123456789ABCDEF".parse::<H64>().unwrap(),
H64::from([0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF])
)
}

#[test]
fn empty_str() {
use crate::core_::str::FromStr;
assert!(H64::from_str("").is_err())
assert!("".parse::<H64>().is_err())
}

#[test]
fn invalid_digits() {
use crate::core_::str::FromStr;
assert!(H64::from_str("Hello, World!").is_err())
assert!("Hello, World!".parse::<H64>().is_err())
}

#[test]
fn too_many_digits() {
use crate::core_::str::FromStr;
assert!(H64::from_str("0123456789ABCDEF0").is_err())
assert!("0123456789ABCDEF0".parse::<H64>().is_err())
}
}

#[cfg(feature = "rand")]
mod rand {
use super::*;
use ::rand::{rngs::StdRng, SeedableRng};

#[test]
fn random() {
let mut rng = StdRng::seed_from_u64(123);
assert_eq!(H32::random_using(&mut rng), H32::from([0xeb, 0x96, 0xaf, 0x1c]));
}
}

Expand Down
15 changes: 7 additions & 8 deletions primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ homepage = "https://github.com/paritytech/parity-common"
repository = "https://github.com/paritytech/parity-common"
description = "Primitive types shared by Ethereum and Substrate"
edition = "2021"
rust-version = "1.60.0"
rust-version = "1.64.0"

[dependencies]
fixed-hash = { version = "0.8", path = "../fixed-hash", default-features = false }
uint = { version = "0.10.0", path = "../uint", default-features = false }
impl-serde = { version = "0.5.0", path = "impls/serde", default-features = false, optional = true }
impl-codec = { version = "0.7.0", path = "impls/codec", default-features = false, optional = true }
impl-num-traits = { version = "0.2.0", path = "impls/num-traits", default-features = false, optional = true }
impl-rlp = { version = "0.4", path = "impls/rlp", default-features = false, optional = true }
fixed-hash = { path = "../fixed-hash", version = "0.8", default-features = false }
uint = { path = "../uint", version = "0.10.0", default-features = false }
impl-serde = { path = "impls/serde", version = "0.5.0", default-features = false, optional = true }
impl-codec = { path = "impls/codec", version = "0.7.0", default-features = false, optional = true }
impl-num-traits = { path = "impls/num-traits", version = "0.2.0",default-features = false, optional = true }
impl-rlp = { path = "impls/rlp", version = "0.4", default-features = false, optional = true }
scale-info-crate = { package = "scale-info", version = ">=0.9, <3", features = ["derive"], default-features = false, optional = true }
schemars = { version = ">=0.8.12", default-features = true, optional = true }

Expand All @@ -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"]
rustc-hex = ["fixed-hash/rustc-hex"]
serde = ["std", "impl-serde", "impl-serde/std"]
json-schema = ["dep:schemars"]
serde_no_std = ["impl-serde"]
Expand Down