Use encoding:: prefix on encoding trait
What changed, and why it matters
This commit is a routine code cleanup in the rust-bitcoin library. It changes how certain traits from another crate are imported and referenced, adding an 'encoding::' prefix and using 'as _' for traits that are only needed for their methods. There is no functional change to how the software behaves, and no security issue is present.
No action required. This is a non-security refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors imports and trait references in four files to use the encoding:: path prefix for traits from the encoding crate (e.g., encoding::Encodable, encoding::Decoder). It also switches some imports to as _ because the traits are only used for method resolution. The diff shows only syntactic changes; no logic, bounds, or behavior was altered.
Changed components
primitives/src/block.rsprimitives/src/script/owned.rsprimitives/src/transaction.rsprimitives/src/witness.rsInspect captured patch +43 / −38
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index b507068d..6088b8af 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -14,7 +14,7 @@ use core::marker::PhantomData;
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use encoding::{ArrayDecoder, Decodable, Decoder, Decoder6, Encodable};
+use encoding::{ArrayDecoder, Decoder6};
#[cfg(feature = "alloc")]
use encoding::{CompactSizeEncoder, Decoder2, Encoder2, SliceEncoder, VecDecoder};
use hashes::{sha256d, HashEngine as _};
@@ -275,7 +275,7 @@ mod sealed {
#[cfg(all(feature = "hex", feature = "alloc"))]
impl core::str::FromStr for Block<Unchecked>
where
- Self: Decodable,
+ Self: encoding::Decodable,
{
type Err = ParseBlockError;
@@ -287,7 +287,7 @@ where
#[cfg(all(feature = "hex", feature = "alloc"))]
impl<V: Validation> fmt::Display for Block<V>
where
- Self: Encodable,
+ Self: encoding::Encodable,
{
#[allow(clippy::use_self)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -344,7 +344,7 @@ encoding::encoder_newtype! {
}
#[cfg(feature = "alloc")]
-impl<V> Encodable for Block<V>
+impl<V> encoding::Encodable for Block<V>
where
V: Validation,
{
@@ -385,7 +385,7 @@ impl Default for BlockDecoder {
}
#[cfg(feature = "alloc")]
-impl Decoder for BlockDecoder {
+impl encoding::Decoder for BlockDecoder {
type Output = Block;
type Error = BlockDecoderError;
@@ -405,7 +405,7 @@ impl Decoder for BlockDecoder {
}
#[cfg(feature = "alloc")]
-impl Decodable for Block<Unchecked> {
+impl encoding::Decodable for Block<Unchecked> {
type Decoder = BlockDecoder;
fn decoder() -> Self::Decoder {
BlockDecoder(Decoder2::new(Header::decoder(), VecDecoder::<Transaction>::new()))
@@ -415,7 +415,7 @@ impl Decodable for Block<Unchecked> {
/// An error consensus decoding a [`Block`].
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct BlockDecoderError(<BlockInnerDecoder as Decoder>::Error);
+pub struct BlockDecoderError(<BlockInnerDecoder as encoding::Decoder>::Error);
#[cfg(feature = "alloc")]
impl From<Infallible> for BlockDecoderError {
@@ -656,7 +656,7 @@ encoding::encoder_newtype_exact! {
);
}
-impl Encodable for Header {
+impl encoding::Encodable for Header {
type Encoder<'e> = HeaderEncoder<'e>;
fn encoder(&self) -> Self::Encoder<'_> {
@@ -696,7 +696,7 @@ impl HeaderDecoder {
))
}
- fn from_inner(e: <HeaderInnerDecoder as Decoder>::Error) -> HeaderDecoderError {
+ fn from_inner(e: <HeaderInnerDecoder as encoding::Decoder>::Error) -> HeaderDecoderError {
match e {
encoding::Decoder6Error::First(e) => HeaderDecoderError::Version(e),
encoding::Decoder6Error::Second(e) => HeaderDecoderError::PrevBlockhash(e),
@@ -712,7 +712,7 @@ impl Default for HeaderDecoder {
fn default() -> Self { Self::new() }
}
-impl Decoder for HeaderDecoder {
+impl encoding::Decoder for HeaderDecoder {
type Output = Header;
type Error = HeaderDecoderError;
@@ -733,7 +733,7 @@ impl Decoder for HeaderDecoder {
fn read_limit(&self) -> usize { self.0.read_limit() }
}
-impl Decodable for Header {
+impl encoding::Decodable for Header {
type Decoder = HeaderDecoder;
fn decoder() -> Self::Decoder {
HeaderDecoder(Decoder6::new(
@@ -908,7 +908,7 @@ encoding::encoder_newtype_exact! {
pub struct VersionEncoder<'e>(encoding::ArrayEncoder<4>);
}
-impl Encodable for Version {
+impl encoding::Encodable for Version {
type Encoder<'e> = VersionEncoder<'e>;
fn encoder(&self) -> Self::Encoder<'_> {
VersionEncoder::new(encoding::ArrayEncoder::without_length_prefix(
@@ -1019,7 +1019,9 @@ mod tests {
#[cfg(all(feature = "alloc", feature = "hex"))]
use core::str::FromStr as _;
- use encoding::{Decoder, Encoder};
+ #[cfg(feature = "alloc")]
+ use encoding::Decodable as _;
+ use encoding::{Decoder as _, Encodable as _, Encoder as _};
#[cfg(all(feature = "serde", feature = "hex", feature = "alloc"))]
use serde::{Deserialize, Serialize};
diff --git a/primitives/src/script/owned.rs b/primitives/src/script/owned.rs
index 5895f018..b5d043ee 100644
--- a/primitives/src/script/owned.rs
+++ b/primitives/src/script/owned.rs
@@ -7,7 +7,7 @@ use core::ops::{Deref, DerefMut};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use encoding::{ByteVecDecoder, ByteVecDecoderError, Decodable, Decoder};
+use encoding::{ByteVecDecoder, ByteVecDecoderError};
use internals::write_err;
use super::Script;
@@ -195,7 +195,7 @@ impl<T> Default for ScriptBufDecoder<T> {
fn default() -> Self { Self::new() }
}
-impl<T> Decoder for ScriptBufDecoder<T> {
+impl<T> encoding::Decoder for ScriptBufDecoder<T> {
type Output = ScriptBuf<T>;
type Error = ScriptBufDecoderError;
@@ -213,7 +213,7 @@ impl<T> Decoder for ScriptBufDecoder<T> {
fn read_limit(&self) -> usize { self.0.read_limit() }
}
-impl<T> Decodable for ScriptBuf<T> {
+impl<T> encoding::Decodable for ScriptBuf<T> {
type Decoder = ScriptBufDecoder<T>;
fn decoder() -> Self::Decoder { ScriptBufDecoder(ByteVecDecoder::new(), PhantomData) }
}
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 4072635b..75525afa 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -17,11 +17,11 @@ use core::{cmp, mem};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
-use encoding::{ArrayEncoder, BytesEncoder, Encodable, Encoder2, UnexpectedEofError};
+use encoding::{ArrayEncoder, BytesEncoder, Encoder2, UnexpectedEofError};
#[cfg(feature = "alloc")]
use encoding::{
- CompactSizeEncoder, Decodable, Decoder, Decoder2, Decoder3, Encoder, Encoder3, Encoder6,
- SliceEncoder, VecDecoder, VecDecoderError,
+ CompactSizeEncoder, Decoder2, Decoder3, Encodable as _, Encoder3, Encoder6, SliceEncoder,
+ VecDecoder, VecDecoderError,
};
#[cfg(feature = "alloc")]
use hashes::sha256d;
@@ -341,7 +341,7 @@ encoding::encoder_newtype! {
}
#[cfg(feature = "alloc")]
-impl Encodable for Transaction {
+impl encoding::Encodable for Transaction {
type Encoder<'e>
= TransactionEncoder<'e>
where
@@ -453,7 +453,7 @@ impl Default for TransactionDecoder {
#[cfg(feature = "alloc")]
#[allow(clippy::too_many_lines)] // TODO: Can we clean this up?
-impl Decoder for TransactionDecoder {
+impl encoding::Decoder for TransactionDecoder {
type Output = Transaction;
type Error = TransactionDecoderError;
@@ -662,7 +662,7 @@ impl Decoder for TransactionDecoder {
}
#[cfg(feature = "alloc")]
-impl Decodable for Transaction {
+impl encoding::Decodable for Transaction {
type Decoder = TransactionDecoder;
fn decoder() -> Self::Decoder { TransactionDecoder::new() }
}
@@ -868,7 +868,7 @@ encoding::encoder_newtype_exact! {
}
#[cfg(feature = "alloc")]
-impl Encodable for TxIn {
+impl encoding::Encodable for TxIn {
type Encoder<'e>
= Encoder3<OutPointEncoder<'e>, ScriptEncoder<'e>, SequenceEncoder<'e>>
where
@@ -900,7 +900,7 @@ impl<'e> WitnessesEncoder<'e> {
}
#[cfg(feature = "alloc")]
-impl Encoder for WitnessesEncoder<'_> {
+impl encoding::Encoder for WitnessesEncoder<'_> {
#[inline]
fn current_chunk(&self) -> &[u8] {
self.cur_enc.as_ref().map(WitnessEncoder::current_chunk).unwrap_or_default()
@@ -960,7 +960,7 @@ impl Default for TxInDecoder {
}
#[cfg(feature = "alloc")]
-impl Decoder for TxInDecoder {
+impl encoding::Decoder for TxInDecoder {
type Output = TxIn;
type Error = TxInDecoderError;
@@ -980,7 +980,7 @@ impl Decoder for TxInDecoder {
}
#[cfg(feature = "alloc")]
-impl Decodable for TxIn {
+impl encoding::Decodable for TxIn {
type Decoder = TxInDecoder;
fn decoder() -> Self::Decoder {
TxInDecoder(Decoder3::new(
@@ -994,7 +994,7 @@ impl Decodable for TxIn {
/// An error consensus decoding a `TxIn`.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct TxInDecoderError(<TxInInnerDecoder as Decoder>::Error);
+pub struct TxInDecoderError(<TxInInnerDecoder as encoding::Decoder>::Error);
#[cfg(feature = "alloc")]
impl From<Infallible> for TxInDecoderError {
@@ -1041,7 +1041,7 @@ encoding::encoder_newtype_exact! {
}
#[cfg(feature = "alloc")]
-impl Encodable for TxOut {
+impl encoding::Encodable for TxOut {
type Encoder<'e>
= Encoder2<AmountEncoder<'e>, ScriptEncoder<'e>>
where
@@ -1073,7 +1073,7 @@ impl Default for TxOutDecoder {
}
#[cfg(feature = "alloc")]
-impl Decoder for TxOutDecoder {
+impl encoding::Decoder for TxOutDecoder {
type Output = TxOut;
type Error = TxOutDecoderError;
@@ -1093,7 +1093,7 @@ impl Decoder for TxOutDecoder {
}
#[cfg(feature = "alloc")]
-impl Decodable for TxOut {
+impl encoding::Decodable for TxOut {
type Decoder = TxOutDecoder;
fn decoder() -> Self::Decoder {
TxOutDecoder(Decoder2::new(AmountDecoder::new(), ScriptPubKeyBufDecoder::new()))
@@ -1103,7 +1103,7 @@ impl Decodable for TxOut {
/// An error consensus decoding a `TxOut`.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct TxOutDecoderError(<TxOutInnerDecoder as Decoder>::Error);
+pub struct TxOutDecoderError(<TxOutInnerDecoder as encoding::Decoder>::Error);
#[cfg(feature = "alloc")]
impl From<Infallible> for TxOutDecoderError {
@@ -1151,7 +1151,7 @@ encoding::encoder_newtype_exact! {
pub struct OutPointEncoder<'e>(Encoder2<BytesEncoder<'e>, ArrayEncoder<4>>);
}
-impl Encodable for OutPoint {
+impl encoding::Encodable for OutPoint {
type Encoder<'e>
= OutPointEncoder<'e>
where
@@ -1649,7 +1649,7 @@ mod tests {
#[cfg(feature = "hex")]
use core::str::FromStr as _;
- use encoding::Encoder as _;
+ use encoding::{Decodable as _, Decoder as _, Encoder as _};
#[cfg(feature = "hex")]
use hex_unstable::hex;
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index fc0924e8..bcb200a7 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -13,8 +13,8 @@ use arbitrary::{Arbitrary, Unstructured};
#[cfg(doc)]
use encoding::Decoder4;
use encoding::{
- self, BytesEncoder, CompactSizeDecoder, CompactSizeDecoderError, CompactSizeEncoder, Decoder,
- Encodable, Encoder, Encoder2,
+ self, BytesEncoder, CompactSizeDecoder, CompactSizeDecoderError, CompactSizeEncoder,
+ Decoder as _, Encoder2,
};
#[cfg(feature = "hex")]
use hex::DecodeVariableLengthBytesError;
@@ -261,7 +261,7 @@ fn decode_cursor(bytes: &[u8], start_of_indices: usize, index: usize) -> Option<
/// The encoder for the [`Witness`] type.
pub struct WitnessEncoder<'e>(Encoder2<CompactSizeEncoder, BytesEncoder<'e>>);
-impl Encodable for Witness {
+impl encoding::Encodable for Witness {
type Encoder<'e>
= WitnessEncoder<'e>
where
@@ -276,7 +276,7 @@ impl Encodable for Witness {
}
}
-impl Encoder for WitnessEncoder<'_> {
+impl encoding::Encoder for WitnessEncoder<'_> {
#[inline]
fn current_chunk(&self) -> &[u8] { self.0.current_chunk() }
@@ -347,7 +347,7 @@ impl Default for WitnessDecoder {
fn default() -> Self { Self::new() }
}
-impl Decoder for WitnessDecoder {
+impl encoding::Decoder for WitnessDecoder {
type Output = Witness;
type Error = WitnessDecoderError;
@@ -977,6 +977,9 @@ mod test {
#[cfg(feature = "alloc")]
use encoding::Decodable as _;
+ #[cfg(feature = "alloc")]
+ use encoding::Encodable as _;
+ use encoding::Encoder as _;
use super::*;
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.