From 3a5e8d85043501c8dc826dafdddafd7806f99b29 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 25 Jan 2020 05:19:46 +0100 Subject: [PATCH] Namespace hygiene for internal macros --- src/internal_macros.rs | 159 +++++++++++++++++++++-------------------- src/util/bip32.rs | 2 +- 2 files changed, 82 insertions(+), 79 deletions(-) diff --git a/src/internal_macros.rs b/src/internal_macros.rs index ef0d815f..6baae6b8 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -18,31 +18,32 @@ macro_rules! impl_consensus_encoding { ($thing:ident, $($field:ident),+) => ( - impl ::consensus::Encodable for $thing { + impl $crate::consensus::Encodable for $thing { #[inline] - fn consensus_encode( + fn consensus_encode( &self, mut s: S, - ) -> Result { + ) -> Result { let mut len = 0; $(len += self.$field.consensus_encode(&mut s)?;)+ Ok(len) } } - impl ::consensus::Decodable for $thing { + impl $crate::consensus::Decodable for $thing { #[inline] - fn consensus_decode( + fn consensus_decode( mut d: D, - ) -> Result<$thing, ::consensus::encode::Error> { + ) -> Result<$thing, $crate::consensus::encode::Error> { Ok($thing { - $($field: ::consensus::Decodable::consensus_decode(&mut d)?),+ + $($field: $crate::consensus::Decodable::consensus_decode(&mut d)?),+ }) } } ); } +/// Implements standard array methods for a given wrapper type macro_rules! impl_array_newtype { ($thing:ident, $ty:ty, $len:expr) => { impl $thing { @@ -81,7 +82,7 @@ macro_rules! impl_array_newtype { pub fn into_bytes(self) -> [$ty; $len] { self.0 } } - impl<'a> From<&'a [$ty]> for $thing { + impl<'a> $crate::std::convert::From<&'a [$ty]> for $thing { fn from(data: &'a [$ty]) -> $thing { assert_eq!(data.len(), $len); let mut ret = [0; $len]; @@ -90,7 +91,7 @@ macro_rules! impl_array_newtype { } } - impl ::std::ops::Index for $thing { + impl $crate::std::ops::Index for $thing { type Output = $ty; #[inline] @@ -102,57 +103,57 @@ macro_rules! impl_array_newtype { impl_index_newtype!($thing, $ty); - impl PartialEq for $thing { + impl $crate::std::cmp::PartialEq for $thing { #[inline] fn eq(&self, other: &$thing) -> bool { &self[..] == &other[..] } } - impl Eq for $thing {} + impl $crate::std::cmp::Eq for $thing {} - impl PartialOrd for $thing { + impl $crate::std::cmp::PartialOrd for $thing { #[inline] - fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> { + fn partial_cmp(&self, other: &$thing) -> Option<$crate::std::cmp::Ordering> { Some(self.cmp(&other)) } } - impl Ord for $thing { + impl $crate::std::cmp::Ord for $thing { #[inline] - fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering { + fn cmp(&self, other: &$thing) -> $crate::std::cmp::Ordering { // manually implement comparison to get little-endian ordering // (we need this for our numeric types; non-numeric ones shouldn't // be ordered anyway except to put them in BTrees or whatever, and // they don't care how we order as long as we're consistent). for i in 0..$len { - if self[$len - 1 - i] < other[$len - 1 - i] { return ::std::cmp::Ordering::Less; } - if self[$len - 1 - i] > other[$len - 1 - i] { return ::std::cmp::Ordering::Greater; } + if self[$len - 1 - i] < other[$len - 1 - i] { return $crate::std::cmp::Ordering::Less; } + if self[$len - 1 - i] > other[$len - 1 - i] { return $crate::std::cmp::Ordering::Greater; } } - ::std::cmp::Ordering::Equal + $crate::std::cmp::Ordering::Equal } } #[cfg_attr(feature = "clippy", allow(expl_impl_clone_on_copy))] // we don't define the `struct`, we have to explicitly impl - impl Clone for $thing { + impl $crate::std::clone::Clone for $thing { #[inline] fn clone(&self) -> $thing { $thing::from(&self[..]) } } - impl Copy for $thing {} + impl $crate::std::marker::Copy for $thing {} - impl ::std::hash::Hash for $thing { + impl $crate::std::hash::Hash for $thing { #[inline] fn hash(&self, state: &mut H) - where H: ::std::hash::Hasher + where H: $crate::std::hash::Hasher { (&self[..]).hash(state); } fn hash_slice(data: &[$thing], state: &mut H) - where H: ::std::hash::Hasher + where H: $crate::std::hash::Hasher { for d in data.iter() { (&d[..]).hash(state); @@ -162,50 +163,52 @@ macro_rules! impl_array_newtype { } } +/// Implements debug formatting for a given wrapper type macro_rules! impl_array_newtype_show { ($thing:ident) => { - impl ::std::fmt::Debug for $thing { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl $crate::std::fmt::Debug for $thing { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { write!(f, concat!(stringify!($thing), "({:?})"), &self[..]) } } } } +/// Implements standard indexing methods for a given wrapper type macro_rules! impl_index_newtype { ($thing:ident, $ty:ty) => { - impl ::std::ops::Index<::std::ops::Range> for $thing { + impl $crate::std::ops::Index<$crate::std::ops::Range> for $thing { type Output = [$ty]; #[inline] - fn index(&self, index: ::std::ops::Range) -> &[$ty] { + fn index(&self, index: $crate::std::ops::Range) -> &[$ty] { &self.0[index] } } - impl ::std::ops::Index<::std::ops::RangeTo> for $thing { + impl $crate::std::ops::Index<$crate::std::ops::RangeTo> for $thing { type Output = [$ty]; #[inline] - fn index(&self, index: ::std::ops::RangeTo) -> &[$ty] { + fn index(&self, index: $crate::std::ops::RangeTo) -> &[$ty] { &self.0[index] } } - impl ::std::ops::Index<::std::ops::RangeFrom> for $thing { + impl $crate::std::ops::Index<$crate::std::ops::RangeFrom> for $thing { type Output = [$ty]; #[inline] - fn index(&self, index: ::std::ops::RangeFrom) -> &[$ty] { + fn index(&self, index: $crate::std::ops::RangeFrom) -> &[$ty] { &self.0[index] } } - impl ::std::ops::Index<::std::ops::RangeFull> for $thing { + impl $crate::std::ops::Index<$crate::std::ops::RangeFull> for $thing { type Output = [$ty]; #[inline] - fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] { + fn index(&self, _: $crate::std::ops::RangeFull) -> &[$ty] { &self.0[..] } } @@ -215,19 +218,19 @@ macro_rules! impl_index_newtype { macro_rules! display_from_debug { ($thing:ident) => { - impl ::std::fmt::Display for $thing { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { - ::std::fmt::Debug::fmt(self, f) + impl $crate::std::fmt::Display for $thing { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> Result<(), $crate::std::fmt::Error> { + $crate::std::fmt::Debug::fmt(self, f) } } } } #[cfg(test)] -macro_rules! hex_script (($s:expr) => (::blockdata::script::Script::from( as ::hashes::hex::FromHex>::from_hex($s).unwrap()))); +macro_rules! hex_script (($s:expr) => ($crate::blockdata::script::Script::from( as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()))); #[cfg(test)] -macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(& as ::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap())); +macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(& as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap())); macro_rules! serde_struct_impl { ($name:ident, $($fe:ident),*) => ( @@ -267,7 +270,7 @@ macro_rules! serde_struct_impl { impl<'de> $crate::serde::Deserialize<'de> for Enum { fn deserialize(deserializer: D) -> Result where - D: ::serde::de::Deserializer<'de>, + D: $crate::serde::de::Deserializer<'de>, { deserializer.deserialize_str(EnumVisitor) } @@ -623,8 +626,8 @@ macro_rules! serde_struct_human_string_impl { macro_rules! impl_bytes_newtype { ($t:ident, $len:expr) => ( - impl ::std::fmt::LowerHex for $t { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + impl $crate::std::fmt::LowerHex for $t { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { for &ch in self.0.iter() { write!(f, "{:02x}", ch)?; } @@ -632,17 +635,17 @@ macro_rules! impl_bytes_newtype { } } - impl ::std::fmt::Display for $t { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + impl $crate::std::fmt::Display for $t { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { fmt::LowerHex::fmt(self, f) } } - impl ::hashes::hex::FromHex for $t { - fn from_byte_iter(iter: I) -> Result - where I: Iterator> + - ExactSizeIterator + - DoubleEndedIterator, + impl $crate::hashes::hex::FromHex for $t { + fn from_byte_iter(iter: I) -> Result + where I: $crate::std::iter::Iterator> + + $crate::std::iter::ExactSizeIterator + + $crate::std::iter::DoubleEndedIterator, { if iter.len() == $len { let mut ret = [0; $len]; @@ -651,23 +654,23 @@ macro_rules! impl_bytes_newtype { } Ok($t(ret)) } else { - Err(::hashes::hex::Error::InvalidLength(2 * $len, 2 * iter.len())) + Err($crate::hashes::hex::Error::InvalidLength(2 * $len, 2 * iter.len())) } } } - impl ::std::str::FromStr for $t { - type Err = ::hashes::hex::Error; + impl $crate::std::str::FromStr for $t { + type Err = $crate::hashes::hex::Error; fn from_str(s: &str) -> Result { - hex::FromHex::from_hex(s) + $crate::hashes::hex::FromHex::from_hex(s) } } #[cfg(feature="serde")] - impl ::serde::Serialize for $t { - fn serialize(&self, s: S) -> Result { + impl $crate::serde::Serialize for $t { + fn serialize(&self, s: S) -> Result { if s.is_human_readable() { - s.serialize_str(&::hashes::hex::ToHex::to_hex(self)) + s.serialize_str(&$crate::hashes::hex::ToHex::to_hex(self)) } else { s.serialize_bytes(&self[..]) } @@ -675,34 +678,34 @@ macro_rules! impl_bytes_newtype { } #[cfg(feature="serde")] - impl<'de> ::serde::Deserialize<'de> for $t { - fn deserialize>(d: D) -> Result<$t, D::Error> { + impl<'de> $crate::serde::Deserialize<'de> for $t { + fn deserialize>(d: D) -> Result<$t, D::Error> { if d.is_human_readable() { struct HexVisitor; - impl<'de> ::serde::de::Visitor<'de> for HexVisitor { + impl<'de> $crate::serde::de::Visitor<'de> for HexVisitor { type Value = $t; - fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fn expecting(&self, formatter: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { formatter.write_str("an ASCII hex string") } fn visit_bytes(self, v: &[u8]) -> Result where - E: ::serde::de::Error, + E: $crate::serde::de::Error, { - if let Ok(hex) = ::std::str::from_utf8(v) { - ::hashes::hex::FromHex::from_hex(hex).map_err(E::custom) + if let Ok(hex) = $crate::std::str::from_utf8(v) { + $crate::hashes::hex::FromHex::from_hex(hex).map_err(E::custom) } else { - return Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self)); + return Err(E::invalid_value($crate::serde::de::Unexpected::Bytes(v), &self)); } } fn visit_str(self, v: &str) -> Result where - E: ::serde::de::Error, + E: $crate::serde::de::Error, { - ::hashes::hex::FromHex::from_hex(v).map_err(E::custom) + $crate::hashes::hex::FromHex::from_hex(v).map_err(E::custom) } } @@ -710,16 +713,16 @@ macro_rules! impl_bytes_newtype { } else { struct BytesVisitor; - impl<'de> ::serde::de::Visitor<'de> for BytesVisitor { + impl<'de> $crate::serde::de::Visitor<'de> for BytesVisitor { type Value = $t; - fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fn expecting(&self, formatter: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { formatter.write_str("a bytestring") } fn visit_bytes(self, v: &[u8]) -> Result where - E: ::serde::de::Error, + E: $crate::serde::de::Error, { if v.len() != $len { Err(E::invalid_length(v.len(), &stringify!($len))) @@ -751,30 +754,30 @@ macro_rules! user_enum { $(#[$doc] $elem),* } - impl ::std::fmt::Debug for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl $crate::std::fmt::Debug for $name { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { f.pad(match *self { $($name::$elem => $txt),* }) } } - impl ::std::fmt::Display for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl $crate::std::fmt::Display for $name { + fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result { f.pad(match *self { $($name::$elem => $txt),* }) } } - impl ::std::str::FromStr for $name { - type Err = ::std::io::Error; + impl $crate::std::str::FromStr for $name { + type Err = $crate::std::io::Error; #[inline] fn from_str(s: &str) -> Result { match s { $($txt => Ok($name::$elem)),*, - _ => Err(::std::io::Error::new( - ::std::io::ErrorKind::InvalidInput, + _ => Err($crate::std::io::Error::new( + $crate::std::io::ErrorKind::InvalidInput, format!("Unknown network (type {})", s), )), } @@ -831,10 +834,10 @@ macro_rules! user_enum { } #[cfg(feature = "serde")] - impl ::serde::Serialize for $name { + impl $crate::serde::Serialize for $name { fn serialize(&self, serializer: S) -> Result where - S: ::serde::Serializer, + S: $crate::serde::Serializer, { serializer.collect_str(&self) } diff --git a/src/util/bip32.rs b/src/util/bip32.rs index 2e8103e7..0288484a 100644 --- a/src/util/bip32.rs +++ b/src/util/bip32.rs @@ -22,7 +22,7 @@ use std::str::FromStr; #[cfg(feature = "serde")] use serde; use hash_types::XpubIdentifier; -use hashes::{hex, sha512, Hash, HashEngine, Hmac, HmacEngine}; +use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine}; use secp256k1::{self, Secp256k1}; use network::constants::Network;