Merge pull request #403 from LNP-BP/fix-macro-ns

Hygiene/single code style for all existing macros
This commit is contained in:
Elichai Turkel 2020-08-09 18:16:36 +03:00 committed by GitHub
commit e8bcde4d38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 80 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>(
&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>(
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> ::std::convert::From<&'a [$ty]> for $thing {
fn from(data: &'a [$ty]) -> $thing {
assert_eq!(data.len(), $len);
let mut ret = [0; $len];
@ -102,23 +103,23 @@ macro_rules! impl_array_newtype {
impl_index_newtype!($thing, $ty);
impl PartialEq for $thing {
impl ::std::cmp::PartialEq for $thing {
#[inline]
fn eq(&self, other: &$thing) -> bool {
&self[..] == &other[..]
}
}
impl Eq for $thing {}
impl ::std::cmp::Eq for $thing {}
impl PartialOrd for $thing {
impl ::std::cmp::PartialOrd for $thing {
#[inline]
fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
impl Ord for $thing {
impl ::std::cmp::Ord for $thing {
#[inline]
fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering {
// manually implement comparison to get little-endian ordering
@ -134,14 +135,14 @@ macro_rules! impl_array_newtype {
}
#[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 ::std::clone::Clone for $thing {
#[inline]
fn clone(&self) -> $thing {
$thing::from(&self[..])
}
}
impl Copy for $thing {}
impl ::std::marker::Copy for $thing {}
impl ::std::hash::Hash for $thing {
#[inline]
@ -162,6 +163,7 @@ 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 {
@ -172,6 +174,7 @@ macro_rules! impl_array_newtype_show {
}
}
/// 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 {
@ -224,10 +227,10 @@ macro_rules! display_from_debug {
}
#[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),*) => (
@ -237,7 +240,7 @@ macro_rules! serde_struct_impl {
where
D: $crate::serde::de::Deserializer<'de>,
{
use $crate::std::fmt::{self, Formatter};
use ::std::fmt::{self, Formatter};
use $crate::serde::de::IgnoredAny;
#[allow(non_camel_case_types)]
@ -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)
}
@ -379,8 +382,8 @@ macro_rules! serde_string_impl {
where
D: $crate::serde::de::Deserializer<'de>,
{
use $crate::std::fmt::{self, Formatter};
use $crate::std::str::FromStr;
use ::std::fmt::{self, Formatter};
use ::std::str::FromStr;
struct Visitor;
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
@ -440,8 +443,8 @@ macro_rules! serde_struct_human_string_impl {
D: $crate::serde::de::Deserializer<'de>,
{
if deserializer.is_human_readable() {
use $crate::std::fmt::{self, Formatter};
use $crate::std::str::FromStr;
use ::std::fmt::{self, Formatter};
use ::std::str::FromStr;
struct Visitor;
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
@ -475,7 +478,7 @@ macro_rules! serde_struct_human_string_impl {
deserializer.deserialize_str(Visitor)
} else {
use $crate::std::fmt::{self, Formatter};
use ::std::fmt::{self, Formatter};
use $crate::serde::de::IgnoredAny;
#[allow(non_camel_case_types)]
@ -624,7 +627,7 @@ macro_rules! impl_bytes_newtype {
($t:ident, $len:expr) => (
impl ::std::fmt::LowerHex for $t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
for &ch in self.0.iter() {
write!(f, "{:02x}", ch)?;
}
@ -633,16 +636,16 @@ macro_rules! impl_bytes_newtype {
}
impl ::std::fmt::Display for $t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::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: ::std::iter::Iterator<Item=Result<u8, $crate::hashes::hex::Error>> +
::std::iter::ExactSizeIterator +
::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;
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,12 +678,12 @@ 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 {
@ -689,20 +692,20 @@ macro_rules! impl_bytes_newtype {
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)
$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,7 +713,7 @@ 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 {
@ -719,7 +722,7 @@ macro_rules! impl_bytes_newtype {
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)))
@ -788,7 +791,7 @@ macro_rules! user_enum {
where
D: $crate::serde::Deserializer<'de>,
{
use $crate::std::fmt::{self, Formatter};
use ::std::fmt::{self, Formatter};
struct Visitor;
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
@ -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;

View File

@ -14,7 +14,7 @@
#[allow(unused_macros)]
macro_rules! hex_psbt {
($s:expr) => { ::consensus::deserialize(&<Vec<u8> as ::hashes::hex::FromHex>::from_hex($s).unwrap()) };
($s:expr) => { $crate::consensus::deserialize(&<Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()) };
}
macro_rules! merge {
@ -34,9 +34,9 @@ macro_rules! impl_psbt_de_serialize {
macro_rules! impl_psbt_deserialize {
($thing:ty) => {
impl ::util::psbt::serialize::Deserialize for $thing {
fn deserialize(bytes: &[u8]) -> Result<Self, ::consensus::encode::Error> {
::consensus::deserialize(&bytes[..])
impl $crate::util::psbt::serialize::Deserialize for $thing {
fn deserialize(bytes: &[u8]) -> Result<Self, $crate::consensus::encode::Error> {
$crate::consensus::deserialize(&bytes[..])
}
}
};
@ -44,9 +44,9 @@ macro_rules! impl_psbt_deserialize {
macro_rules! impl_psbt_serialize {
($thing:ty) => {
impl ::util::psbt::serialize::Serialize for $thing {
impl $crate::util::psbt::serialize::Serialize for $thing {
fn serialize(&self) -> Vec<u8> {
::consensus::serialize(self)
$crate::consensus::serialize(self)
}
}
};
@ -54,20 +54,20 @@ macro_rules! impl_psbt_serialize {
macro_rules! impl_psbtmap_consensus_encoding {
($thing:ty) => {
impl ::consensus::Encodable for $thing {
impl $crate::consensus::Encodable for $thing {
fn consensus_encode<S: ::std::io::Write>(
&self,
mut s: S,
) -> Result<usize, ::consensus::encode::Error> {
) -> Result<usize, $crate::consensus::encode::Error> {
let mut len = 0;
for pair in ::util::psbt::Map::get_pairs(self)? {
len += ::consensus::Encodable::consensus_encode(
for pair in $crate::util::psbt::Map::get_pairs(self)? {
len += $crate::consensus::Encodable::consensus_encode(
&pair,
&mut s,
)?;
}
Ok(len + ::consensus::Encodable::consensus_encode(&0x00_u8, s)?)
Ok(len + $crate::consensus::Encodable::consensus_encode(&0x00_u8, s)?)
}
}
};
@ -75,16 +75,16 @@ macro_rules! impl_psbtmap_consensus_encoding {
macro_rules! impl_psbtmap_consensus_decoding {
($thing:ty) => {
impl ::consensus::Decodable for $thing {
impl $crate::consensus::Decodable for $thing {
fn consensus_decode<D: ::std::io::Read>(
mut d: D,
) -> Result<Self, ::consensus::encode::Error> {
) -> Result<Self, $crate::consensus::encode::Error> {
let mut rv: Self = ::std::default::Default::default();
loop {
match ::consensus::Decodable::consensus_decode(&mut d) {
Ok(pair) => ::util::psbt::Map::insert_pair(&mut rv, pair)?,
Err(::consensus::encode::Error::Psbt(::util::psbt::Error::NoMorePairs)) => return Ok(rv),
match $crate::consensus::Decodable::consensus_decode(&mut d) {
Ok(pair) => $crate::util::psbt::Map::insert_pair(&mut rv, pair)?,
Err($crate::consensus::encode::Error::Psbt($crate::util::psbt::Error::NoMorePairs)) => return Ok(rv),
Err(e) => return Err(e),
}
}
@ -105,29 +105,27 @@ macro_rules! impl_psbt_insert_pair {
($slf:ident.$unkeyed_name:ident <= <$raw_key:ident: _>|<$raw_value:ident: $unkeyed_value_type:ty>) => {
if $raw_key.key.is_empty() {
if $slf.$unkeyed_name.is_none() {
let val: $unkeyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
let val: $unkeyed_value_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
$slf.$unkeyed_name = Some(val)
} else {
return Err(::util::psbt::Error::DuplicateKey($raw_key).into());
return Err($crate::util::psbt::Error::DuplicateKey($raw_key).into());
}
} else {
return Err(::util::psbt::Error::InvalidKey($raw_key).into());
return Err($crate::util::psbt::Error::InvalidKey($raw_key).into());
}
};
($slf:ident.$keyed_name:ident <= <$raw_key:ident: $keyed_key_type:ty>|<$raw_value:ident: $keyed_value_type:ty>) => {
if !$raw_key.key.is_empty() {
let key_val: $keyed_key_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?;
let key_val: $keyed_key_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?;
match $slf.$keyed_name.entry(key_val) {
::std::collections::btree_map::Entry::Vacant(empty_key) => {
let val: $keyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
let val: $keyed_value_type = $crate::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
empty_key.insert(val);
}
::std::collections::btree_map::Entry::Occupied(_) => return Err(::util::psbt::Error::DuplicateKey($raw_key).into()),
::std::collections::btree_map::Entry::Occupied(_) => return Err($crate::util::psbt::Error::DuplicateKey($raw_key).into()),
}
} else {
return Err(::util::psbt::Error::InvalidKey($raw_key).into());
return Err($crate::util::psbt::Error::InvalidKey($raw_key).into());
}
};
}
@ -136,23 +134,23 @@ macro_rules! impl_psbt_insert_pair {
macro_rules! impl_psbt_get_pair {
($rv:ident.push($slf:ident.$unkeyed_name:ident as <$unkeyed_typeval:expr, _>|<$unkeyed_value_type:ty>)) => {
if let Some(ref $unkeyed_name) = $slf.$unkeyed_name {
$rv.push(::util::psbt::raw::Pair {
key: ::util::psbt::raw::Key {
$rv.push($crate::util::psbt::raw::Pair {
key: $crate::util::psbt::raw::Key {
type_value: $unkeyed_typeval,
key: vec![],
},
value: ::util::psbt::serialize::Serialize::serialize($unkeyed_name),
value: $crate::util::psbt::serialize::Serialize::serialize($unkeyed_name),
});
}
};
($rv:ident.push($slf:ident.$keyed_name:ident as <$keyed_typeval:expr, $keyed_key_type:ty>|<$keyed_value_type:ty>)) => {
for (key, val) in &$slf.$keyed_name {
$rv.push(::util::psbt::raw::Pair {
key: ::util::psbt::raw::Key {
$rv.push($crate::util::psbt::raw::Pair {
key: $crate::util::psbt::raw::Key {
type_value: $keyed_typeval,
key: ::util::psbt::serialize::Serialize::serialize(key),
key: $crate::util::psbt::serialize::Serialize::serialize(key),
},
value: ::util::psbt::serialize::Serialize::serialize(val),
value: $crate::util::psbt::serialize::Serialize::serialize(val),
});
}
};