Merge rust-bitcoin/rust-bitcoin#1293: Move a bunch of stuff out of `internal_macros`
7d851b42ee
Move serde_string_* macros to the serde_utils module (Tobin C. Harding)53b681b838
Move const_assert to bitcoin_internals (Tobin C. Harding)5a8a5ff6c9
Move debug_from_display to bitcoin_internals (Tobin C. Harding)a2f08f2bc6
Improve docs on impl_array_newtype macro (Tobin C. Harding)771cdde282
Move impl_array_newtype to bitcoin_internals (Tobin C. Harding) Pull request description: Move macros out of `internal_macros`, done in an effort to work towards removing the `internal_macros` module since we have `bitcoin_internals` now. ACKs for top commit: apoelstra: ACK7d851b42ee
Kixunil: ACK7d851b42ee
Tree-SHA512: b31b3a5b4d18a2dbe3f358bff62ae6ca4041d432c755e9c45b0241d48903e02c95e79ec72a7478b9d2a53486ce9eef19bfe3b8905aba19036e59c0719f193ce7
This commit is contained in:
commit
01d5129d79
|
@ -39,7 +39,6 @@ use crate::blockdata::{opcodes, script};
|
|||
use crate::error::ParseIntError;
|
||||
use crate::hash_types::{PubkeyHash, ScriptHash};
|
||||
use crate::hashes::{sha256, Hash, HashEngine};
|
||||
use crate::internal_macros::serde_string_impl;
|
||||
use crate::network::constants::Network;
|
||||
use crate::prelude::*;
|
||||
use crate::util::base58;
|
||||
|
@ -565,7 +564,8 @@ pub struct Address {
|
|||
/// The network on which this address is usable.
|
||||
pub network: Network,
|
||||
}
|
||||
serde_string_impl!(Address, "a Bitcoin address");
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(Address, "a Bitcoin address");
|
||||
|
||||
impl Address {
|
||||
/// Creates a pay to (compressed) public key hash address from a public key.
|
||||
|
|
|
@ -10,9 +10,11 @@ use core::{convert, fmt, mem};
|
|||
#[cfg(feature = "std")]
|
||||
use std::error;
|
||||
|
||||
use bitcoin_internals::impl_array_newtype;
|
||||
|
||||
use crate::consensus::encode::{self, Decodable, Encodable, VarInt};
|
||||
use crate::hashes::{sha256, siphash24, Hash};
|
||||
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, impl_consensus_encoding};
|
||||
use crate::internal_macros::{impl_bytes_newtype, impl_consensus_encoding};
|
||||
use crate::prelude::*;
|
||||
use crate::util::endian;
|
||||
use crate::{io, Block, BlockHash, BlockHeader, Transaction};
|
||||
|
|
|
@ -12,14 +12,14 @@ use core::fmt;
|
|||
use core::ops::Index;
|
||||
use core::str::FromStr;
|
||||
|
||||
use bitcoin_internals::write_err;
|
||||
use bitcoin_internals::{impl_array_newtype, write_err};
|
||||
use secp256k1::{self, Secp256k1, XOnlyPublicKey};
|
||||
#[cfg(feature = "serde")]
|
||||
use serde;
|
||||
|
||||
use crate::hash_types::XpubIdentifier;
|
||||
use crate::hashes::{hex, sha512, Hash, HashEngine, Hmac, HmacEngine};
|
||||
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl};
|
||||
use crate::internal_macros::impl_bytes_newtype;
|
||||
use crate::io::Write;
|
||||
use crate::network::constants::Network;
|
||||
use crate::prelude::*;
|
||||
|
@ -55,7 +55,8 @@ pub struct ExtendedPrivKey {
|
|||
/// Chain code
|
||||
pub chain_code: ChainCode,
|
||||
}
|
||||
serde_string_impl!(ExtendedPrivKey, "a BIP-32 extended private key");
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(ExtendedPrivKey, "a BIP-32 extended private key");
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
#[cfg_attr(docsrs, doc(cfg(not(feature = "std"))))]
|
||||
|
@ -88,7 +89,8 @@ pub struct ExtendedPubKey {
|
|||
/// Chain code
|
||||
pub chain_code: ChainCode,
|
||||
}
|
||||
serde_string_impl!(ExtendedPubKey, "a BIP-32 extended public key");
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(ExtendedPubKey, "a BIP-32 extended public key");
|
||||
|
||||
/// A child number for a derived key
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
|
||||
|
@ -233,7 +235,9 @@ pub trait IntoDerivationPath {
|
|||
/// A BIP-32 derivation path.
|
||||
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
|
||||
pub struct DerivationPath(Vec<ChildNumber>);
|
||||
serde_string_impl!(DerivationPath, "a BIP-32 derivation path");
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(DerivationPath, "a BIP-32 derivation path");
|
||||
|
||||
impl<I> Index<I> for DerivationPath
|
||||
where
|
||||
|
|
|
@ -12,6 +12,8 @@ use crate::prelude::*;
|
|||
|
||||
use core::default::Default;
|
||||
|
||||
use bitcoin_internals::impl_array_newtype;
|
||||
|
||||
use crate::hashes::hex::{self, HexIterator};
|
||||
use crate::hashes::{Hash, sha256d};
|
||||
use crate::blockdata::opcodes;
|
||||
|
@ -22,7 +24,7 @@ use crate::blockdata::block::{Block, BlockHeader, BlockVersion};
|
|||
use crate::blockdata::witness::Witness;
|
||||
use crate::network::constants::Network;
|
||||
use crate::pow::CompactTarget;
|
||||
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype};
|
||||
use crate::internal_macros::impl_bytes_newtype;
|
||||
|
||||
/// How many satoshis are in "one bitcoin"
|
||||
pub const COIN_VALUE: u64 = 100_000_000;
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#[cfg(feature = "serde")] use crate::prelude::*;
|
||||
|
||||
use core::{fmt, convert::From};
|
||||
use crate::internal_macros::debug_from_display;
|
||||
use bitcoin_internals::debug_from_display;
|
||||
|
||||
// Note: I am deliberately not implementing PartialOrd or Ord on the
|
||||
// opcode enum. If you want to check ranges of opcodes, etc.,
|
||||
|
|
|
@ -18,7 +18,7 @@ use crate::io;
|
|||
use core::convert::TryFrom;
|
||||
use core::{fmt, default::Default};
|
||||
use core::ops::Index;
|
||||
use crate::internal_macros::debug_from_display;
|
||||
use bitcoin_internals::debug_from_display;
|
||||
#[cfg(feature = "bitcoinconsensus")]
|
||||
use bitcoin_internals::write_err;
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ use crate::blockdata::locktime::relative;
|
|||
use crate::consensus::{encode, Decodable, Encodable};
|
||||
use crate::hash_types::{Sighash, Txid, Wtxid};
|
||||
use crate::VarInt;
|
||||
use crate::internal_macros::{impl_consensus_encoding, serde_struct_human_string_impl};
|
||||
use crate::internal_macros::impl_consensus_encoding;
|
||||
use crate::parse::impl_parse_str_through_int;
|
||||
|
||||
#[cfg(doc)]
|
||||
|
@ -50,7 +50,8 @@ pub struct OutPoint {
|
|||
/// The index of the referenced output in its transaction's vout.
|
||||
pub vout: u32,
|
||||
}
|
||||
serde_struct_human_string_impl!(OutPoint, "an OutPoint", txid, vout);
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_struct_human_string_impl!(OutPoint, "an OutPoint", txid, vout);
|
||||
|
||||
impl OutPoint {
|
||||
/// Creates a new [`OutPoint`].
|
||||
|
|
|
@ -219,7 +219,7 @@ macro_rules! decoder_fn {
|
|||
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
|
||||
#[inline]
|
||||
fn $name(&mut self) -> Result<$val_type, Error> {
|
||||
$crate::internal_macros::const_assert!(::core::mem::size_of::<$val_type>() == $byte_len);
|
||||
bitcoin_internals::const_assert!(core::mem::size_of::<$val_type>() == $byte_len);
|
||||
let mut val = [0; $byte_len];
|
||||
self.read_exact(&mut val[..]).map_err(Error::Io)?;
|
||||
Ok(endian::$readfn(&val))
|
||||
|
|
|
@ -45,78 +45,6 @@ macro_rules! impl_consensus_encoding {
|
|||
);
|
||||
}
|
||||
pub(crate) use impl_consensus_encoding;
|
||||
|
||||
/// Implements standard array methods for a given wrapper type
|
||||
macro_rules! impl_array_newtype {
|
||||
($thing:ident, $ty:ty, $len:literal) => {
|
||||
impl $thing {
|
||||
/// Converts the object to a raw pointer
|
||||
#[inline]
|
||||
pub fn as_ptr(&self) -> *const $ty {
|
||||
let &$thing(ref dat) = self;
|
||||
dat.as_ptr()
|
||||
}
|
||||
|
||||
/// Converts the object to a mutable raw pointer
|
||||
#[inline]
|
||||
pub fn as_mut_ptr(&mut self) -> *mut $ty {
|
||||
let &mut $thing(ref mut dat) = self;
|
||||
dat.as_mut_ptr()
|
||||
}
|
||||
|
||||
/// Returns the length of the object as an array
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize { $len }
|
||||
|
||||
/// Returns whether the object, as an array, is empty. Always false.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool { false }
|
||||
|
||||
/// Returns the underlying bytes.
|
||||
#[inline]
|
||||
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
|
||||
|
||||
/// Returns the underlying bytes.
|
||||
#[inline]
|
||||
pub fn to_bytes(self) -> [$ty; $len] { self.0.clone() }
|
||||
|
||||
/// Returns the underlying bytes.
|
||||
#[inline]
|
||||
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
|
||||
}
|
||||
|
||||
impl<'a> core::convert::From<&'a [$ty]> for $thing {
|
||||
fn from(data: &'a [$ty]) -> $thing {
|
||||
assert_eq!(data.len(), $len);
|
||||
let mut ret = [0; $len];
|
||||
ret.copy_from_slice(&data[..]);
|
||||
$thing(ret)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> core::ops::Index<I> for $thing
|
||||
where
|
||||
[$ty]: core::ops::Index<I>,
|
||||
{
|
||||
type Output = <[$ty] as core::ops::Index<I>>::Output;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use impl_array_newtype;
|
||||
|
||||
macro_rules! debug_from_display {
|
||||
($thing:ident) => {
|
||||
impl core::fmt::Debug for $thing {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
core::fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use debug_from_display;
|
||||
// We use test_macros module to keep things organised, re-export everything for ease of use.
|
||||
#[cfg(test)]
|
||||
pub(crate) use test_macros::*;
|
||||
|
@ -182,227 +110,6 @@ mod test_macros {
|
|||
pub(crate) use hex_decode;
|
||||
}
|
||||
|
||||
macro_rules! serde_string_impl {
|
||||
($name:ident, $expecting:literal) => {
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
|
||||
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
use core::fmt::{self, Formatter};
|
||||
use core::str::FromStr;
|
||||
|
||||
struct Visitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str($expecting)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
$name::from_str(v).map_err(E::custom)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(Visitor)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
|
||||
impl $crate::serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: $crate::serde::Serializer,
|
||||
{
|
||||
serializer.collect_str(&self)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use serde_string_impl;
|
||||
|
||||
/// A combination macro where the human-readable serialization is done like
|
||||
/// serde_string_impl and the non-human-readable impl is done as a struct.
|
||||
macro_rules! serde_struct_human_string_impl {
|
||||
($name:ident, $expecting:literal, $($fe:ident),*) => (
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
|
||||
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
if deserializer.is_human_readable() {
|
||||
use core::fmt::{self, Formatter};
|
||||
use core::str::FromStr;
|
||||
|
||||
struct Visitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str($expecting)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
$name::from_str(v).map_err(E::custom)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(Visitor)
|
||||
} else {
|
||||
use core::fmt::{self, Formatter};
|
||||
use $crate::serde::de::IgnoredAny;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
enum Enum { Unknown__Field, $($fe),* }
|
||||
|
||||
struct EnumVisitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
|
||||
type Value = Enum;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str("a field name")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
match v {
|
||||
$(
|
||||
stringify!($fe) => Ok(Enum::$fe)
|
||||
),*,
|
||||
_ => Ok(Enum::Unknown__Field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> $crate::serde::Deserialize<'de> for Enum {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(EnumVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct Visitor;
|
||||
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str("a struct")
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
|
||||
where
|
||||
V: $crate::serde::de::SeqAccess<'de>,
|
||||
{
|
||||
use $crate::serde::de::Error;
|
||||
|
||||
let length = 0;
|
||||
$(
|
||||
let $fe = seq.next_element()?.ok_or_else(|| {
|
||||
Error::invalid_length(length, &self)
|
||||
})?;
|
||||
#[allow(unused_variables)]
|
||||
let length = length + 1;
|
||||
)*
|
||||
|
||||
let ret = $name {
|
||||
$($fe),*
|
||||
};
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: $crate::serde::de::MapAccess<'de>,
|
||||
{
|
||||
use $crate::serde::de::Error;
|
||||
|
||||
$(let mut $fe = None;)*
|
||||
|
||||
loop {
|
||||
match map.next_key::<Enum>()? {
|
||||
Some(Enum::Unknown__Field) => {
|
||||
map.next_value::<IgnoredAny>()?;
|
||||
}
|
||||
$(
|
||||
Some(Enum::$fe) => {
|
||||
$fe = Some(map.next_value()?);
|
||||
}
|
||||
)*
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
let $fe = match $fe {
|
||||
Some(x) => x,
|
||||
None => return Err(A::Error::missing_field(stringify!($fe))),
|
||||
};
|
||||
)*
|
||||
|
||||
let ret = $name {
|
||||
$($fe),*
|
||||
};
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
// end type defs
|
||||
|
||||
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
||||
|
||||
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
|
||||
impl $crate::serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: $crate::serde::Serializer,
|
||||
{
|
||||
if serializer.is_human_readable() {
|
||||
serializer.collect_str(&self)
|
||||
} else {
|
||||
use $crate::serde::ser::SerializeStruct;
|
||||
|
||||
// Only used to get the struct length.
|
||||
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
||||
|
||||
let mut st = serializer.serialize_struct(stringify!($name), FIELDS.len())?;
|
||||
|
||||
$(
|
||||
st.serialize_field(stringify!($fe), &self.$fe)?;
|
||||
)*
|
||||
|
||||
st.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
pub(crate) use serde_struct_human_string_impl;
|
||||
|
||||
/// Implements several traits for byte-based newtypes.
|
||||
/// Implements:
|
||||
/// - core::fmt::LowerHex (implies hashes::hex::ToHex)
|
||||
|
@ -537,11 +244,3 @@ macro_rules! impl_bytes_newtype {
|
|||
};
|
||||
}
|
||||
pub(crate) use impl_bytes_newtype;
|
||||
|
||||
/// Asserts a boolean expression at compile time.
|
||||
macro_rules! const_assert {
|
||||
($x:expr) => {{
|
||||
const _: [(); 0 - !$x as usize] = [];
|
||||
}};
|
||||
}
|
||||
pub(crate) use const_assert;
|
||||
|
|
|
@ -32,13 +32,13 @@ use core::str::FromStr;
|
|||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use bitcoin_internals::{debug_from_display, write_err};
|
||||
|
||||
use crate::io;
|
||||
use crate::prelude::{String, ToOwned};
|
||||
use crate::consensus::encode::{self, Encodable, Decodable};
|
||||
use crate::internal_macros::debug_from_display;
|
||||
use crate::hashes::hex::{FromHex, Error};
|
||||
use crate::error::impl_std_error;
|
||||
use bitcoin_internals::write_err;
|
||||
|
||||
/// Version of the protocol as appearing in network message headers
|
||||
/// This constant is used to signal to other peers which features you support.
|
||||
|
|
|
@ -293,3 +293,216 @@ pub mod hex_bytes {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! serde_string_impl {
|
||||
($name:ident, $expecting:literal) => {
|
||||
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
use core::fmt::{self, Formatter};
|
||||
use core::str::FromStr;
|
||||
|
||||
struct Visitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str($expecting)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
$name::from_str(v).map_err(E::custom)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(Visitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: $crate::serde::Serializer,
|
||||
{
|
||||
serializer.collect_str(&self)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
pub(crate) use serde_string_impl;
|
||||
|
||||
/// A combination macro where the human-readable serialization is done like
|
||||
/// serde_string_impl and the non-human-readable impl is done as a struct.
|
||||
macro_rules! serde_struct_human_string_impl {
|
||||
($name:ident, $expecting:literal, $($fe:ident),*) => (
|
||||
impl<'de> $crate::serde::Deserialize<'de> for $name {
|
||||
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
if deserializer.is_human_readable() {
|
||||
use core::fmt::{self, Formatter};
|
||||
use core::str::FromStr;
|
||||
|
||||
struct Visitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str($expecting)
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
$name::from_str(v).map_err(E::custom)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
deserializer.deserialize_str(Visitor)
|
||||
} else {
|
||||
use core::fmt::{self, Formatter};
|
||||
use $crate::serde::de::IgnoredAny;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
enum Enum { Unknown__Field, $($fe),* }
|
||||
|
||||
struct EnumVisitor;
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for EnumVisitor {
|
||||
type Value = Enum;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str("a field name")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: $crate::serde::de::Error,
|
||||
{
|
||||
match v {
|
||||
$(
|
||||
stringify!($fe) => Ok(Enum::$fe)
|
||||
),*,
|
||||
_ => Ok(Enum::Unknown__Field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> $crate::serde::Deserialize<'de> for Enum {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: $crate::serde::de::Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(EnumVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct Visitor;
|
||||
|
||||
impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
|
||||
type Value = $name;
|
||||
|
||||
fn expecting(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.write_str("a struct")
|
||||
}
|
||||
|
||||
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
|
||||
where
|
||||
V: $crate::serde::de::SeqAccess<'de>,
|
||||
{
|
||||
use $crate::serde::de::Error;
|
||||
|
||||
let length = 0;
|
||||
$(
|
||||
let $fe = seq.next_element()?.ok_or_else(|| {
|
||||
Error::invalid_length(length, &self)
|
||||
})?;
|
||||
#[allow(unused_variables)]
|
||||
let length = length + 1;
|
||||
)*
|
||||
|
||||
let ret = $name {
|
||||
$($fe),*
|
||||
};
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: $crate::serde::de::MapAccess<'de>,
|
||||
{
|
||||
use $crate::serde::de::Error;
|
||||
|
||||
$(let mut $fe = None;)*
|
||||
|
||||
loop {
|
||||
match map.next_key::<Enum>()? {
|
||||
Some(Enum::Unknown__Field) => {
|
||||
map.next_value::<IgnoredAny>()?;
|
||||
}
|
||||
$(
|
||||
Some(Enum::$fe) => {
|
||||
$fe = Some(map.next_value()?);
|
||||
}
|
||||
)*
|
||||
None => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
let $fe = match $fe {
|
||||
Some(x) => x,
|
||||
None => return Err(A::Error::missing_field(stringify!($fe))),
|
||||
};
|
||||
)*
|
||||
|
||||
let ret = $name {
|
||||
$($fe),*
|
||||
};
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
// end type defs
|
||||
|
||||
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
||||
|
||||
deserializer.deserialize_struct(stringify!($name), FIELDS, Visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: $crate::serde::Serializer,
|
||||
{
|
||||
if serializer.is_human_readable() {
|
||||
serializer.collect_str(&self)
|
||||
} else {
|
||||
use $crate::serde::ser::SerializeStruct;
|
||||
|
||||
// Only used to get the struct length.
|
||||
static FIELDS: &'static [&'static str] = &[$(stringify!($fe)),*];
|
||||
|
||||
let mut st = serializer.serialize_struct(stringify!($name), FIELDS.len())?;
|
||||
|
||||
$(
|
||||
st.serialize_field(stringify!($fe), &self.$fe)?;
|
||||
)*
|
||||
|
||||
st.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
pub(crate) use serde_struct_human_string_impl;
|
||||
|
|
|
@ -30,7 +30,7 @@ macro_rules! define_be_to_array {
|
|||
($name: ident, $type: ty, $byte_len: expr) => {
|
||||
#[inline]
|
||||
pub fn $name(val: $type) -> [u8; $byte_len] {
|
||||
$crate::internal_macros::const_assert!(::core::mem::size_of::<$type>() == $byte_len);
|
||||
bitcoin_internals::const_assert!(core::mem::size_of::<$type>() == $byte_len);
|
||||
let mut res = [0; $byte_len];
|
||||
for i in 0..$byte_len {
|
||||
res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8;
|
||||
|
@ -43,7 +43,7 @@ macro_rules! define_le_to_array {
|
|||
($name: ident, $type: ty, $byte_len: expr) => {
|
||||
#[inline]
|
||||
pub fn $name(val: $type) -> [u8; $byte_len] {
|
||||
$crate::internal_macros::const_assert!(::core::mem::size_of::<$type>() == $byte_len);
|
||||
bitcoin_internals::const_assert!(core::mem::size_of::<$type>() == $byte_len);
|
||||
let mut res = [0; $byte_len];
|
||||
for i in 0..$byte_len {
|
||||
res[i] = ((val >> i*8) & 0xff) as u8;
|
||||
|
|
|
@ -19,7 +19,6 @@ use crate::consensus::{encode, Encodable};
|
|||
use crate::error::impl_std_error;
|
||||
use crate::util::endian;
|
||||
use crate::hashes::{sha256, sha256d, Hash};
|
||||
use crate::internal_macros::serde_string_impl;
|
||||
use crate::prelude::*;
|
||||
use crate::util::taproot::{TapLeafHash, TAPROOT_ANNEX_PREFIX, TapSighashHash, LeafVersion};
|
||||
|
||||
|
@ -121,7 +120,8 @@ pub enum SchnorrSighashType {
|
|||
/// 0x83: Sign one output and only this input (see `Single` for what "one output" means).
|
||||
SinglePlusAnyoneCanPay = 0x83,
|
||||
}
|
||||
serde_string_impl!(SchnorrSighashType, "a SchnorrSighashType data");
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(SchnorrSighashType, "a SchnorrSighashType data");
|
||||
|
||||
impl fmt::Display for SchnorrSighashType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
@ -318,7 +318,8 @@ pub enum EcdsaSighashType {
|
|||
/// 0x83: Sign one output and only this input (see `Single` for what "one output" means).
|
||||
SinglePlusAnyoneCanPay = 0x83
|
||||
}
|
||||
serde_string_impl!(EcdsaSighashType, "a EcdsaSighashType data");
|
||||
#[cfg(feature = "serde")]
|
||||
crate::serde_utils::serde_string_impl!(EcdsaSighashType, "a EcdsaSighashType data");
|
||||
|
||||
impl fmt::Display for EcdsaSighashType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
|
|
@ -28,6 +28,7 @@ extern crate std;
|
|||
|
||||
pub mod error;
|
||||
pub mod hex;
|
||||
pub mod macros;
|
||||
|
||||
/// Mainly reexports based on features.
|
||||
pub(crate) mod prelude {
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
//! Various macros used by the Rust Bitcoin ecosystem.
|
||||
//!
|
||||
|
||||
/// Implements standard array methods for a given wrapper type.
|
||||
#[macro_export]
|
||||
macro_rules! impl_array_newtype {
|
||||
($thing:ident, $ty:ty, $len:literal) => {
|
||||
impl $thing {
|
||||
/// Converts the object to a raw pointer.
|
||||
#[inline]
|
||||
pub fn as_ptr(&self) -> *const $ty {
|
||||
let &$thing(ref dat) = self;
|
||||
dat.as_ptr()
|
||||
}
|
||||
|
||||
/// Converts the object to a mutable raw pointer.
|
||||
#[inline]
|
||||
pub fn as_mut_ptr(&mut self) -> *mut $ty {
|
||||
let &mut $thing(ref mut dat) = self;
|
||||
dat.as_mut_ptr()
|
||||
}
|
||||
|
||||
/// Returns the length of the object as an array.
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize { $len }
|
||||
|
||||
/// Returns whether the object, as an array, is empty. Always false.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool { false }
|
||||
|
||||
/// Returns a reference the underlying bytes.
|
||||
#[inline]
|
||||
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
|
||||
|
||||
/// Returns a clone of the underlying bytes.
|
||||
#[inline]
|
||||
pub fn to_bytes(self) -> [$ty; $len] { self.0.clone() }
|
||||
|
||||
/// Returns the underlying bytes (takes ownership).
|
||||
#[inline]
|
||||
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
|
||||
}
|
||||
|
||||
impl<'a> core::convert::From<&'a [$ty]> for $thing {
|
||||
fn from(data: &'a [$ty]) -> $thing {
|
||||
assert_eq!(data.len(), $len);
|
||||
let mut ret = [0; $len];
|
||||
ret.copy_from_slice(&data[..]);
|
||||
$thing(ret)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> core::ops::Index<I> for $thing
|
||||
where
|
||||
[$ty]: core::ops::Index<I>,
|
||||
{
|
||||
type Output = <[$ty] as core::ops::Index<I>>::Output;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Implements `Debug` by calling through to `Display`.
|
||||
#[macro_export]
|
||||
macro_rules! debug_from_display {
|
||||
($thing:ident) => {
|
||||
impl core::fmt::Debug for $thing {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
||||
core::fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Asserts a boolean expression at compile time.
|
||||
#[macro_export]
|
||||
macro_rules! const_assert {
|
||||
($x:expr) => {{
|
||||
const _: [(); 0 - !$x as usize] = [];
|
||||
}};
|
||||
}
|
Loading…
Reference in New Issue