Merge rust-bitcoin/rust-bitcoin#3784: units: Document public macros

b1399d193f units: Improve rustdocs on macros (Tobin C. Harding)
706c7c9f5f Hide impl_tryfrom_str (Tobin C. Harding)
2675cd1040 units: Re-order impl_parse_str (Tobin C. Harding)
4cc7aae6b9 Hide impl_tryfrom_str_from_int_infallible (Tobin C. Harding)
fc9e40ab1a units: Re-order impl_parse_str_from_int_infallible (Tobin C. Harding)

Pull request description:

  Document the public macros in `units::parse`. Done as part of #3632

  First 2 patches are trivial preparatory refactor.

ACKs for top commit:
  apoelstra:
    ACK b1399d193fcb20c09457a1f22c5c1dd8009c84b1; successfully ran local tests

Tree-SHA512: e2b0196adb37b3616963e3e3ded1c2be95f98fe33a4e6edb269b7eca1ddb66b82be139f4edb3269a5cf69a73b3c803845fe83a5f6e300b08abf9fcb0da602084
This commit is contained in:
merge-script 2025-01-03 17:12:09 +00:00
commit e8a52ccf8f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 71 additions and 35 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -91,27 +91,30 @@ pub fn int<T: Integer, S: AsRef<str> + Into<InputString>>(s: S) -> Result<T, Par
})
}
/// Implements `TryFrom<$from> 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<Self, Self::Error> {
$crate::parse::int::<$inner, $from>(s).map($to::$fn)
}
}
)*
}
}
/// Implements `FromStr` and `TryFrom<{&str, String, Box<str>}> 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<Box<str>>`
/// * `TryFrom<String>`
///
/// # 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<Self, Self::Error> {
$inner_fn(s)
}
fn try_from(s: $from) -> $crate::_export::_core::result::Result<Self, Self::Error> {
$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<Box<str>>`
/// * `TryFrom<String>`
///
/// # 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<Self, Self::Error> {
$inner_fn(s)
}
}
)*
}
}
/// Removes the prefix `0x` (or `0X`) from a hex string.
///
/// # Errors