Merge rust-bitcoin/rust-bitcoin#2491: Make from_hex inherent for byte-like types

1ee887a2fc Make from_hex inherent for byte-like types (Tobin C. Harding)

Pull request description:

  Byte like types naturally display in hex, therefore they should havean inherent method `from_hex` and not implement `FromHex`.

ACKs for top commit:
  Kixunil:
    ACK 1ee887a2fc
  apoelstra:
    ACK 1ee887a2fc

Tree-SHA512: fc69168f6111e402ffca6e861a9fdd50742d8faf361929c620d94c8f5598fb7d6920cab4529d69a7cce2a5ff112849aa77517d230fa580ad11b62e6eb7f4a8d3
This commit is contained in:
Andrew Poelstra 2024-02-22 23:45:42 +00:00
commit 31c0bf8d5f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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::UpperHex
/// - core::fmt::Display /// - core::fmt::Display
/// - core::str::FromStr /// - core::str::FromStr
/// - hex::FromHex
macro_rules! impl_bytes_newtype { macro_rules! impl_bytes_newtype {
($t:ident, $len:literal) => { ($t:ident, $len:literal) => {
impl $t { impl $t {
@ -68,6 +67,11 @@ macro_rules! impl_bytes_newtype {
self.0 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 { 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 { impl core::str::FromStr for $t {
type Err = $crate::hex::HexToArrayError; 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")] #[cfg(feature = "serde")]
@ -145,17 +136,17 @@ macro_rules! impl_bytes_newtype {
use $crate::serde::de::Unexpected; use $crate::serde::de::Unexpected;
if let Ok(hex) = core::str::from_utf8(v) { 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 { } else {
return Err(E::invalid_value(Unexpected::Bytes(v), &self)); 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 where
E: $crate::serde::de::Error, 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)
} }
} }