units: Add FromStr to CompactTarget and BlockTime
What changed, and why it matters
This commit adds the ability to parse CompactTarget and BlockTime values from strings in the rust-bitcoin library. It is a routine consistency improvement that makes two numeric wrapper types behave like all other similar types in the library. There is no security relevance in the change itself.
No security action required. Review as normal API maintenance if consuming these types.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds FromStr implementations for CompactTarget (units/src/pow.rs) and BlockTime (units/src/time.rs) using the existing impl_parse_str_from_int_infallible! macro. This delegates parsing to the underlying u32’s FromStr in decimal and then constructs the wrapper via from_consensus / from_u32. It is purely an API completeness change; no existing behavior is altered and no unsafe code is introduced.
Changed components
units/src/pow.rsunits/src/time.rsInspect captured patch +6 / −0
diff --git a/units/src/pow.rs b/units/src/pow.rs
index e1040fa6..a73735c7 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -102,6 +102,8 @@ impl fmt::Binary for CompactTarget {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Binary::fmt(&self.0, f) }
}
+parse_int::impl_parse_str_from_int_infallible!(CompactTarget, u32, from_consensus);
+
#[cfg(feature = "encoding")]
encoding::encoder_newtype_exact! {
/// The encoder for the [`CompactTarget`] type.
diff --git a/units/src/time.rs b/units/src/time.rs
index 71a68dd8..e7c66cdb 100644
--- a/units/src/time.rs
+++ b/units/src/time.rs
@@ -18,6 +18,8 @@ use internals::write_err;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
+use crate::parse_int;
+
mod encapsulate {
/// A Bitcoin block timestamp.
///
@@ -63,6 +65,8 @@ impl From<BlockTime> for u32 {
fn from(t: BlockTime) -> Self { t.to_u32() }
}
+parse_int::impl_parse_str_from_int_infallible!(BlockTime, u32, from_u32);
+
#[cfg(feature = "serde")]
impl Serialize for BlockTime {
#[inline]
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.