Move impl_array_newtype to bitcoin_internals

`impl_array_newtype` is an internal macro, move it to a new, ever so
meaningfully named, `macros` module.

Use `#[macro_export]`, no other changes to the macro.
This commit is contained in:
Tobin C. Harding 2022-09-20 14:39:20 +10:00
parent 938b61bf66
commit 771cdde282
6 changed files with 72 additions and 65 deletions

View File

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

View File

@ -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, serde_string_impl};
use crate::io::Write;
use crate::network::constants::Network;
use crate::prelude::*;

View File

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

View File

@ -46,67 +46,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 {

View File

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

63
internals/src/macros.rs Normal file
View File

@ -0,0 +1,63 @@
//! 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 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] }
}
};
}