diff --git a/api/units/all-features.txt b/api/units/all-features.txt index 08c1a2ce0..4cfb244a8 100644 --- a/api/units/all-features.txt +++ b/api/units/all-features.txt @@ -1222,8 +1222,6 @@ pub fn u64::from(value: bitcoin_units::weight::Weight) -> Self pub fn u64::mul(self, rhs: bitcoin_units::weight::Weight) -> Self::Output pub macro bitcoin_units::impl_parse_str! pub macro bitcoin_units::impl_parse_str_from_int_infallible! -pub macro bitcoin_units::impl_tryfrom_str! -pub macro bitcoin_units::impl_tryfrom_str_from_int_infallible! pub mod bitcoin_units pub mod bitcoin_units::amount pub mod bitcoin_units::amount::serde diff --git a/api/units/alloc-only.txt b/api/units/alloc-only.txt index d64e17e23..61e5afbfd 100644 --- a/api/units/alloc-only.txt +++ b/api/units/alloc-only.txt @@ -1066,8 +1066,6 @@ pub fn u64::from(value: bitcoin_units::weight::Weight) -> Self pub fn u64::mul(self, rhs: bitcoin_units::weight::Weight) -> Self::Output pub macro bitcoin_units::impl_parse_str! pub macro bitcoin_units::impl_parse_str_from_int_infallible! -pub macro bitcoin_units::impl_tryfrom_str! -pub macro bitcoin_units::impl_tryfrom_str_from_int_infallible! pub mod bitcoin_units pub mod bitcoin_units::amount pub mod bitcoin_units::block diff --git a/api/units/no-features.txt b/api/units/no-features.txt index db86722ee..367d8501a 100644 --- a/api/units/no-features.txt +++ b/api/units/no-features.txt @@ -1020,8 +1020,6 @@ pub fn u64::from(value: bitcoin_units::weight::Weight) -> Self pub fn u64::mul(self, rhs: bitcoin_units::weight::Weight) -> Self::Output pub macro bitcoin_units::impl_parse_str! pub macro bitcoin_units::impl_parse_str_from_int_infallible! -pub macro bitcoin_units::impl_tryfrom_str! -pub macro bitcoin_units::impl_tryfrom_str_from_int_infallible! pub mod bitcoin_units pub mod bitcoin_units::amount pub mod bitcoin_units::block diff --git a/units/src/parse.rs b/units/src/parse.rs index ec36d4b94..ec927716f 100644 --- a/units/src/parse.rs +++ b/units/src/parse.rs @@ -91,27 +91,30 @@ pub fn int + Into>(s: S) -> Result for $to` using `parse::int`, mapping the output using infallible -/// conversion function `fn`. -#[macro_export] -macro_rules! impl_tryfrom_str_from_int_infallible { - ($($from:ty, $to:ident, $inner:ident, $fn:ident);*) => { - $( - impl $crate::_export::_core::convert::TryFrom<$from> for $to { - type Error = $crate::parse::ParseIntError; - - fn try_from(s: $from) -> $crate::_export::_core::result::Result { - $crate::parse::int::<$inner, $from>(s).map($to::$fn) - } - } - )* - } -} - -/// Implements `FromStr` and `TryFrom<{&str, String, Box}> for $to` using `parse::int`, mapping -/// the output using infallible conversion function `fn`. +/// Implements standard parsing traits for `$type` by calling `parse::int`. /// -/// The `Error` type is `ParseIntError` +/// Once the string is converted to an integer the infallible conversion function `fn` is used to +/// create the type `to`. +/// +/// Implements: +/// +/// * `FromStr` +/// * `TryFrom<&str>` +/// +/// And if `alloc` feature is enabled in calling crate: +/// +/// * `TryFrom>` +/// * `TryFrom` +/// +/// # Arguments +/// +/// * `to` - the type converted to e.g., `impl From<&str> for $to`. +/// * `err` - the error type returned by `$inner_fn` (implies returned by `FromStr` and `TryFrom`). +/// * `fn`: The infallible conversion function to call to convert from an integer. +/// +/// # Errors +/// +/// If parsing the string fails then a `units::parse::ParseIntError` is returned. #[macro_export] macro_rules! impl_parse_str_from_int_infallible { ($to:ident, $inner:ident, $fn:ident) => { @@ -130,23 +133,45 @@ macro_rules! impl_parse_str_from_int_infallible { } } -/// Implements `TryFrom<$from> for $to`. +/// Implements `TryFrom<$from> for $to` using `parse::int`, mapping the output using infallible +/// conversion function `fn`. #[macro_export] -macro_rules! impl_tryfrom_str { - ($($from:ty, $to:ty, $err:ty, $inner_fn:expr);*) => { +#[doc(hidden)] // Helper macro called by `impl_parse_str_from_int_infallible`. +macro_rules! impl_tryfrom_str_from_int_infallible { + ($($from:ty, $to:ident, $inner:ident, $fn:ident);*) => { $( - impl $crate::_export::_core::convert::TryFrom<$from> for $to { - type Error = $err; + impl $crate::_export::_core::convert::TryFrom<$from> for $to { + type Error = $crate::parse::ParseIntError; - fn try_from(s: $from) -> $crate::_export::_core::result::Result { - $inner_fn(s) - } + fn try_from(s: $from) -> $crate::_export::_core::result::Result { + $crate::parse::int::<$inner, $from>(s).map($to::$fn) } + } )* } } -/// Implements standard parsing traits for `$type` by calling into `$inner_fn`. +/// Implements standard parsing traits for `$type` by calling through to `$inner_fn`. +/// +/// Implements: +/// +/// * `FromStr` +/// * `TryFrom<&str>` +/// +/// And if `alloc` feature is enabled in calling crate: +/// +/// * `TryFrom>` +/// * `TryFrom` +/// +/// # Arguments +/// +/// * `to` - the type converted to e.g., `impl From<&str> for $to`. +/// * `err` - the error type returned by `$inner_fn` (implies returned by `FromStr` and `TryFrom`). +/// * `inner_fn`: The fallible conversion function to call to convert from a string reference. +/// +/// # Errors +/// +/// All functions use the error returned by `$inner_fn`. #[macro_export] macro_rules! impl_parse_str { ($to:ty, $err:ty, $inner_fn:expr) => { @@ -164,6 +189,23 @@ macro_rules! impl_parse_str { } } +/// Implements `TryFrom<$from> for $to`. +#[macro_export] +#[doc(hidden)] // Helper macro called by `impl_parse_str`. +macro_rules! impl_tryfrom_str { + ($($from:ty, $to:ty, $err:ty, $inner_fn:expr);*) => { + $( + impl $crate::_export::_core::convert::TryFrom<$from> for $to { + type Error = $err; + + fn try_from(s: $from) -> $crate::_export::_core::result::Result { + $inner_fn(s) + } + } + )* + } +} + /// Removes the prefix `0x` (or `0X`) from a hex string. /// /// # Errors