From f0558e8eb9743381a2ef3b83de1fb894dfdc8ff8 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 28 Feb 2024 09:47:21 +1100 Subject: [PATCH] Use fmt_hex_exact Currently we have two functions for displaying forwards and backwards and we also have `fmt_hex_exact`. I do not know why we added the functions. In the latest version of hex we do not have the ability to construct a `DisplayArray` type so we have to use `fmt_hex_exact`. Make the change now, separate from the `hex` upgrade, to assist review. --- hashes/src/internal_macros.rs | 16 ---------------- hashes/src/util.rs | 16 ++++++++-------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 9352cde5..5534f697 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -71,22 +71,6 @@ pub(crate) use arr_newtype_fmt_impl; macro_rules! hash_trait_impls { ($bits:expr, $reverse:expr $(, $gen:ident: $gent:ident)*) => { impl<$($gen: $gent),*> Hash<$($gen),*> { - /// Displays hex forwards, regardless of how this type would display it naturally. - /// - /// This is mainly intended as an internal method and you shouldn't need it unless - /// you're doing something special. - pub fn forward_hex(&self) -> impl '_ + core::fmt::LowerHex + core::fmt::UpperHex { - $crate::hex::DisplayHex::as_hex(&self.0) - } - - /// Displays hex backwards, regardless of how this type would display it naturally. - /// - /// This is mainly intended as an internal method and you shouldn't need it unless - /// you're doing something special. - pub fn backward_hex(&self) -> impl '_ + core::fmt::LowerHex + core::fmt::UpperHex { - $crate::hex::display::DisplayArray::<_, [u8; $bits / 8 * 2]>::new(self.0.iter().rev()) - } - /// Zero cost conversion between a fixed length byte array shared reference and /// a shared reference to this Hash type. pub fn from_bytes_ref(bytes: &[u8; $bits / 8]) -> &Self { diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 92fd4855..fcb1b173 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -3,17 +3,17 @@ #[macro_export] /// Adds hexadecimal formatting implementation of a trait `$imp` to a given type `$ty`. macro_rules! hex_fmt_impl( - ($reverse:expr, $ty:ident) => ( - $crate::hex_fmt_impl!($reverse, $ty, ); + ($reverse:expr, $len:expr, $ty:ident) => ( + $crate::hex_fmt_impl!($reverse, $len, $ty, ); ); - ($reverse:expr, $ty:ident, $($gen:ident: $gent:ident),*) => ( + ($reverse:expr, $len:expr, $ty:ident, $($gen:ident: $gent:ident),*) => ( impl<$($gen: $gent),*> $crate::_export::_core::fmt::LowerHex 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::LowerHex::fmt(&self.0.backward_hex(), f) + $crate::hex::fmt_hex_exact!(f, $len, ::as_byte_array(&self).iter().rev(), $crate::hex::Case::Lower) } else { - $crate::_export::_core::fmt::LowerHex::fmt(&self.0.forward_hex(), f) + $crate::hex::fmt_hex_exact!(f, $len, ::as_byte_array(&self), $crate::hex::Case::Lower) } } } @@ -22,9 +22,9 @@ macro_rules! hex_fmt_impl( #[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) + $crate::hex::fmt_hex_exact!(f, $len, ::as_byte_array(&self).iter().rev(), $crate::hex::Case::Upper) } else { - $crate::_export::_core::fmt::UpperHex::fmt(&self.0.forward_hex(), f) + $crate::hex::fmt_hex_exact!(f, $len, ::as_byte_array(&self), $crate::hex::Case::Upper) } } } @@ -188,7 +188,7 @@ macro_rules! hash_newtype { $({ $($type_attrs)* })* } - $crate::hex_fmt_impl!(<$newtype as $crate::Hash>::DISPLAY_BACKWARD, $newtype); + $crate::hex_fmt_impl!(<$newtype as $crate::Hash>::DISPLAY_BACKWARD, <$newtype as $crate::Hash>::LEN, $newtype); $crate::serde_impl!($newtype, <$newtype as $crate::Hash>::LEN); $crate::borrow_slice_impl!($newtype);