2023-04-30 23:26:36 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
/// Adds hexadecimal formatting implementation of a trait `$imp` to a given type `$ty`.
|
|
|
|
macro_rules! hex_fmt_impl(
|
2022-12-06 23:50:50 +00:00
|
|
|
($reverse:expr, $ty:ident) => (
|
|
|
|
$crate::hex_fmt_impl!($reverse, $ty, );
|
2022-09-16 01:52:57 +00:00
|
|
|
);
|
2022-12-06 23:50:50 +00:00
|
|
|
($reverse:expr, $ty:ident, $($gen:ident: $gent:ident),*) => (
|
2022-09-16 01:52:57 +00:00
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($gen),*> {
|
2022-12-06 23:50:50 +00:00
|
|
|
#[inline]
|
2022-09-16 01:52:57 +00:00
|
|
|
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
|
2022-12-06 23:50:50 +00:00
|
|
|
if $reverse {
|
|
|
|
$crate::_export::_core::fmt::LowerHex::fmt(&self.0.backward_hex(), f)
|
|
|
|
} else {
|
|
|
|
$crate::_export::_core::fmt::LowerHex::fmt(&self.0.forward_hex(), f)
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
2022-12-06 23:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::fmt::UpperHex for $ty<$($gen),*> {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
|
|
|
|
if $reverse {
|
|
|
|
$crate::_export::_core::fmt::UpperHex::fmt(&self.0.backward_hex(), f)
|
2022-09-16 01:52:57 +00:00
|
|
|
} else {
|
2022-12-06 23:50:50 +00:00
|
|
|
$crate::_export::_core::fmt::UpperHex::fmt(&self.0.forward_hex(), f)
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Display for $ty<$($gen),*> {
|
2022-12-06 23:50:50 +00:00
|
|
|
#[inline]
|
2022-09-16 01:52:57 +00:00
|
|
|
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
|
2022-12-06 23:50:50 +00:00
|
|
|
$crate::_export::_core::fmt::LowerHex::fmt(&self, f)
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
|
2022-12-06 23:50:50 +00:00
|
|
|
#[inline]
|
2022-09-16 01:52:57 +00:00
|
|
|
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
|
|
|
|
write!(f, "{:#}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Adds slicing traits implementations to a given type `$ty`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! borrow_slice_impl(
|
|
|
|
($ty:ident) => (
|
|
|
|
$crate::borrow_slice_impl!($ty, );
|
|
|
|
);
|
|
|
|
($ty:ident, $($gen:ident: $gent:ident),*) => (
|
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
|
|
|
|
fn borrow(&self) -> &[u8] {
|
|
|
|
&self[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<$($gen: $gent),*> $crate::_export::_core::convert::AsRef<[u8]> for $ty<$($gen),*> {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! engine_input_impl(
|
|
|
|
() => (
|
2023-04-28 18:44:54 +00:00
|
|
|
#[cfg(not(hashes_fuzz))]
|
2022-09-16 01:52:57 +00:00
|
|
|
fn input(&mut self, mut inp: &[u8]) {
|
|
|
|
while !inp.is_empty() {
|
|
|
|
let buf_idx = self.length % <Self as crate::HashEngine>::BLOCK_SIZE;
|
|
|
|
let rem_len = <Self as crate::HashEngine>::BLOCK_SIZE - buf_idx;
|
|
|
|
let write_len = cmp::min(rem_len, inp.len());
|
|
|
|
|
|
|
|
self.buffer[buf_idx..buf_idx + write_len]
|
|
|
|
.copy_from_slice(&inp[..write_len]);
|
|
|
|
self.length += write_len;
|
|
|
|
if self.length % <Self as crate::HashEngine>::BLOCK_SIZE == 0 {
|
|
|
|
self.process_block();
|
|
|
|
}
|
|
|
|
inp = &inp[write_len..];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 18:44:54 +00:00
|
|
|
#[cfg(hashes_fuzz)]
|
2022-09-16 01:52:57 +00:00
|
|
|
fn input(&mut self, inp: &[u8]) {
|
|
|
|
for c in inp {
|
|
|
|
self.buffer[0] ^= *c;
|
|
|
|
}
|
|
|
|
self.length += inp.len();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Creates a new newtype around a [`Hash`] type.
|
2023-02-22 08:20:28 +00:00
|
|
|
///
|
|
|
|
/// The syntax is similar to the usual tuple struct syntax:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use bitcoin_hashes::{hash_newtype, sha256};
|
|
|
|
/// hash_newtype! {
|
|
|
|
/// /// Hash of `Foo`.
|
|
|
|
/// pub struct MyNewtype(pub sha256::Hash);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2023-02-24 20:39:29 +00:00
|
|
|
/// You can use any valid visibility specifier in place of `pub` or you can omit either or both, if
|
|
|
|
/// you want the type or its field to be private.
|
2023-02-22 08:20:28 +00:00
|
|
|
///
|
2023-02-24 20:39:29 +00:00
|
|
|
/// Whether the hash is reversed or not when displaying depends on the inner type. However you can
|
|
|
|
/// override it like this:
|
2023-02-22 08:20:28 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use bitcoin_hashes::{hash_newtype, sha256};
|
|
|
|
/// hash_newtype! {
|
|
|
|
/// #[hash_newtype(backward)]
|
|
|
|
/// struct MyNewtype(sha256::Hash);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This will display the hash backwards regardless of what the inner type does. Use `forward`
|
|
|
|
/// instead of `backward` to force displaying forward.
|
|
|
|
///
|
|
|
|
/// You can add arbitrary doc comments or other attributes to the struct or it's field. Note that
|
|
|
|
/// the macro already derives [`Copy`], [`Clone`], [`Eq`], [`PartialEq`],
|
|
|
|
/// [`Hash`](core::hash::Hash), [`Ord`], [`PartialOrd`]. With the `serde` feature on, this also adds
|
2023-05-26 06:50:46 +00:00
|
|
|
/// `Serialize` and `Deserialize` implementations.
|
2023-02-22 08:20:28 +00:00
|
|
|
///
|
|
|
|
/// You can also define multiple newtypes within one macro call:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use bitcoin_hashes::{hash_newtype, sha256, hash160};
|
|
|
|
///
|
|
|
|
/// hash_newtype! {
|
|
|
|
/// /// My custom type 1
|
|
|
|
/// pub struct Newtype1(sha256::Hash);
|
|
|
|
///
|
|
|
|
/// /// My custom type 2
|
|
|
|
/// struct Newtype2(hash160::Hash);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-02-24 20:39:29 +00:00
|
|
|
///
|
|
|
|
/// Note: the macro is internally recursive. If you use too many attributes (> 256 tokens) you may
|
|
|
|
/// hit recursion limit. If you have so many attributes for a good reason, just raising the limit
|
|
|
|
/// should be OK. Note however that attribute-processing part has to use [TT muncher] which has
|
|
|
|
/// quadratic complexity, so having many attributes may blow up compile time. This should be rare.
|
|
|
|
///
|
|
|
|
/// [TT muncher]: https://danielkeep.github.io/tlborm/book/pat-incremental-tt-munchers.html
|
|
|
|
///
|
|
|
|
// Ever heard of legendary comments warning developers to not touch the code? Yep, here's another
|
|
|
|
// one. The following code is written the way it is for some specific reasons. If you think you can
|
|
|
|
// simplify it, I suggest spending your time elsewhere.
|
|
|
|
//
|
|
|
|
// If you looks at the code carefully you might ask these questions:
|
|
|
|
//
|
|
|
|
// * Why are attributes using `tt` and not `meta`?!
|
|
|
|
// * Why are the macros split like that?!
|
|
|
|
// * Why use recursion instead of `$()*`?
|
|
|
|
//
|
|
|
|
// None of these are here by accident. For some reason unknown to me, if you accept an argument to
|
|
|
|
// macro with any fragment specifier other than `tt` it will **not** match any of the rules
|
|
|
|
// requiring a specific token. Yep, I tried it, I literally got error that `hash_newtype` doesn't
|
|
|
|
// match `hash_newtype`. So all input attributes must be `tt`.
|
|
|
|
//
|
|
|
|
// Originally I wanted to define a bunch of macros that would filter-out hash_type attributes. Then
|
|
|
|
// I remembered (by seeing compiler error) that calling macros is not allowed inside attributes.
|
|
|
|
// And no, you can't bypass it by calling a helper macro and passing "output of another macro" into
|
|
|
|
// it. The whole macro gets passed, not the resulting value. So we have to generate the entire
|
|
|
|
// attributes. And you can't just place an attribute-producing macro above struct - they are
|
|
|
|
// considered separate items. This is not C.
|
|
|
|
//
|
|
|
|
// Thus struct is generated in a separate macro together with attributes. And since the macro needs
|
|
|
|
// attributes as the input and I didn't want to create confusion by using `#[]` syntax *after*
|
|
|
|
// struct, I opted to use `{}` as a separator. Yes, a separator is required because an attribute
|
|
|
|
// may be composed of multiple token trees - that's the point of "double repetition".
|
2022-09-16 01:52:57 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! hash_newtype {
|
2023-02-22 08:20:28 +00:00
|
|
|
($($(#[$($type_attrs:tt)*])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:tt])* $field_vis:vis $hash:path);)+) => {
|
|
|
|
$(
|
|
|
|
$($crate::hash_newtype_known_attrs!(#[ $($type_attrs)* ]);)*
|
|
|
|
|
|
|
|
$crate::hash_newtype_struct! {
|
|
|
|
$type_vis struct $newtype($(#[$field_attrs])* $field_vis $hash);
|
|
|
|
|
|
|
|
$({ $($type_attrs)* })*
|
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
|
2023-02-22 08:20:28 +00:00
|
|
|
$crate::hex_fmt_impl!(<$newtype as $crate::Hash>::DISPLAY_BACKWARD, $newtype);
|
2023-02-22 00:09:01 +00:00
|
|
|
$crate::serde_impl!($newtype, <$newtype as $crate::Hash>::LEN);
|
2022-09-16 01:52:57 +00:00
|
|
|
$crate::borrow_slice_impl!($newtype);
|
|
|
|
|
|
|
|
impl $newtype {
|
2023-01-28 22:47:24 +00:00
|
|
|
/// Creates this wrapper type from the inner hash type.
|
2023-02-22 08:20:28 +00:00
|
|
|
#[allow(unused)] // the user of macro may not need this
|
2023-01-28 22:47:24 +00:00
|
|
|
pub fn from_raw_hash(inner: $hash) -> $newtype {
|
2022-09-16 01:52:57 +00:00
|
|
|
$newtype(inner)
|
|
|
|
}
|
|
|
|
|
2023-01-28 22:47:24 +00:00
|
|
|
/// Returns the inner hash (sha256, sh256d etc.).
|
2023-02-22 08:20:28 +00:00
|
|
|
#[allow(unused)] // the user of macro may not need this
|
2023-01-28 22:47:24 +00:00
|
|
|
pub fn to_raw_hash(self) -> $hash {
|
2022-09-16 01:52:57 +00:00
|
|
|
self.0
|
|
|
|
}
|
2023-01-28 22:47:24 +00:00
|
|
|
|
|
|
|
/// Returns a reference to the inner hash (sha256, sh256d etc.).
|
|
|
|
#[allow(unused)] // the user of macro may not need this
|
|
|
|
pub fn as_raw_hash(&self) -> &$hash {
|
|
|
|
&self.0
|
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::_export::_core::convert::From<$hash> for $newtype {
|
|
|
|
fn from(inner: $hash) -> $newtype {
|
|
|
|
// Due to rust 1.22 we have to use this instead of simple `Self(inner)`
|
|
|
|
Self { 0: inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::_export::_core::convert::From<$newtype> for $hash {
|
|
|
|
fn from(hashtype: $newtype) -> $hash {
|
|
|
|
hashtype.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::Hash for $newtype {
|
|
|
|
type Engine = <$hash as $crate::Hash>::Engine;
|
2023-01-28 22:47:24 +00:00
|
|
|
type Bytes = <$hash as $crate::Hash>::Bytes;
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
const LEN: usize = <$hash as $crate::Hash>::LEN;
|
2023-02-22 08:20:28 +00:00
|
|
|
const DISPLAY_BACKWARD: bool = $crate::hash_newtype_get_direction!($hash, $(#[$($type_attrs)*])*);
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
fn engine() -> Self::Engine {
|
|
|
|
<$hash as $crate::Hash>::engine()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_engine(e: Self::Engine) -> Self {
|
|
|
|
Self::from(<$hash as $crate::Hash>::from_engine(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-05-24 03:29:30 +00:00
|
|
|
fn from_slice(sl: &[u8]) -> Result<$newtype, $crate::FromSliceError> {
|
2022-09-16 01:52:57 +00:00
|
|
|
Ok($newtype(<$hash as $crate::Hash>::from_slice(sl)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-01-28 22:47:24 +00:00
|
|
|
fn from_byte_array(bytes: Self::Bytes) -> Self {
|
|
|
|
$newtype(<$hash as $crate::Hash>::from_byte_array(bytes))
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-01-28 22:47:24 +00:00
|
|
|
fn to_byte_array(self) -> Self::Bytes {
|
|
|
|
self.0.to_byte_array()
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-01-28 22:47:24 +00:00
|
|
|
fn as_byte_array(&self) -> &Self::Bytes {
|
|
|
|
self.0.as_byte_array()
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn all_zeros() -> Self {
|
|
|
|
let zeros = <$hash>::all_zeros();
|
|
|
|
$newtype(zeros)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $crate::_export::_core::str::FromStr for $newtype {
|
2023-07-21 00:38:34 +00:00
|
|
|
type Err = $crate::hex::HexToArrayError;
|
2022-09-16 01:52:57 +00:00
|
|
|
fn from_str(s: &str) -> $crate::_export::_core::result::Result<$newtype, Self::Err> {
|
2023-07-21 00:38:34 +00:00
|
|
|
use $crate::hex::{FromHex, HexToBytesIter};
|
2023-01-20 00:24:48 +00:00
|
|
|
use $crate::Hash;
|
|
|
|
|
2023-01-28 22:47:24 +00:00
|
|
|
let inner: <$hash as Hash>::Bytes = if <Self as $crate::Hash>::DISPLAY_BACKWARD {
|
2023-07-21 00:38:34 +00:00
|
|
|
FromHex::from_byte_iter(HexToBytesIter::new(s)?.rev())?
|
2023-01-20 00:24:48 +00:00
|
|
|
} else {
|
2023-07-21 00:38:34 +00:00
|
|
|
FromHex::from_byte_iter(HexToBytesIter::new(s)?)?
|
2023-01-20 00:24:48 +00:00
|
|
|
};
|
2023-01-28 22:47:24 +00:00
|
|
|
Ok($newtype(<$hash>::from_byte_array(inner)))
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 00:09:01 +00:00
|
|
|
impl $crate::_export::_core::convert::AsRef<[u8; <$hash as $crate::Hash>::LEN]> for $newtype {
|
|
|
|
fn as_ref(&self) -> &[u8; <$hash as $crate::Hash>::LEN] {
|
|
|
|
AsRef::<[u8; <$hash as $crate::Hash>::LEN]>::as_ref(&self.0)
|
2023-01-25 23:50:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:52:57 +00:00
|
|
|
impl<I: $crate::_export::_core::slice::SliceIndex<[u8]>> $crate::_export::_core::ops::Index<I> for $newtype {
|
|
|
|
type Output = I::Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: I) -> &Self::Output {
|
|
|
|
&self.0[index]
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 08:20:28 +00:00
|
|
|
)+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:39:29 +00:00
|
|
|
// Generates the struct only (no impls)
|
|
|
|
//
|
|
|
|
// This is a separate macro to make it more readable and have a separate interface that allows for
|
|
|
|
// two groups of type attributes: processed and not-yet-processed ones (think about it like
|
|
|
|
// computation via recursion). The macro recursively matches unprocessed attributes, popping them
|
|
|
|
// one at a time and either ignoring them (`hash_newtype`) or appending them to the list of
|
|
|
|
// processed attributes to be added to the struct.
|
|
|
|
//
|
|
|
|
// Once the list of not-yet-processed attributes is empty the struct is generated with processed
|
|
|
|
// attributes added.
|
2023-02-22 08:20:28 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! hash_newtype_struct {
|
|
|
|
($(#[$other_attrs:meta])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:meta])* $field_vis:vis $hash:path);) => {
|
|
|
|
$(#[$other_attrs])*
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
$type_vis struct $newtype($(#[$field_attrs])* $field_vis $hash);
|
|
|
|
};
|
|
|
|
($(#[$other_attrs:meta])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:meta])* $field_vis:vis $hash:path); { hash_newtype($($ignore:tt)*) } $($type_attrs:tt)*) => {
|
|
|
|
$crate::hash_newtype_struct! {
|
|
|
|
$(#[$other_attrs])*
|
|
|
|
$type_vis struct $newtype($(#[$field_attrs])* $field_vis $hash);
|
|
|
|
|
|
|
|
$($type_attrs)*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($(#[$other_attrs:meta])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:meta])* $field_vis:vis $hash:path); { $other_attr:meta } $($type_attrs:tt)*) => {
|
|
|
|
$crate::hash_newtype_struct! {
|
|
|
|
$(#[$other_attrs])*
|
|
|
|
#[$other_attr]
|
|
|
|
$type_vis struct $newtype($(#[$field_attrs])* $field_vis $hash);
|
|
|
|
|
|
|
|
$($type_attrs)*
|
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:39:29 +00:00
|
|
|
// Extracts `hash_newtype(forward)` and `hash_newtype(backward)` attributes if any and turns them
|
|
|
|
// into bool, defaulting to `DISPLAY_BACKWARD` of the wrapped type if the attribute is omitted.
|
|
|
|
//
|
|
|
|
// Once an appropriate attribute is found we pass the remaining ones into another macro to detect
|
|
|
|
// duplicates/conflicts and report an error.
|
|
|
|
//
|
|
|
|
// FYI, no, we can't use a helper macro to first filter all `hash_newtype` attributes. We would be
|
|
|
|
// attempting to match on macros instead. So we must write `hashe_newtype` in each branch.
|
2023-02-22 08:20:28 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! hash_newtype_get_direction {
|
|
|
|
($hash:ty, ) => { <$hash as $crate::Hash>::DISPLAY_BACKWARD };
|
|
|
|
($hash:ty, #[hash_newtype(forward)] $($others:tt)*) => { { $crate::hash_newtype_forbid_direction!(forward, $($others)*); false } };
|
|
|
|
($hash:ty, #[hash_newtype(backward)] $($others:tt)*) => { { $crate::hash_newtype_forbid_direction!(backward, $($others)*); true } };
|
|
|
|
($hash:ty, #[$($ignore:tt)*] $($others:tt)*) => { $crate::hash_newtype_get_direction!($hash, $($others)*) };
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:39:29 +00:00
|
|
|
// Reports an error if any of the attributes is `hash_newtype($direction)`.
|
|
|
|
//
|
|
|
|
// This is used for detection of duplicates/conflicts, see the macro above.
|
2023-02-22 08:20:28 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! hash_newtype_forbid_direction {
|
|
|
|
($direction:ident, ) => {};
|
|
|
|
($direction:ident, #[hash_newtype(forward)] $(others:tt)*) => {
|
|
|
|
compile_error!(concat!("Cannot set display direction to forward: ", stringify!($direction), " was already specified"));
|
|
|
|
};
|
|
|
|
($direction:ident, #[hash_newtype(backward)] $(others:tt)*) => {
|
|
|
|
compile_error!(concat!("Cannot set display direction to backward: ", stringify!($direction), " was already specified"));
|
|
|
|
};
|
|
|
|
($direction:ident, #[$($ignore:tt)*] $(#[$others:tt])*) => {
|
|
|
|
$crate::hash_newtype_forbid_direction!($direction, $(#[$others])*)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 20:39:29 +00:00
|
|
|
// Checks (at compile time) that all `hash_newtype` attributes are known.
|
|
|
|
//
|
|
|
|
// An unknown attribute could be a typo that could cause problems - e.g. wrong display direction if
|
|
|
|
// it's missing. To prevent this, we call this macro above. The macro produces nothing unless an
|
|
|
|
// unknown attribute is found in which case it produces `compile_error!`.
|
2023-02-22 08:20:28 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! hash_newtype_known_attrs {
|
|
|
|
(#[hash_newtype(forward)]) => {};
|
|
|
|
(#[hash_newtype(backward)]) => {};
|
|
|
|
(#[hash_newtype($($unknown:tt)*)]) => { compile_error!(concat!("Unrecognized attribute ", stringify!($($unknown)*))); };
|
|
|
|
($($ignore:tt)*) => {};
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:52:57 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-02-21 23:25:12 +00:00
|
|
|
use crate::{sha256, Hash};
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-01-25 23:50:20 +00:00
|
|
|
fn hash_as_ref_array() {
|
|
|
|
let hash = sha256::Hash::hash(&[3, 50]);
|
|
|
|
let r = AsRef::<[u8; 32]>::as_ref(&hash);
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(r, hash.as_byte_array());
|
2023-01-25 23:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_as_ref_slice() {
|
2022-12-07 01:40:57 +00:00
|
|
|
let hash = sha256::Hash::hash(&[3, 50]);
|
2023-01-25 23:50:20 +00:00
|
|
|
let r = AsRef::<[u8]>::as_ref(&hash);
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(r, hash.as_byte_array());
|
2022-12-07 01:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_borrow() {
|
|
|
|
use core::borrow::Borrow;
|
|
|
|
|
2022-09-16 01:52:57 +00:00
|
|
|
let hash = sha256::Hash::hash(&[3, 50]);
|
2022-12-07 01:40:57 +00:00
|
|
|
let borrowed: &[u8] = hash.borrow();
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(borrowed, hash.as_byte_array());
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 08:20:28 +00:00
|
|
|
hash_newtype! {
|
|
|
|
/// Test hash.
|
|
|
|
struct TestHash(crate::sha256d::Hash);
|
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display() {
|
|
|
|
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
let got = format!("{}", TestHash::all_zeros());
|
|
|
|
assert_eq!(got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn display_alternate() {
|
|
|
|
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
let got = format!("{:#}", TestHash::all_zeros());
|
|
|
|
assert_eq!(got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lower_hex() {
|
|
|
|
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
let got = format!("{:x}", TestHash::all_zeros());
|
|
|
|
assert_eq!(got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lower_hex_alternate() {
|
|
|
|
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
let got = format!("{:#x}", TestHash::all_zeros());
|
|
|
|
assert_eq!(got, want)
|
|
|
|
}
|
2023-01-25 23:50:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_hash_as_ref_array() {
|
|
|
|
let hash = TestHash::all_zeros();
|
|
|
|
let r = AsRef::<[u8; 32]>::as_ref(&hash);
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(r, hash.as_byte_array());
|
2023-01-25 23:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inner_hash_as_ref_slice() {
|
|
|
|
let hash = TestHash::all_zeros();
|
|
|
|
let r = AsRef::<[u8]>::as_ref(&hash);
|
2023-01-28 22:47:24 +00:00
|
|
|
assert_eq!(r, hash.as_byte_array());
|
2023-01-25 23:50:20 +00:00
|
|
|
}
|
2022-09-16 01:52:57 +00:00
|
|
|
}
|