units: Convert various functions to const
What changed, and why it matters
This commit is a routine code-quality improvement. It marks several simple constructor and getter functions as 'const', meaning they can be evaluated at compile time. There is no change to program logic, no bug fix, and no security relevance.
No security action required. Treat as a normal API/ergonomics improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch converts zero-argument constructors (e.g., AmountDecoder::new, BlockHeightDecoder::new, LockTimeDecoder::new, SequenceDecoder::new, BlockTimeDecoder::new) and simple getter functions (e.g., Sequence::to_consensus_u32, Sequence::low_u16) from pub fn to pub const fn. The function bodies are unchanged. This only affects Rust const-eval compatibility and API surface; it does not alter runtime behavior, input validation, memory safety, or cryptographic logic.
Changed components
units/src/amount/unsigned.rsunits/src/block.rsunits/src/locktime/absolute/mod.rsunits/src/sequence.rsunits/src/time.rsInspect captured patch +15 / −15
diff --git a/api/units/all-features.txt b/api/units/all-features.txt
index e6d5ba5b..07889bdf 100644
--- a/api/units/all-features.txt
+++ b/api/units/all-features.txt
@@ -1544,8 +1544,10 @@ pub const fn bitcoin_units::Weight::to_vbytes_floor(self) -> u64
pub const fn bitcoin_units::Weight::to_wu(self) -> u64
pub const fn bitcoin_units::absolute::is_block_height(n: u32) -> bool
pub const fn bitcoin_units::absolute::is_block_time(n: u32) -> bool
+pub const fn bitcoin_units::amount::AmountDecoder::new() -> Self
pub const fn bitcoin_units::block::BlockHeight::from_u32(inner: u32) -> Self
pub const fn bitcoin_units::block::BlockHeight::to_u32(self) -> u32
+pub const fn bitcoin_units::block::BlockHeightDecoder::new() -> Self
pub const fn bitcoin_units::block::BlockHeightInterval::from_u32(inner: u32) -> Self
pub const fn bitcoin_units::block::BlockHeightInterval::to_u32(self) -> u32
pub const fn bitcoin_units::block::BlockMtp::from_u32(inner: u32) -> Self
@@ -1559,6 +1561,7 @@ pub const fn bitcoin_units::locktime::absolute::Height::to_u32(self) -> u32
pub const fn bitcoin_units::locktime::absolute::LockTime::is_block_height(self) -> bool
pub const fn bitcoin_units::locktime::absolute::LockTime::is_block_time(self) -> bool
pub const fn bitcoin_units::locktime::absolute::LockTime::is_same_unit(self, other: Self) -> bool
+pub const fn bitcoin_units::locktime::absolute::LockTimeDecoder::new() -> Self
pub const fn bitcoin_units::locktime::absolute::MedianTimePast::from_u32(n: u32) -> core::result::Result<Self, bitcoin_units::locktime::absolute::error::ConversionError>
pub const fn bitcoin_units::locktime::absolute::MedianTimePast::to_u32(self) -> u32
pub const fn bitcoin_units::locktime::absolute::is_block_height(n: u32) -> bool
@@ -1579,6 +1582,9 @@ pub const fn bitcoin_units::locktime::relative::NumberOf512Seconds::to_seconds(s
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::from_height(blocks: u16) -> Self
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_consensus_u32(self) -> u32
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_height(self) -> u16
+pub const fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
+pub const fn bitcoin_units::sequence::SequenceDecoder::new() -> Self
+pub const fn bitcoin_units::time::BlockTimeDecoder::new() -> Self
pub enum bitcoin_units::NumOpResult<T>
pub enum bitcoin_units::absolute::LockTime
pub enum bitcoin_units::locktime::absolute::LockTime
@@ -1915,7 +1921,6 @@ pub fn bitcoin_units::Weight::try_from(s: alloc::boxed::Box<str>) -> core::resul
pub fn bitcoin_units::Weight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
pub fn bitcoin_units::amount::AmountDecoder::default() -> Self
pub fn bitcoin_units::amount::AmountDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
-pub fn bitcoin_units::amount::AmountDecoder::new() -> Self
pub fn bitcoin_units::amount::AmountDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
pub fn bitcoin_units::amount::AmountDecoder::read_limit(&self) -> usize
pub fn bitcoin_units::amount::AmountEncoder::advance(&mut self) -> bool
@@ -2034,7 +2039,6 @@ pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::boxed::Box<str>) ->
pub fn bitcoin_units::block::BlockHeight::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
pub fn bitcoin_units::block::BlockHeightDecoder::default() -> Self
pub fn bitcoin_units::block::BlockHeightDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
-pub fn bitcoin_units::block::BlockHeightDecoder::new() -> Self
pub fn bitcoin_units::block::BlockHeightDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
pub fn bitcoin_units::block::BlockHeightDecoder::read_limit(&self) -> usize
pub fn bitcoin_units::block::BlockHeightDecoderError::clone(&self) -> bitcoin_units::block::BlockHeightDecoderError
@@ -2184,7 +2188,6 @@ pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::boxed::Bo
pub fn bitcoin_units::locktime::absolute::LockTime::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::default() -> Self
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
-pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::new() -> Self
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
pub fn bitcoin_units::locktime::absolute::LockTimeDecoder::read_limit(&self) -> usize
pub fn bitcoin_units::locktime::absolute::LockTimeEncoder::advance(&mut self) -> bool
@@ -2442,7 +2445,6 @@ pub fn bitcoin_units::sequence::Sequence::is_relative_lock_time(self) -> bool
pub fn bitcoin_units::sequence::Sequence::is_time_locked(self) -> bool
pub fn bitcoin_units::sequence::Sequence::partial_cmp(&self, other: &bitcoin_units::sequence::Sequence) -> core::option::Option<core::cmp::Ordering>
pub fn bitcoin_units::sequence::Sequence::serialize<__S>(&self, __serializer: __S) -> core::result::Result<<__S as serde::ser::Serializer>::Ok, <__S as serde::ser::Serializer>::Error> where __S: serde::ser::Serializer
-pub fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
pub fn bitcoin_units::sequence::Sequence::to_hex(self) -> alloc::string::String
pub fn bitcoin_units::sequence::Sequence::to_relative_lock_time(self) -> core::option::Option<bitcoin_units::locktime::relative::LockTime>
pub fn bitcoin_units::sequence::Sequence::try_from(s: &str) -> core::result::Result<Self, Self::Error>
@@ -2450,7 +2452,6 @@ pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::boxed::Box<str>) ->
pub fn bitcoin_units::sequence::Sequence::try_from(s: alloc::string::String) -> core::result::Result<Self, Self::Error>
pub fn bitcoin_units::sequence::SequenceDecoder::default() -> Self
pub fn bitcoin_units::sequence::SequenceDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
-pub fn bitcoin_units::sequence::SequenceDecoder::new() -> Self
pub fn bitcoin_units::sequence::SequenceDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
pub fn bitcoin_units::sequence::SequenceDecoder::read_limit(&self) -> usize
pub fn bitcoin_units::sequence::SequenceDecoderError::clone(&self) -> bitcoin_units::sequence::SequenceDecoderError
@@ -2462,7 +2463,6 @@ pub fn bitcoin_units::sequence::SequenceEncoder::advance(&mut self) -> bool
pub fn bitcoin_units::sequence::SequenceEncoder::current_chunk(&self) -> &[u8]
pub fn bitcoin_units::time::BlockTimeDecoder::default() -> Self
pub fn bitcoin_units::time::BlockTimeDecoder::end(self) -> core::result::Result<Self::Output, Self::Error>
-pub fn bitcoin_units::time::BlockTimeDecoder::new() -> Self
pub fn bitcoin_units::time::BlockTimeDecoder::push_bytes(&mut self, bytes: &mut &[u8]) -> core::result::Result<bool, Self::Error>
pub fn bitcoin_units::time::BlockTimeDecoder::read_limit(&self) -> usize
pub fn bitcoin_units::time::BlockTimeDecoderError::clone(&self) -> bitcoin_units::time::BlockTimeDecoderError
diff --git a/api/units/alloc-only.txt b/api/units/alloc-only.txt
index 04191097..de665ac2 100644
--- a/api/units/alloc-only.txt
+++ b/api/units/alloc-only.txt
@@ -1340,6 +1340,7 @@ pub const fn bitcoin_units::locktime::relative::NumberOf512Seconds::to_seconds(s
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::from_height(blocks: u16) -> Self
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_consensus_u32(self) -> u32
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_height(self) -> u16
+pub const fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
pub enum bitcoin_units::NumOpResult<T>
pub enum bitcoin_units::absolute::LockTime
pub enum bitcoin_units::locktime::absolute::LockTime
@@ -2079,7 +2080,6 @@ pub fn bitcoin_units::sequence::Sequence::is_rbf(self) -> bool
pub fn bitcoin_units::sequence::Sequence::is_relative_lock_time(self) -> bool
pub fn bitcoin_units::sequence::Sequence::is_time_locked(self) -> bool
pub fn bitcoin_units::sequence::Sequence::partial_cmp(&self, other: &bitcoin_units::sequence::Sequence) -> core::option::Option<core::cmp::Ordering>
-pub fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
pub fn bitcoin_units::sequence::Sequence::to_hex(self) -> alloc::string::String
pub fn bitcoin_units::sequence::Sequence::to_relative_lock_time(self) -> core::option::Option<bitcoin_units::locktime::relative::LockTime>
pub fn bitcoin_units::sequence::Sequence::try_from(s: &str) -> core::result::Result<Self, Self::Error>
diff --git a/api/units/no-features.txt b/api/units/no-features.txt
index 8b860555..369e5d91 100644
--- a/api/units/no-features.txt
+++ b/api/units/no-features.txt
@@ -1316,6 +1316,7 @@ pub const fn bitcoin_units::locktime::relative::NumberOf512Seconds::to_seconds(s
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::from_height(blocks: u16) -> Self
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_consensus_u32(self) -> u32
pub const fn bitcoin_units::locktime::relative::NumberOfBlocks::to_height(self) -> u16
+pub const fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
pub enum bitcoin_units::NumOpResult<T>
pub enum bitcoin_units::absolute::LockTime
pub enum bitcoin_units::locktime::absolute::LockTime
@@ -2020,7 +2021,6 @@ pub fn bitcoin_units::sequence::Sequence::is_rbf(self) -> bool
pub fn bitcoin_units::sequence::Sequence::is_relative_lock_time(self) -> bool
pub fn bitcoin_units::sequence::Sequence::is_time_locked(self) -> bool
pub fn bitcoin_units::sequence::Sequence::partial_cmp(&self, other: &bitcoin_units::sequence::Sequence) -> core::option::Option<core::cmp::Ordering>
-pub fn bitcoin_units::sequence::Sequence::to_consensus_u32(self) -> u32
pub fn bitcoin_units::sequence::Sequence::to_relative_lock_time(self) -> core::option::Option<bitcoin_units::locktime::relative::LockTime>
pub fn core::num::error::ParseIntError::from(value: bitcoin_units::parse_int::ParseIntError) -> Self
pub fn i64::mul(self, rhs: &bitcoin_units::SignedAmount) -> Self::Output
diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs
index abf9d788..87b4ceb1 100644
--- a/units/src/amount/unsigned.rs
+++ b/units/src/amount/unsigned.rs
@@ -576,7 +576,7 @@ pub struct AmountDecoder(encoding::ArrayDecoder<8>);
#[cfg(feature = "encoding")]
impl AmountDecoder {
/// Constructs a new [`Amount`] decoder.
- pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+ pub const fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
}
#[cfg(feature = "encoding")]
diff --git a/units/src/block.rs b/units/src/block.rs
index 6f9f7dc1..bc53095d 100644
--- a/units/src/block.rs
+++ b/units/src/block.rs
@@ -173,7 +173,7 @@ impl Default for BlockHeightDecoder {
#[cfg(feature = "encoding")]
impl BlockHeightDecoder {
/// Constructs a new [`BlockHeight`] decoder.
- pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+ pub const fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
}
#[cfg(feature = "encoding")]
diff --git a/units/src/locktime/absolute/mod.rs b/units/src/locktime/absolute/mod.rs
index a4ba9955..3400d586 100644
--- a/units/src/locktime/absolute/mod.rs
+++ b/units/src/locktime/absolute/mod.rs
@@ -426,7 +426,7 @@ pub struct LockTimeDecoder(encoding::ArrayDecoder<4>);
#[cfg(feature = "encoding")]
impl LockTimeDecoder {
/// Constructs a new [`LockTime`] decoder.
- pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+ pub const fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
}
#[cfg(feature = "encoding")]
diff --git a/units/src/sequence.rs b/units/src/sequence.rs
index 3c903ed3..b165eef5 100644
--- a/units/src/sequence.rs
+++ b/units/src/sequence.rs
@@ -198,7 +198,7 @@ impl Sequence {
/// Returns the inner 32bit integer value of Sequence.
#[inline]
- pub fn to_consensus_u32(self) -> u32 { self.0 }
+ pub const fn to_consensus_u32(self) -> u32 { self.0 }
/// Gets the hex representation of this [`Sequence`].
#[cfg(feature = "alloc")]
@@ -228,7 +228,7 @@ impl Sequence {
///
/// BIP-0068 only uses the low 16 bits for relative lock value.
#[inline]
- fn low_u16(self) -> u16 { self.0 as u16 }
+ const fn low_u16(self) -> u16 { self.0 as u16 }
}
impl Default for Sequence {
@@ -296,7 +296,7 @@ impl Default for SequenceDecoder {
#[cfg(feature = "encoding")]
impl SequenceDecoder {
/// Constructs a new [`Sequence`] decoder.
- pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+ pub const fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
}
#[cfg(feature = "encoding")]
diff --git a/units/src/time.rs b/units/src/time.rs
index 59410415..c15cc3e6 100644
--- a/units/src/time.rs
+++ b/units/src/time.rs
@@ -106,7 +106,7 @@ impl Default for BlockTimeDecoder {
#[cfg(feature = "encoding")]
impl BlockTimeDecoder {
/// Constructs a new [`BlockTime`] decoder.
- pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+ pub const fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
}
#[cfg(feature = "encoding")]
Why this scored 15/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.