diff --git a/units/src/lib.rs b/units/src/lib.rs index 189e11aff..f0e3994a1 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -22,23 +22,16 @@ extern crate alloc; extern crate std; pub mod amount; -#[cfg(feature = "alloc")] pub mod block; -#[cfg(feature = "alloc")] pub mod fee_rate; -#[cfg(feature = "alloc")] pub mod locktime; -#[cfg(feature = "alloc")] pub mod parse; -#[cfg(feature = "alloc")] pub mod weight; -#[doc(inline)] -pub use self::amount::{Amount, SignedAmount}; -#[cfg(feature = "alloc")] #[doc(inline)] #[rustfmt::skip] pub use self::{ + amount::{Amount, SignedAmount}, block::{BlockHeight, BlockInterval}, fee_rate::FeeRate, weight::Weight diff --git a/units/src/locktime/absolute.rs b/units/src/locktime/absolute.rs index 92283468d..8407eaece 100644 --- a/units/src/locktime/absolute.rs +++ b/units/src/locktime/absolute.rs @@ -2,15 +2,11 @@ //! 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 internals::error::InputString; -use crate::parse::ParseIntError; -#[cfg(feature = "alloc")] -use crate::parse; +use crate::parse::{self, ParseIntError}; /// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]). /// diff --git a/units/src/parse.rs b/units/src/parse.rs index a2ee0e4db..9e670c5bb 100644 --- a/units/src/parse.rs +++ b/units/src/parse.rs @@ -2,7 +2,6 @@ //! Parsing utilities. -use alloc::string::String; use core::fmt; use core::str::FromStr; @@ -107,10 +106,9 @@ macro_rules! impl_tryfrom_str_from_int_infallible { #[macro_export] macro_rules! impl_parse_str_from_int_infallible { ($to:ident, $inner:ident, $fn:ident) => { - #[cfg(all(feature = "alloc", not(feature = "std")))] - $crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn; alloc::string::String, $to, $inner, $fn; alloc::boxed::Box, $to, $inner, $fn); - #[cfg(feature = "std")] - $crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn; std::string::String, $to, $inner, $fn; std::boxed::Box, $to, $inner, $fn); + $crate::impl_tryfrom_str_from_int_infallible!(&str, $to, $inner, $fn); + #[cfg(feature = "alloc")] + $crate::impl_tryfrom_str_from_int_infallible!(alloc::string::String, $to, $inner, $fn; alloc::boxed::Box, $to, $inner, $fn); impl core::str::FromStr for $to { type Err = $crate::parse::ParseIntError; @@ -143,7 +141,9 @@ macro_rules! impl_tryfrom_str { #[macro_export] macro_rules! impl_parse_str { ($to:ty, $err:ty, $inner_fn:expr) => { - $crate::impl_tryfrom_str!(&str, $to, $err, $inner_fn; String, $to, $err, $inner_fn; Box, $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, $to, $err, $inner_fn); impl core::str::FromStr for $to { 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") { Ok(checked) } 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. pub fn hex_check_unprefixed(s: &str) -> Result<&str, UnprefixedHexError> { if s.starts_with("0x") || s.starts_with("0X") { - return Err(ContainsPrefixError::new(s.into()).into()); + return Err(ContainsPrefixError::new(s).into()); } Ok(s) } @@ -374,17 +374,17 @@ impl From for UnprefixedHexError { /// Error returned when a hex string is missing a prefix (e.g. `0x`). #[derive(Debug, Clone, Eq, PartialEq)] pub struct MissingPrefixError { - hex: String, + hex: InputString, } impl MissingPrefixError { /// 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 { 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). #[derive(Debug, Clone, Eq, PartialEq)] pub struct ContainsPrefixError { - hex: String, + hex: InputString, } impl ContainsPrefixError { /// 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 { 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")) } }