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 {
($thing:ident, $($field:ident),+) => (
impl ::consensus::Encodable for $thing {
impl $crate::consensus::Encodable for $thing {
#[inline]
fn consensus_encode<S: ::std::io::Write>(
fn consensus_encode<S: $crate::std::io::Write>(
&self,
mut s: S,
) -> Result<usize, ::consensus::encode::Error> {
) -> Result<usize, $crate::consensus::encode::Error> {
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<D: ::std::io::Read>(
fn consensus_decode<D: $crate::std::io::Read>(
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<usize> for $thing {
impl $crate::std::ops::Index<usize> 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<H>(&self, state: &mut H)
where H: ::std::hash::Hasher
where H: $crate::std::hash::Hasher
{
(&self[..]).hash(state);
}
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() {
(&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<usize>> for $thing {
impl $crate::std::ops::Index<$crate::std::ops::Range<usize>> for $thing {
type Output = [$ty];
#[inline]
fn index(&self, index: ::std::ops::Range<usize>) -> &[$ty] {
fn index(&self, index: $crate::std::ops::Range<usize>) -> &[$ty] {
&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];
#[inline]
fn index(&self, index: ::std::ops::RangeTo<usize>) -> &[$ty] {
fn index(&self, index: $crate::std::ops::RangeTo<usize>) -> &[$ty] {
&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];
#[inline]
fn index(&self, index: ::std::ops::RangeFrom<usize>) -> &[$ty] {
fn index(&self, index: $crate::std::ops::RangeFrom<usize>) -> &[$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(<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)]
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 {
($name:ident, $($fe:ident),*) => (
@ -267,7 +270,7 @@ macro_rules! serde_struct_impl {
impl<'de> $crate::serde::Deserialize<'de> for Enum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<I>(iter: I) -> Result<Self, ::hashes::hex::Error>
where I: Iterator<Item=Result<u8, ::hashes::hex::Error>> +
ExactSizeIterator +
DoubleEndedIterator,
impl $crate::hashes::hex::FromHex for $t {
fn from_byte_iter<I>(iter: I) -> Result<Self, $crate::hashes::hex::Error>
where I: $crate::std::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>> +
$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<Self, Self::Err> {
hex::FromHex::from_hex(s)
$crate::hashes::hex::FromHex::from_hex(s)
}
}
#[cfg(feature="serde")]
impl ::serde::Serialize for $t {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
impl $crate::serde::Serialize for $t {
fn serialize<S: $crate::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
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: ::serde::Deserializer<'de>>(d: D) -> Result<$t, D::Error> {
impl<'de> $crate::serde::Deserialize<'de> for $t {
fn deserialize<D: $crate::serde::Deserializer<'de>>(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<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<E>(self, v: &str) -> Result<Self::Value, E>
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<E>(self, v: &[u8]) -> Result<Self::Value, E>
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<Self, Self::Err> {
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
S: $crate::serde::Serializer,
{
serializer.collect_str(&self)
}

View File

@ -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;