Merge rust-bitcoin/rust-bitcoin#3560: units: Remove `alloc` feature gating from modules

38b8c6f5ea units: Remove a bunch of feature gateing (Tobin C. Harding)
e347b50614 Use InputString in hex prefix error types (Tobin C. Harding)
ff20249bdc Remove unneeded feature gate (Tobin C. Harding)
88f6621e30 Split parse macros (Tobin C. Harding)

Pull request description:

  Remove the feature gating from the modules in `units` - BOOM!

  Close: #3213

ACKs for top commit:
  apoelstra:
    ACK 38b8c6f5ea2d997e08aae496ed333abeedab9406; successfully ran local tests; nice!

Tree-SHA512: 75c7e70e64c705fe0e32d92d9a0557cd77f5cd5d68a50f274be4a128331f5ae69259f6c6a9d483699478cbc2c40ebadcb83e1d419952287f74657906ca65caaf
This commit is contained in:
merge-script 2024-11-02 01:24:30 +00:00
commit 60841f689f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 16 additions and 27 deletions

View File

@ -22,23 +22,16 @@ extern crate alloc;
extern crate std; extern crate std;
pub mod amount; pub mod amount;
#[cfg(feature = "alloc")]
pub mod block; pub mod block;
#[cfg(feature = "alloc")]
pub mod fee_rate; pub mod fee_rate;
#[cfg(feature = "alloc")]
pub mod locktime; pub mod locktime;
#[cfg(feature = "alloc")]
pub mod parse; pub mod parse;
#[cfg(feature = "alloc")]
pub mod weight; pub mod weight;
#[doc(inline)]
pub use self::amount::{Amount, SignedAmount};
#[cfg(feature = "alloc")]
#[doc(inline)] #[doc(inline)]
#[rustfmt::skip] #[rustfmt::skip]
pub use self::{ pub use self::{
amount::{Amount, SignedAmount},
block::{BlockHeight, BlockInterval}, block::{BlockHeight, BlockInterval},
fee_rate::FeeRate, fee_rate::FeeRate,
weight::Weight weight::Weight

View File

@ -2,15 +2,11 @@
//! Provides [`Height`] and [`Time`] types used by the `rust-bitcoin` `absolute::LockTime` type. //! Provides [`Height`] and [`Time`] types used by the `rust-bitcoin` `absolute::LockTime` type.
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, string::String};
use core::fmt; use core::fmt;
use internals::error::InputString; use internals::error::InputString;
use crate::parse::ParseIntError; use crate::parse::{self, ParseIntError};
#[cfg(feature = "alloc")]
use crate::parse;
/// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]). /// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]).
/// ///

View File

@ -2,7 +2,6 @@
//! Parsing utilities. //! Parsing utilities.
use alloc::string::String;
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
@ -107,10 +106,9 @@ macro_rules! impl_tryfrom_str_from_int_infallible {
#[macro_export] #[macro_export]
macro_rules! impl_parse_str_from_int_infallible { macro_rules! impl_parse_str_from_int_infallible {
($to:ident, $inner:ident, $fn:ident) => { ($to:ident, $inner:ident, $fn:ident) => {
#[cfg(all(feature = "alloc", not(feature = "std")))] $crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn);
$crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn; alloc::string::String, $to, $inner, $fn; alloc::boxed::Box<str>, $to, $inner, $fn); #[cfg(feature = "alloc")]
#[cfg(feature = "std")] $crate::impl_tryfrom_str_from_int_infallible!(alloc::string::String, $to, $inner, $fn; alloc::boxed::Box<str>, $to, $inner, $fn);
$crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn; std::string::String, $to, $inner, $fn; std::boxed::Box<str>, $to, $inner, $fn);
impl core::str::FromStr for $to { impl core::str::FromStr for $to {
type Err = $crate::parse::ParseIntError; type Err = $crate::parse::ParseIntError;
@ -143,7 +141,9 @@ macro_rules! impl_tryfrom_str {
#[macro_export] #[macro_export]
macro_rules! impl_parse_str { macro_rules! impl_parse_str {
($to:ty, $err:ty, $inner_fn:expr) => { ($to:ty, $err:ty, $inner_fn:expr) => {
$crate::impl_tryfrom_str!(&str, $to, $err, $inner_fn; String, $to, $err, $inner_fn; Box<str>, $to, $err, $inner_fn); $crate::impl_tryfrom_str!(&str, $to, $err, $inner_fn);
#[cfg(feature = "alloc")]
$crate::impl_tryfrom_str!(alloc::string::String, $to, $err, $inner_fn; alloc::boxed::Box<str>, $to, $err, $inner_fn);
impl core::str::FromStr for $to { impl core::str::FromStr for $to {
type Err = $err; type Err = $err;
@ -166,7 +166,7 @@ pub fn hex_remove_prefix(s: &str) -> Result<&str, PrefixedHexError> {
} else if let Some(checked) = s.strip_prefix("0X") { } else if let Some(checked) = s.strip_prefix("0X") {
Ok(checked) Ok(checked)
} else { } else {
Err(MissingPrefixError::new(s.into()).into()) Err(MissingPrefixError::new(s).into())
} }
} }
@ -177,7 +177,7 @@ pub fn hex_remove_prefix(s: &str) -> Result<&str, PrefixedHexError> {
/// If the input string contains a prefix. /// If the input string contains a prefix.
pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> { pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> {
if s.starts_with("0x") || s.starts_with("0X") { if s.starts_with("0x") || s.starts_with("0X") {
return Err(ContainsPrefixError::new(s.into()).into()); return Err(ContainsPrefixError::new(s).into());
} }
Ok(s) Ok(s)
} }
@ -374,17 +374,17 @@ impl From<ParseIntError> for UnprefixedHexError {
/// Error returned when a hex string is missing a prefix (e.g. `0x`). /// Error returned when a hex string is missing a prefix (e.g. `0x`).
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct MissingPrefixError { pub struct MissingPrefixError {
hex: String, hex: InputString,
} }
impl MissingPrefixError { impl MissingPrefixError {
/// Creates an error from the string with the missing prefix. /// Creates an error from the string with the missing prefix.
pub(crate) fn new(hex: String) -> Self { Self { hex } } pub(crate) fn new(hex: &str) -> Self { Self { hex: hex.into() } }
} }
impl fmt::Display for MissingPrefixError { impl fmt::Display for MissingPrefixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hex string is missing a prefix (e.g. 0x): {}", self.hex) write!(f, "{} because it is missing the '0x' prefix", self.hex.display_cannot_parse("hex"))
} }
} }
@ -394,17 +394,17 @@ impl std::error::Error for MissingPrefixError {}
/// Error when hex string contains a prefix (e.g. 0x). /// Error when hex string contains a prefix (e.g. 0x).
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct ContainsPrefixError { pub struct ContainsPrefixError {
hex: String, hex: InputString,
} }
impl ContainsPrefixError { impl ContainsPrefixError {
/// Creates an error from the string that contains the prefix. /// Creates an error from the string that contains the prefix.
pub(crate) fn new(hex: String) -> Self { Self { hex } } pub(crate) fn new(hex: &str) -> Self { Self { hex: hex.into() } }
} }
impl fmt::Display for ContainsPrefixError { impl fmt::Display for ContainsPrefixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hex string contains a prefix: {}", self.hex) write!(f, "{} because it contains the '0x' prefix", self.hex.display_cannot_parse("hex"))
} }
} }