Stop using macro for Midstate

In preparation for changing the `sha256::Midstate` internals stop using
the `arr_newtype_fmt_impl` macro and implement the `fmt` traits
manually.

In doing so, remove the `DISPLAY_BACKWARDS` const but keep the current
behaviour of displaying the midstate backwards.
This commit is contained in:
Tobin C. Harding 2024-07-11 13:55:00 +10:00
parent 37b54dd54c
commit 07e8e5d3a6
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 21 additions and 7 deletions

View File

@ -6,9 +6,9 @@
use core::arch::x86::*; use core::arch::x86::*;
#[cfg(all(feature = "std", target_arch = "x86_64"))] #[cfg(all(feature = "std", target_arch = "x86_64"))]
use core::arch::x86_64::*; use core::arch::x86_64::*;
use core::cmp;
use core::ops::Index; use core::ops::Index;
use core::slice::SliceIndex; use core::slice::SliceIndex;
use core::{cmp, fmt};
#[cfg(doc)] #[cfg(doc)]
use crate::sha256t; use crate::sha256t;
@ -143,11 +143,6 @@ impl Midstate {
/// Length of the midstate, in bytes. /// Length of the midstate, in bytes.
const LEN: usize = 32; const LEN: usize = 32;
/// Flag indicating whether user-visible serializations of this hash
/// should be backward. For some reason Satoshi decided this should be
/// true for `Sha256dHash`, so here we are.
const DISPLAY_BACKWARD: bool = true;
/// Construct a new [`Midstate`] from the inner value. /// Construct a new [`Midstate`] from the inner value.
pub const fn from_byte_array(inner: [u8; 32]) -> Self { Midstate(inner) } pub const fn from_byte_array(inner: [u8; 32]) -> Self { Midstate(inner) }
@ -183,7 +178,26 @@ impl Midstate {
} }
} }
crate::internal_macros::arr_newtype_fmt_impl!(Midstate, 32); impl fmt::LowerHex for Midstate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
hex::fmt_hex_exact!(f, 32, self.0.iter().rev(), hex::Case::Lower)
}
}
impl fmt::UpperHex for Midstate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
hex::fmt_hex_exact!(f, 32, self.0.iter().rev(), hex::Case::Upper)
}
}
impl fmt::Display for Midstate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self, f) }
}
impl fmt::Debug for Midstate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:#}", self) }
}
serde_impl!(Midstate, 32); serde_impl!(Midstate, 32);
borrow_slice_impl!(Midstate); borrow_slice_impl!(Midstate);