Skip to content

Commit

Permalink
Use as-underscore for hash traits
Browse files Browse the repository at this point in the history
Bringing traits into scope risks naming conflicts for no real reason
because we can import traits using `as _`.

Also, in `hashes` we always use `as _` so `bitcoin` and `hashes` are
currently non-uniform.

Use as-underscore when importing the `Hash` and `HashEngine` traits.

Refactor, no logic changes.
  • Loading branch information
tcharding committed Apr 26, 2024
1 parent 26fc0d5 commit 9e924cf
Show file tree
Hide file tree
Showing 26 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion bitcoin/examples/sign-tx-segwit-v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::hashes::Hash as _;
use bitcoin::locktime::absolute;
use bitcoin::secp256k1::{rand, Message, Secp256k1, SecretKey, Signing};
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/examples/sign-tx-taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::str::FromStr;

use bitcoin::hashes::Hash;
use bitcoin::hashes::Hash as _;
use bitcoin::key::{Keypair, TapTweak, TweakedKeypair, UntweakedPublicKey};
use bitcoin::locktime::absolute;
use bitcoin::secp256k1::{rand, Message, Secp256k1, SecretKey, Signing, Verification};
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use core::marker::PhantomData;
use core::str::FromStr;

use bech32::primitives::hrp::Hrp;
use hashes::{sha256, Hash, HashEngine};
use hashes::{sha256, Hash as _, HashEngine as _};
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};

use crate::blockdata::constants::{
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/bip152.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{convert, fmt, mem};
#[cfg(feature = "std")]
use std::error;

use hashes::{sha256, siphash24, Hash};
use hashes::{sha256, siphash24, Hash as _};
use internals::impl_array_newtype;
use io::{BufRead, Write};

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
use core::cmp::{self, Ordering};
use core::fmt::{self, Display, Formatter};

use hashes::{sha256d, siphash24, Hash};
use hashes::{sha256d, siphash24, Hash as _};
use internals::write_err;
use io::{BufRead, Write};

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::ops::Index;
use core::str::FromStr;
use core::{fmt, slice};

use hashes::{hash160, hash_newtype, sha512, Hash, HashEngine, Hmac, HmacEngine};
use hashes::{hash160, hash_newtype, sha512, Hash as _, HashEngine as _, Hmac, HmacEngine};
use internals::{impl_array_newtype, write_err};
use io::Write;
use secp256k1::{Secp256k1, XOnlyPublicKey};
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use core::fmt;

use hashes::{sha256d, Hash, HashEngine};
use hashes::{sha256d, Hash as _, HashEngine as _};
use io::{BufRead, Write};

use super::Weight;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! single transaction.
//!

use hashes::{sha256d, Hash};
use hashes::{sha256d, Hash as _};
use hex_lit::hex;
use internals::impl_array_newtype;

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/script/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ops::{
Bound, Index, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
};

use hashes::Hash;
use hashes::Hash as _;
use secp256k1::{Secp256k1, Verification};

use super::PushBytes;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/script/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::str::FromStr;

use hashes::Hash;
use hashes::Hash as _;
use hex_lit::hex;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use core::{cmp, fmt, str};

use hashes::{sha256d, Hash};
use hashes::{sha256d, Hash as _};
use internals::write_err;
use io::{BufRead, Write};
use units::parse;
Expand Down
10 changes: 5 additions & 5 deletions bitcoin/src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use core::{fmt, mem};

use hashes::{sha256, sha256d, Hash};
use hashes::{sha256, sha256d, Hash as _};
use hex::error::{InvalidCharError, OddLengthStringError};
use internals::write_err;
use io::{BufRead, Cursor, Read, Write};
Expand Down Expand Up @@ -768,7 +768,7 @@ impl Decodable for Box<[u8]> {

/// Does a double-SHA256 on `data` and returns the first 4 bytes.
fn sha2_checksum(data: &[u8]) -> [u8; 4] {
let checksum = <sha256d::Hash as Hash>::hash(data);
let checksum = <sha256d::Hash as hashes::Hash>::hash(data);
[checksum[0], checksum[1], checksum[2], checksum[3]]
}

Expand Down Expand Up @@ -869,7 +869,7 @@ impl Encodable for sha256d::Hash {

impl Decodable for sha256d::Hash {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, Error> {
Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
Ok(Self::from_byte_array(<<Self as hashes::Hash>::Bytes>::consensus_decode(r)?))
}
}

Expand All @@ -881,7 +881,7 @@ impl Encodable for sha256::Hash {

impl Decodable for sha256::Hash {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, Error> {
Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
Ok(Self::from_byte_array(<<Self as hashes::Hash>::Bytes>::consensus_decode(r)?))
}
}

Expand All @@ -893,7 +893,7 @@ impl Encodable for TapLeafHash {

impl Decodable for TapLeafHash {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, Error> {
Ok(Self::from_byte_array(<<Self as Hash>::Bytes>::consensus_decode(r)?))
Ok(Self::from_byte_array(<<Self as hashes::Hash>::Bytes>::consensus_decode(r)?))
}
}

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt::{self, Write as _};
use core::ops;
use core::str::FromStr;

use hashes::{hash160, Hash};
use hashes::{hash160, Hash as _};
use hex::{FromHex, HexToArrayError};
use internals::array_vec::ArrayVec;
use internals::write_err;
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/crypto/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use core::{fmt, str};

use hashes::{hash_newtype, sha256, sha256d, sha256t_hash_newtype, Hash};
use hashes::{hash_newtype, sha256, sha256d, sha256t_hash_newtype, Hash as _};
use internals::write_err;
use io::Write;

Expand Down Expand Up @@ -1450,7 +1450,7 @@ impl<E: std::error::Error + 'static> std::error::Error for SigningDataError<E> {
mod tests {
use std::str::FromStr;

use hashes::HashEngine;
use hashes::HashEngine as _;
use hex::{test_hex_unwrap as hex, FromHex};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/hash_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use crate::{
#[cfg(test)]
mod tests {
use super::*;
use crate::hashes::Hash;
use crate::hashes::Hash as _;
use crate::{
LegacySighash, PubkeyHash, ScriptHash, SegwitV0Sighash, TapSighash, WPubkeyHash,
WScriptHash, XKeyIdentifier,
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ macro_rules! impl_hashencode {

impl $crate::consensus::Decodable for $hashtype {
fn consensus_decode<R: $crate::io::BufRead + ?Sized>(r: &mut R) -> core::result::Result<Self, $crate::consensus::encode::Error> {
use $crate::hashes::Hash;
use $crate::hashes::Hash as _;
Ok(Self::from_byte_array(<<$hashtype as $crate::hashes::Hash>::Bytes>::consensus_decode(r)?))
}
}
Expand All @@ -213,14 +213,14 @@ macro_rules! impl_asref_push_bytes {
$(
impl AsRef<$crate::blockdata::script::PushBytes> for $hashtype {
fn as_ref(&self) -> &$crate::blockdata::script::PushBytes {
use $crate::hashes::Hash;
use $crate::hashes::Hash as _;
self.as_byte_array().into()
}
}

impl From<$hashtype> for $crate::blockdata::script::PushBytesBuf {
fn from(hash: $hashtype) -> Self {
use $crate::hashes::Hash;
use $crate::hashes::Hash as _;
hash.as_byte_array().into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/merkle_tree/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

use core::fmt;

use hashes::Hash;
use hashes::Hash as _;
use io::{BufRead, Write};

use self::MerkleBlockError::*;
Expand Down
17 changes: 8 additions & 9 deletions bitcoin/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! ```
//! # use bitcoin::{merkle_tree, Txid};
//! # use bitcoin::hashes::Hash;
//! # use bitcoin::hashes::Hash as _;
//! # let tx1 = Txid::all_zeros(); // Dummy hash values.
//! # let tx2 = Txid::all_zeros();
//! let tx_hashes = vec![tx1, tx2]; // All the hashes we wish to merkelize.
Expand All @@ -18,7 +18,6 @@ mod block;
use core::cmp::min;
use core::iter;

use hashes::Hash;
use io::Write;

use crate::consensus::encode::Encodable;
Expand All @@ -40,8 +39,8 @@ pub use self::block::{MerkleBlock, MerkleBlockError, PartialMerkleTree};
/// - `Some(merkle_root)` if length of `hashes` is greater than one.
pub fn calculate_root_inline<T>(hashes: &mut [T]) -> Option<T>
where
T: Hash + Encodable,
<T as Hash>::Engine: Write,
T: hashes::Hash + Encodable,
<T as hashes::Hash>::Engine: Write,
{
match hashes.len() {
0 => None,
Expand All @@ -58,8 +57,8 @@ where
/// - `Some(merkle_root)` if length of `hashes` is greater than one.
pub fn calculate_root<T, I>(mut hashes: I) -> Option<T>
where
T: Hash + Encodable,
<T as Hash>::Engine: Write,
T: hashes::Hash + Encodable,
<T as hashes::Hash>::Engine: Write,
I: Iterator<Item = T>,
{
let first = hashes.next()?;
Expand Down Expand Up @@ -90,8 +89,8 @@ where
// `hashes` must contain at least one hash.
fn merkle_root_r<T>(hashes: &mut [T]) -> T
where
T: Hash + Encodable,
<T as Hash>::Engine: Write,
T: hashes::Hash + Encodable,
<T as hashes::Hash>::Engine: Write,
{
if hashes.len() == 1 {
return hashes[0];
Expand All @@ -112,7 +111,7 @@ where

#[cfg(test)]
mod tests {
use hashes::sha256d;
use hashes::{sha256d, Hash as _};

use super::*;
use crate::blockdata::block::Block;
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/p2p/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use core::{fmt, iter};

use hashes::{sha256d, Hash};
use hashes::{sha256d, Hash as _};
use io::{BufRead, Write};

use crate::blockdata::{block, transaction};
Expand Down
4 changes: 1 addition & 3 deletions bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::cmp;
use core::fmt::{self, LowerHex, UpperHex};
use core::ops::{Add, Div, Mul, Not, Rem, Shl, Shr, Sub};

use hashes::Hash as _;
use io::{BufRead, Write};
#[cfg(all(test, mutate))]
use mutagen::mutate;
Expand Down Expand Up @@ -220,7 +221,6 @@ impl Target {
/// to the target.
#[cfg_attr(all(test, mutate), mutate)]
pub fn is_met_by(&self, hash: BlockHash) -> bool {
use hashes::Hash;
let hash = U256::from_le_bytes(hash.to_byte_array());
hash <= self.0
}
Expand Down Expand Up @@ -1712,8 +1712,6 @@ mod tests {
fn target_is_met_by_for_target_equals_hash() {
use std::str::FromStr;

use hashes::Hash;

let hash =
BlockHash::from_str("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")
.expect("failed to parse block hash");
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ pub use self::display_from_str::PsbtParseError;

#[cfg(test)]
mod tests {
use hashes::{hash160, ripemd160, sha256, Hash};
use hashes::{hash160, ripemd160, sha256, Hash as _};
use hex::{test_hex_unwrap as hex, FromHex};
#[cfg(feature = "rand-std")]
use secp256k1::{All, SecretKey};
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/psbt/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! according to the BIP-174 specification.
//!

use hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use hashes::{hash160, ripemd160, sha256, sha256d, Hash as _};
use secp256k1::XOnlyPublicKey;

use super::map::{Input, Map, Output, PsbtSighashType};
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/sign_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! library is used with the `secp-recovery` feature.
//!

use hashes::{sha256d, Hash, HashEngine};
use hashes::{sha256d, Hash as _, HashEngine as _};

use crate::consensus::{encode, Encodable};

Expand All @@ -22,7 +22,7 @@ pub const BITCOIN_SIGNED_MSG_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
mod message_signing {
use core::fmt;

use hashes::{sha256d, Hash};
use hashes::{sha256d, Hash as _};
use internals::write_err;
use secp256k1::ecdsa::{RecoverableSignature, RecoveryId};

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/taproot/merkle_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Contains `TaprootMerkleBranch` and its associated types.

use hashes::Hash;
use hashes::Hash as _;

use super::{
TapNodeHash, TaprootBuilderError, TaprootError, TAPROOT_CONTROL_MAX_NODE_COUNT,
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/taproot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::cmp::Reverse;
use core::fmt;
use core::iter::FusedIterator;

use hashes::{sha256t_hash_newtype, Hash, HashEngine};
use hashes::{sha256t_hash_newtype, Hash as _, HashEngine as _};
use internals::write_err;
use io::Write;
use secp256k1::{Scalar, Secp256k1};
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use bitcoin::bip32::{ChildNumber, KeySource, Xpriv, Xpub};
use bitcoin::blockdata::locktime::{absolute, relative};
use bitcoin::blockdata::witness::Witness;
use bitcoin::consensus::encode::deserialize;
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash as _};
use bitcoin::hex::FromHex;
use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey};
use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType};
Expand Down

0 comments on commit 9e924cf

Please sign in to comment.