Namespace hygiene for internal macros

This commit is contained in:
Dr Maxim Orlovsky 2020-01-25 05:19:46 +01:00
parent 9cb5d2e711
commit 3a5e8d8504
2 changed files with 82 additions and 79 deletions

View File

@ -18,31 +18,32 @@
macro_rules! impl_consensus_encoding { macro_rules! impl_consensus_encoding {
($thing:ident, $($field:ident),+) => ( ($thing:ident, $($field:ident),+) => (
impl ::consensus::Encodable for $thing { impl $crate::consensus::Encodable for $thing {
#[inline] #[inline]
fn consensus_encode<S: ::std::io::Write>( fn consensus_encode<S: $crate::std::io::Write>(
&self, &self,
mut s: S, mut s: S,
) -> Result<usize, ::consensus::encode::Error> { ) -> Result<usize, $crate::consensus::encode::Error> {
let mut len = 0; let mut len = 0;
$(len += self.$field.consensus_encode(&mut s)?;)+ $(len += self.$field.consensus_encode(&mut s)?;)+
Ok(len) Ok(len)
} }
} }
impl ::consensus::Decodable for $thing { impl $crate::consensus::Decodable for $thing {
#[inline] #[inline]
fn consensus_decode<D: ::std::io::Read>( fn consensus_decode<D: $crate::std::io::Read>(
mut d: D, mut d: D,
) -> Result<$thing, ::consensus::encode::Error> { ) -> Result<$thing, $crate::consensus::encode::Error> {
Ok($thing { 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 { macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => { ($thing:ident, $ty:ty, $len:expr) => {
impl $thing { impl $thing {
@ -81,7 +82,7 @@ macro_rules! impl_array_newtype {
pub fn into_bytes(self) -> [$ty; $len] { self.0 } 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 { fn from(data: &'a [$ty]) -> $thing {
assert_eq!(data.len(), $len); assert_eq!(data.len(), $len);
let mut ret = [0; $len]; let mut ret = [0; $len];
@ -90,7 +91,7 @@ macro_rules! impl_array_newtype {
} }
} }
impl ::std::ops::Index<usize> for $thing { impl $crate::std::ops::Index<usize> for $thing {
type Output = $ty; type Output = $ty;
#[inline] #[inline]
@ -102,57 +103,57 @@ macro_rules! impl_array_newtype {
impl_index_newtype!($thing, $ty); impl_index_newtype!($thing, $ty);
impl PartialEq for $thing { impl $crate::std::cmp::PartialEq for $thing {
#[inline] #[inline]
fn eq(&self, other: &$thing) -> bool { fn eq(&self, other: &$thing) -> bool {
&self[..] == &other[..] &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] #[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)) Some(self.cmp(&other))
} }
} }
impl Ord for $thing { impl $crate::std::cmp::Ord for $thing {
#[inline] #[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 // manually implement comparison to get little-endian ordering
// (we need this for our numeric types; non-numeric ones shouldn't // (we need this for our numeric types; non-numeric ones shouldn't
// be ordered anyway except to put them in BTrees or whatever, and // 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). // they don't care how we order as long as we're consistent).
for i in 0..$len { 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 $crate::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::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 #[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] #[inline]
fn clone(&self) -> $thing { fn clone(&self) -> $thing {
$thing::from(&self[..]) $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] #[inline]
fn hash<H>(&self, state: &mut H) fn hash<H>(&self, state: &mut H)
where H: ::std::hash::Hasher where H: $crate::std::hash::Hasher
{ {
(&self[..]).hash(state); (&self[..]).hash(state);
} }
fn hash_slice<H>(data: &[$thing], state: &mut H) fn hash_slice<H>(data: &[$thing], state: &mut H)
where H: ::std::hash::Hasher where H: $crate::std::hash::Hasher
{ {
for d in data.iter() { for d in data.iter() {
(&d[..]).hash(state); (&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 { macro_rules! impl_array_newtype_show {
($thing:ident) => { ($thing:ident) => {
impl ::std::fmt::Debug for $thing { impl $crate::std::fmt::Debug for $thing {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result {
write!(f, concat!(stringify!($thing), "({:?})"), &self[..]) write!(f, concat!(stringify!($thing), "({:?})"), &self[..])
} }
} }
} }
} }
/// Implements standard indexing methods for a given wrapper type
macro_rules! impl_index_newtype { macro_rules! impl_index_newtype {
($thing:ident, $ty:ty) => { ($thing:ident, $ty:ty) => {
impl ::std::ops::Index<::std::ops::Range<usize>> for $thing { impl $crate::std::ops::Index<$crate::std::ops::Range<usize>> for $thing {
type Output = [$ty]; type Output = [$ty];
#[inline] #[inline]
fn index(&self, index: ::std::ops::Range<usize>) -> &[$ty] { fn index(&self, index: $crate::std::ops::Range<usize>) -> &[$ty] {
&self.0[index] &self.0[index]
} }
} }
impl ::std::ops::Index<::std::ops::RangeTo<usize>> for $thing { impl $crate::std::ops::Index<$crate::std::ops::RangeTo<usize>> for $thing {
type Output = [$ty]; type Output = [$ty];
#[inline] #[inline]
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[$ty] { fn index(&self, index: $crate::std::ops::RangeTo<usize>) -> &[$ty] {
&self.0[index] &self.0[index]
} }
} }
impl ::std::ops::Index<::std::ops::RangeFrom<usize>> for $thing { impl $crate::std::ops::Index<$crate::std::ops::RangeFrom<usize>> for $thing {
type Output = [$ty]; type Output = [$ty];
#[inline] #[inline]
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[$ty] { fn index(&self, index: $crate::std::ops::RangeFrom<usize>) -> &[$ty] {
&self.0[index] &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]; type Output = [$ty];
#[inline] #[inline]
fn index(&self, _: ::std::ops::RangeFull) -> &[$ty] { fn index(&self, _: $crate::std::ops::RangeFull) -> &[$ty] {
&self.0[..] &self.0[..]
} }
} }
@ -215,19 +218,19 @@ macro_rules! impl_index_newtype {
macro_rules! display_from_debug { macro_rules! display_from_debug {
($thing:ident) => { ($thing:ident) => {
impl ::std::fmt::Display for $thing { impl $crate::std::fmt::Display for $thing {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> Result<(), $crate::std::fmt::Error> {
::std::fmt::Debug::fmt(self, f) $crate::std::fmt::Debug::fmt(self, f)
} }
} }
} }
} }
#[cfg(test)] #[cfg(test)]
macro_rules! hex_script (($s:expr) => (::blockdata::script::Script::from(<Vec<u8> as ::hashes::hex::FromHex>::from_hex($s).unwrap()))); macro_rules! hex_script (($s:expr) => ($crate::blockdata::script::Script::from(<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap())));
#[cfg(test)] #[cfg(test)]
macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<Vec<u8> as ::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap())); macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));
macro_rules! serde_struct_impl { macro_rules! serde_struct_impl {
($name:ident, $($fe:ident),*) => ( ($name:ident, $($fe:ident),*) => (
@ -267,7 +270,7 @@ macro_rules! serde_struct_impl {
impl<'de> $crate::serde::Deserialize<'de> for Enum { impl<'de> $crate::serde::Deserialize<'de> for Enum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: ::serde::de::Deserializer<'de>, D: $crate::serde::de::Deserializer<'de>,
{ {
deserializer.deserialize_str(EnumVisitor) deserializer.deserialize_str(EnumVisitor)
} }
@ -623,8 +626,8 @@ macro_rules! serde_struct_human_string_impl {
macro_rules! impl_bytes_newtype { macro_rules! impl_bytes_newtype {
($t:ident, $len:expr) => ( ($t:ident, $len:expr) => (
impl ::std::fmt::LowerHex for $t { impl $crate::std::fmt::LowerHex for $t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result {
for &ch in self.0.iter() { for &ch in self.0.iter() {
write!(f, "{:02x}", ch)?; write!(f, "{:02x}", ch)?;
} }
@ -632,17 +635,17 @@ macro_rules! impl_bytes_newtype {
} }
} }
impl ::std::fmt::Display for $t { impl $crate::std::fmt::Display for $t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result {
fmt::LowerHex::fmt(self, f) fmt::LowerHex::fmt(self, f)
} }
} }
impl ::hashes::hex::FromHex for $t { impl $crate::hashes::hex::FromHex for $t {
fn from_byte_iter<I>(iter: I) -> Result<Self, ::hashes::hex::Error> fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hashes::hex::Error>
where I: Iterator<Item=Result<u8, ::hashes::hex::Error>> + where I: $crate::std::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>> +
ExactSizeIterator + $crate::std::iter::ExactSizeIterator +
DoubleEndedIterator, $crate::std::iter::DoubleEndedIterator,
{ {
if iter.len() == $len { if iter.len() == $len {
let mut ret = [0; $len]; let mut ret = [0; $len];
@ -651,23 +654,23 @@ macro_rules! impl_bytes_newtype {
} }
Ok($t(ret)) Ok($t(ret))
} else { } 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 { impl $crate::std::str::FromStr for $t {
type Err = ::hashes::hex::Error; type Err = $crate::hashes::hex::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
hex::FromHex::from_hex(s) $crate::hashes::hex::FromHex::from_hex(s)
} }
} }
#[cfg(feature="serde")] #[cfg(feature="serde")]
impl ::serde::Serialize for $t { impl $crate::serde::Serialize for $t {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { fn serialize<S: $crate::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() { 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 { } else {
s.serialize_bytes(&self[..]) s.serialize_bytes(&self[..])
} }
@ -675,34 +678,34 @@ macro_rules! impl_bytes_newtype {
} }
#[cfg(feature="serde")] #[cfg(feature="serde")]
impl<'de> ::serde::Deserialize<'de> for $t { impl<'de> $crate::serde::Deserialize<'de> for $t {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> { fn deserialize<D: $crate::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
if d.is_human_readable() { if d.is_human_readable() {
struct HexVisitor; struct HexVisitor;
impl<'de> ::serde::de::Visitor<'de> for HexVisitor { impl<'de> $crate::serde::de::Visitor<'de> for HexVisitor {
type Value = $t; 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") formatter.write_str("an ASCII hex string")
} }
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where where
E: ::serde::de::Error, E: $crate::serde::de::Error,
{ {
if let Ok(hex) = ::std::str::from_utf8(v) { if let Ok(hex) = $crate::std::str::from_utf8(v) {
::hashes::hex::FromHex::from_hex(hex).map_err(E::custom) $crate::hashes::hex::FromHex::from_hex(hex).map_err(E::custom)
} else { } 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<E>(self, v: &str) -> Result<Self::Value, E> fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where 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 { } else {
struct BytesVisitor; struct BytesVisitor;
impl<'de> ::serde::de::Visitor<'de> for BytesVisitor { impl<'de> $crate::serde::de::Visitor<'de> for BytesVisitor {
type Value = $t; 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") formatter.write_str("a bytestring")
} }
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where where
E: ::serde::de::Error, E: $crate::serde::de::Error,
{ {
if v.len() != $len { if v.len() != $len {
Err(E::invalid_length(v.len(), &stringify!($len))) Err(E::invalid_length(v.len(), &stringify!($len)))
@ -751,30 +754,30 @@ macro_rules! user_enum {
$(#[$doc] $elem),* $(#[$doc] $elem),*
} }
impl ::std::fmt::Debug for $name { impl $crate::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result {
f.pad(match *self { f.pad(match *self {
$($name::$elem => $txt),* $($name::$elem => $txt),*
}) })
} }
} }
impl ::std::fmt::Display for $name { impl $crate::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { fn fmt(&self, f: &mut $crate::std::fmt::Formatter) -> $crate::std::fmt::Result {
f.pad(match *self { f.pad(match *self {
$($name::$elem => $txt),* $($name::$elem => $txt),*
}) })
} }
} }
impl ::std::str::FromStr for $name { impl $crate::std::str::FromStr for $name {
type Err = ::std::io::Error; type Err = $crate::std::io::Error;
#[inline] #[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
$($txt => Ok($name::$elem)),*, $($txt => Ok($name::$elem)),*,
_ => Err(::std::io::Error::new( _ => Err($crate::std::io::Error::new(
::std::io::ErrorKind::InvalidInput, $crate::std::io::ErrorKind::InvalidInput,
format!("Unknown network (type {})", s), format!("Unknown network (type {})", s),
)), )),
} }
@ -831,10 +834,10 @@ macro_rules! user_enum {
} }
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
impl ::serde::Serialize for $name { impl $crate::serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: ::serde::Serializer, S: $crate::serde::Serializer,
{ {
serializer.collect_str(&self) serializer.collect_str(&self)
} }

View File

@ -22,7 +22,7 @@ use std::str::FromStr;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
use hash_types::XpubIdentifier; use hash_types::XpubIdentifier;
use hashes::{hex, sha512, Hash, HashEngine, Hmac, HmacEngine}; use hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
use secp256k1::{self, Secp256k1}; use secp256k1::{self, Secp256k1};
use network::constants::Network; use network::constants::Network;