Make from_hex inherent for byte-like types

Byte like types naturally display in hex, therefore they should have
an inherent method `from_hex` and not implement `FromHex`.
This commit is contained in:
Tobin C. Harding 2024-02-21 14:43:04 +11:00
parent 975ada3570
commit 1ee887a2fc
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 9 additions and 18 deletions

View File

@ -50,7 +50,6 @@ pub(crate) use impl_consensus_encoding;
/// - core::fmt::UpperHex
/// - core::fmt::Display
/// - core::str::FromStr
/// - hex::FromHex
macro_rules! impl_bytes_newtype {
($t:ident, $len:literal) => {
impl $t {
@ -68,6 +67,11 @@ macro_rules! impl_bytes_newtype {
self.0
}
/// Creates `Self` from a hex string.
pub fn from_hex(s: &str) -> Result<Self, hex::HexToArrayError> {
Ok($t($crate::hex::FromHex::from_hex(s)?))
}
}
impl core::fmt::LowerHex for $t {
@ -96,22 +100,9 @@ macro_rules! impl_bytes_newtype {
}
}
impl $crate::hex::FromHex for $t {
type Err = $crate::hex::HexToArrayError;
fn from_byte_iter<I>(iter: I) -> core::result::Result<Self, $crate::hex::HexToArrayError>
where
I: core::iter::Iterator<Item = core::result::Result<u8, $crate::hex::HexToBytesError>>
+ core::iter::ExactSizeIterator
+ core::iter::DoubleEndedIterator,
{
Ok($t($crate::hex::FromHex::from_byte_iter(iter)?))
}
}
impl core::str::FromStr for $t {
type Err = $crate::hex::HexToArrayError;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> { $crate::hex::FromHex::from_hex(s) }
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> { Self::from_hex(s) }
}
#[cfg(feature = "serde")]
@ -145,17 +136,17 @@ macro_rules! impl_bytes_newtype {
use $crate::serde::de::Unexpected;
if let Ok(hex) = core::str::from_utf8(v) {
$crate::hex::FromHex::from_hex(hex).map_err(E::custom)
core::str::FromStr::from_str(hex).map_err(E::custom)
} else {
return Err(E::invalid_value(Unexpected::Bytes(v), &self));
}
}
fn visit_str<E>(self, v: &str) -> core::result::Result<Self::Value, E>
fn visit_str<E>(self, hex: &str) -> core::result::Result<Self::Value, E>
where
E: $crate::serde::de::Error,
{
$crate::hex::FromHex::from_hex(v).map_err(E::custom)
core::str::FromStr::from_str(hex).map_err(E::custom)
}
}