What changed, and why it matters
This commit is a simple internal code reorganization. It moves a small helper trait called ToU64 from one internal crate (internals) into the main bitcoin crate, and updates import statements accordingly. There is no change to what the code does, no bug fix, and no security-relevant behavior change.
No security action needed. Treat as ordinary refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch relocates the ToU64 trait and its implementations from internals/src/lib.rs to bitcoin/src/lib.rs, keeping it public. All call sites are updated from internals::ToU64 to crate::ToU64 or crate::{…, ToU64, …}. The trait’s logic, including the usize const_assert and the u8/u16/u32/u64 blanket impls, is copied verbatim. No functional changes are introduced.
Changed components
bitcoin/src/lib.rsinternals/src/lib.rsbitcoin/src/bip158.rsbitcoin/src/blockdata/block.rsbitcoin/src/blockdata/script/borrowed.rsbitcoin/src/blockdata/script/owned.rsbitcoin/src/blockdata/transaction.rsbitcoin/src/consensus/encode.rsbitcoin/src/internal_macros.rsInspect captured patch +37 / −42
diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs
index d71b81c0..cbe0813b 100644
--- a/bitcoin/src/bip158.rs
+++ b/bitcoin/src/bip158.rs
@@ -41,7 +41,6 @@ use core::cmp::{self, Ordering};
use hashes::{sha256d, siphash24};
use internals::array::ArrayExt as _;
-use internals::ToU64 as _;
use io::{BufRead, Write};
use crate::block::{Block, BlockHash, Checked};
@@ -49,6 +48,7 @@ use crate::consensus::{ReadExt, WriteExt};
use crate::prelude::{BTreeSet, Borrow, Vec};
use crate::script::{ScriptPubKey, ScriptPubKeyExt as _};
use crate::transaction::OutPoint;
+use crate::ToU64 as _;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(no_inline)]
diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs
index 996f3620..f481a310 100644
--- a/bitcoin/src/blockdata/block.rs
+++ b/bitcoin/src/blockdata/block.rs
@@ -8,7 +8,6 @@
//! these blocks and the blockchain.
use encoding::CompactSizeEncoder;
-use internals::ToU64;
use io::{BufRead, Write};
use crate::consensus::encode::{self, Decodable, Encodable, WriteExt as _};
@@ -18,7 +17,7 @@ use crate::pow::TargetExt as _;
use crate::prelude::Vec;
use crate::script::{PushBytesExt as _, ScriptExt as _};
use crate::transaction::{Coinbase, Transaction, TransactionExt as _};
-use crate::{internal_macros, BlockTime, Target, Weight, Work};
+use crate::{internal_macros, BlockTime, Target, ToU64, Weight, Work};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
@@ -283,7 +282,7 @@ impl Decodable for Block<Unchecked> {
#[inline]
fn consensus_decode<R: io::BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
- let mut r = io::Read::take(r, internals::ToU64::to_u64(encode::MAX_VEC_SIZE));
+ let mut r = io::Read::take(r, crate::ToU64::to_u64(encode::MAX_VEC_SIZE));
let header = Decodable::consensus_decode(&mut r)?;
let transactions = Decodable::consensus_decode(&mut r)?;
@@ -411,7 +410,6 @@ mod tests {
use alloc::string::ToString;
use hex::hex;
- use internals::ToU64 as _;
use super::*;
use crate::consensus::encode::{deserialize, serialize};
diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs
index 6f6c6d4e..5e9bac5a 100644
--- a/bitcoin/src/blockdata/script/borrowed.rs
+++ b/bitcoin/src/blockdata/script/borrowed.rs
@@ -3,7 +3,6 @@
use core::fmt;
use internals::array::ArrayExt; // For `split_first`.
-use internals::ToU64 as _;
use super::witness_version::WitnessVersion;
use super::{
@@ -20,7 +19,7 @@ use crate::prelude::{sink, String, ToString};
use crate::script::{self, ScriptPubKeyBufExt as _};
use crate::taproot::{LeafVersion, TapLeafHash, TapLeafHashExt as _, TapNodeHash};
use crate::witness_program::P2A_PROGRAM;
-use crate::{internal_macros, Amount, FeeRate, ScriptPubKeyBuf, WitnessScriptBuf};
+use crate::{internal_macros, Amount, FeeRate, ScriptPubKeyBuf, ToU64 as _, WitnessScriptBuf};
internal_macros::define_extension_trait! {
/// Extension functionality for the [`Script`] type.
diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs
index c3d59126..f9d18520 100644
--- a/bitcoin/src/blockdata/script/owned.rs
+++ b/bitcoin/src/blockdata/script/owned.rs
@@ -3,13 +3,10 @@
#[cfg(doc)]
use core::ops::Deref;
-use internals::ToU64 as _;
-
use super::{
opcode_to_verify, write_scriptint, Builder, Error, Instruction, PushBytes, ScriptBuf,
ScriptExtPriv as _, ScriptPubKeyBuf, ScriptSigBuf, WitnessScript,
};
-use crate::internal_macros;
use crate::key::{
FullPublicKey, LegacyPublicKey, PubkeyHash, TapTweak, TweakedPublicKey, UntweakedPublicKey,
WPubkeyHash,
@@ -21,6 +18,7 @@ use crate::script::witness_program::{WitnessProgram, P2A_PROGRAM};
use crate::script::witness_version::WitnessVersion;
use crate::script::{self, ScriptHash, WScriptHash};
use crate::taproot::TapNodeHash;
+use crate::{internal_macros, ToU64 as _};
internal_macros::define_extension_trait! {
/// Extension functionality for the [`ScriptBuf`] type.
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index e136700c..57ace5e7 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -13,7 +13,7 @@
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use encoding::CompactSizeEncoder;
-use internals::{const_casts, ToU64};
+use internals::const_casts;
use io::{BufRead, Write};
use super::Weight;
@@ -27,7 +27,7 @@ use crate::script::{
#[cfg(doc)]
use crate::sighash::{EcdsaSighashType, TapSighashType};
use crate::witness::Witness;
-use crate::{internal_macros, Amount, FeeRate, Sequence, SignedAmount};
+use crate::{internal_macros, Amount, FeeRate, Sequence, SignedAmount, ToU64};
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(no_inline)]
diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs
index 8837c691..15450d03 100644
--- a/bitcoin/src/consensus/encode.rs
+++ b/bitcoin/src/consensus/encode.rs
@@ -20,12 +20,12 @@ use core::{cmp, mem, slice};
use encoding::{CompactSizeEncoder, Encoder};
use hashes::{sha256, sha256d, Hash};
use hex::DisplayHex as _;
-use internals::ToU64;
use io::{BufRead, Cursor, Read, Write};
use super::IterReader;
use crate::prelude::{rc, sync, Box, Cow, String, Vec};
use crate::taproot::TapLeafHash;
+use crate::ToU64;
#[rustfmt::skip] // Keep public re-exports separate.
pub use super::{Error, FromHexError, ParseError, DeserializeError};
diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs
index de9b54e2..c5f66d62 100644
--- a/bitcoin/src/internal_macros.rs
+++ b/bitcoin/src/internal_macros.rs
@@ -33,7 +33,7 @@ macro_rules! impl_consensus_encoding {
fn consensus_decode<R: $crate::io::BufRead + ?Sized>(
r: &mut R,
) -> core::result::Result<$thing, $crate::consensus::encode::Error> {
- let mut r = $crate::io::Read::take(r, internals::ToU64::to_u64($crate::consensus::encode::MAX_VEC_SIZE));
+ let mut r = $crate::io::Read::take(r, crate::ToU64::to_u64($crate::consensus::encode::MAX_VEC_SIZE));
Ok($thing {
$($field: $crate::consensus::Decodable::consensus_decode(&mut r)?),+
})
diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs
index fc1a53e0..bcb4fdec 100644
--- a/bitcoin/src/lib.rs
+++ b/bitcoin/src/lib.rs
@@ -321,3 +321,31 @@ mod encode_impls {
impl_encodable_for_u32_wrapper!(BlockHeight);
impl_encodable_for_u32_wrapper!(BlockHeightInterval);
}
+
+/// A conversion trait for unsigned integer types smaller than or equal to 64-bits.
+///
+/// This trait exists because [`usize`] doesn't implement `Into<u64>`. We only support 32 and 64 bit
+/// architectures because of consensus code so we can infallibly do the conversion.
+pub trait ToU64 {
+ /// Converts unsigned integer type to a [`u64`].
+ fn to_u64(self) -> u64;
+}
+
+macro_rules! impl_to_u64 {
+ ($($ty:ident),*) => {
+ $(
+ impl ToU64 for $ty { fn to_u64(self) -> u64 { self.into() } }
+ )*
+ }
+}
+impl_to_u64!(u8, u16, u32, u64);
+
+impl ToU64 for usize {
+ fn to_u64(self) -> u64 {
+ internals::const_assert!(
+ core::mem::size_of::<usize>() <= 8;
+ "platforms that have usize larger than 64 bits are not supported"
+ );
+ self as u64
+ }
+}
diff --git a/internals/src/lib.rs b/internals/src/lib.rs
index 4335b242..daa04c0a 100644
--- a/internals/src/lib.rs
+++ b/internals/src/lib.rs
@@ -46,31 +46,3 @@ pub mod slice;
#[macro_use]
pub mod serde;
pub mod const_casts;
-
-/// A conversion trait for unsigned integer types smaller than or equal to 64-bits.
-///
-/// This trait exists because [`usize`] doesn't implement `Into<u64>`. We only support 32 and 64 bit
-/// architectures because of consensus code so we can infallibly do the conversion.
-pub trait ToU64 {
- /// Converts unsigned integer type to a [`u64`].
- fn to_u64(self) -> u64;
-}
-
-macro_rules! impl_to_u64 {
- ($($ty:ident),*) => {
- $(
- impl ToU64 for $ty { fn to_u64(self) -> u64 { self.into() } }
- )*
- }
-}
-impl_to_u64!(u8, u16, u32, u64);
-
-impl ToU64 for usize {
- fn to_u64(self) -> u64 {
- crate::const_assert!(
- core::mem::size_of::<usize>() <= 8;
- "platforms that have usize larger than 64 bits are not supported"
- );
- self as u64
- }
-}
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.