From f3e762b555372625c3052276a778dd6148f57fb9 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 21 Jan 2020 13:11:48 +0100 Subject: [PATCH 1/2] Improving `construct_uint` macro --- src/util/uint.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/util/uint.rs b/src/util/uint.rs index 2e26f979..9456df0a 100644 --- a/src/util/uint.rs +++ b/src/util/uint.rs @@ -18,11 +18,6 @@ //! The functions here are designed to be fast. //! -use std::fmt; - -use consensus::encode; -use util::BitArray; - macro_rules! construct_uint { ($name:ident, $n_words:expr) => ( /// Little-endian large integer type @@ -116,7 +111,7 @@ macro_rules! construct_uint { #[inline] fn sub(self, other: $name) -> $name { - self + !other + BitArray::one() + self + !other + $crate::util::BitArray::one() } } @@ -124,6 +119,7 @@ macro_rules! construct_uint { type Output = $name; fn mul(self, other: $name) -> $name { + use $crate::util::BitArray; let mut me = $name::zero(); // TODO: be more efficient about this for i in 0..(2 * $n_words) { @@ -170,7 +166,7 @@ macro_rules! construct_uint { } } - impl BitArray for $name { + impl $crate::util::BitArray for $name { #[inline] fn bit(&self, index: usize) -> bool { let &$name(ref arr) = self; @@ -214,7 +210,7 @@ macro_rules! construct_uint { impl ::std::default::Default for $name { fn default() -> $name { - BitArray::zero() + $crate::util::BitArray::zero() } } @@ -319,8 +315,8 @@ macro_rules! construct_uint { } } - impl fmt::Debug for $name { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + impl ::std::fmt::Debug for $name { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { let &$name(ref data) = self; write!(f, "0x")?; for ch in data.iter().rev() { @@ -330,18 +326,18 @@ macro_rules! construct_uint { } } - impl fmt::Display for $name { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - ::fmt(self, f) + impl ::std::fmt::Display for $name { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + <::std::fmt::Debug>::fmt(self, f) } } - impl ::consensus::Encodable for $name { + impl $crate::consensus::Encodable for $name { #[inline] fn consensus_encode( &self, mut s: S, - ) -> Result { + ) -> Result { let &$name(ref data) = self; let mut len = 0; for word in data.iter() { @@ -351,11 +347,11 @@ macro_rules! construct_uint { } } - impl ::consensus::Decodable for $name { + impl $crate::consensus::Decodable for $name { fn consensus_decode( mut d: D, - ) -> Result<$name, encode::Error> { - use consensus::Decodable; + ) -> Result<$name, $crate::consensus::encode::Error> { + use $crate::consensus::Decodable; let mut ret: [u64; $n_words] = [0; $n_words]; for i in 0..$n_words { ret[i] = Decodable::consensus_decode(&mut d)?; From 9c0f4b1a6037bb7f29cf2b9f5d0afb9c458849d4 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 22 Jan 2020 11:28:11 +0100 Subject: [PATCH 2/2] Fixing namespaces in `impl_hashencode` --- src/hash_types.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/hash_types.rs b/src/hash_types.rs index 514c5309..3eefe39a 100644 --- a/src/hash_types.rs +++ b/src/hash_types.rs @@ -16,23 +16,22 @@ //! to avoid mixing data of the same hash format (like SHA256d) but of different meaning //! (transaction id, block hash etc). -use std::io; - use consensus::encode::{Encodable, Decodable, Error}; -use hashes::{sha256, sha256d, hash160, Hash}; -use hashes::hex::{ToHex, FromHex}; +use hashes::{Hash, sha256, sha256d, ripemd160, hash160}; +use hashes::hex::{FromHex, ToHex}; macro_rules! impl_hashencode { ($hashtype:ident) => { - impl Encodable for $hashtype { - fn consensus_encode(&self, s: S) -> Result { + impl $crate::consensus::Encodable for $hashtype { + fn consensus_encode(&self, s: S) -> Result { self.0.consensus_encode(s) } } - impl Decodable for $hashtype { - fn consensus_decode(d: D) -> Result { - Ok(Self::from_inner(<<$hashtype as Hash>::Inner>::consensus_decode(d)?)) + impl $crate::consensus::Decodable for $hashtype { + fn consensus_decode(d: D) -> Result { + use $crate::hashes::Hash; + Ok(Self::from_inner(<<$hashtype as $crate::hashes::Hash>::Inner>::consensus_decode(d)?)) } } }