Skip to content

Commit

Permalink
Remove ?Sized from consensus_encode
Browse files Browse the repository at this point in the history
I am not sure why we added `?Sized`. It was added in rust-bitcoin#1035 while we
changed the reader to use `&mut R` instead of `R`.

Done in preparation for making the `Read` and `BufRead` traits dyn
compatible (object safe).

Remove `?Sized` trait bound from `consensus_encode`.
  • Loading branch information
tcharding committed Jan 17, 2025
1 parent dc76043 commit 789c578
Show file tree
Hide file tree
Showing 28 changed files with 144 additions and 144 deletions.
16 changes: 8 additions & 8 deletions bitcoin/src/bip152.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ impl convert::AsRef<Transaction> for PrefilledTransaction {

impl Encodable for PrefilledTransaction {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
Ok(w.emit_compact_size(self.idx)? + self.tx.consensus_encode(w)?)
}
}

impl Decodable for PrefilledTransaction {
#[inline]
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
let idx = r.read_compact_size()?;
let idx = u16::try_from(idx).map_err(|_| {
consensus::parse_failed_error("BIP152 prefilled tx index out of bounds")
Expand Down Expand Up @@ -137,14 +137,14 @@ impl ShortId {

impl Encodable for ShortId {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
self.0.consensus_encode(w)
}
}

impl Decodable for ShortId {
#[inline]
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<ShortId, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<ShortId, encode::Error> {
Ok(ShortId(Decodable::consensus_decode(r)?))
}
}
Expand All @@ -169,7 +169,7 @@ pub struct HeaderAndShortIds {
}

impl Decodable for HeaderAndShortIds {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
let header_short_ids = HeaderAndShortIds {
header: Decodable::consensus_decode(r)?,
nonce: Decodable::consensus_decode(r)?,
Expand All @@ -184,7 +184,7 @@ impl Decodable for HeaderAndShortIds {
}

impl Encodable for HeaderAndShortIds {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = 0;
len += self.header.consensus_encode(w)?;
len += self.nonce.consensus_encode(w)?;
Expand Down Expand Up @@ -292,7 +292,7 @@ impl Encodable for BlockTransactionsRequest {
///
/// Panics if the index overflows [`u64::MAX`]. This happens when [`BlockTransactionsRequest::indexes`]
/// contains an entry with the value [`u64::MAX`] as `u64` overflows during differential encoding.
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = self.block_hash.consensus_encode(w)?;
// Manually encode indexes because they are differentially encoded VarInts.
len += w.emit_compact_size(self.indexes.len())?;
Expand All @@ -306,7 +306,7 @@ impl Encodable for BlockTransactionsRequest {
}

impl Decodable for BlockTransactionsRequest {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
Ok(BlockTransactionsRequest {
block_hash: BlockHash::consensus_decode(r)?,
indexes: {
Expand Down
12 changes: 6 additions & 6 deletions bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl BlockFilterReader {
where
I: Iterator,
I::Item: Borrow<[u8]>,
R: BufRead + ?Sized,
R: BufRead,
{
self.reader.match_any(reader, query)
}
Expand All @@ -270,7 +270,7 @@ impl BlockFilterReader {
where
I: Iterator,
I::Item: Borrow<[u8]>,
R: BufRead + ?Sized,
R: BufRead,
{
self.reader.match_all(reader, query)
}
Expand All @@ -293,7 +293,7 @@ impl GcsFilterReader {
where
I: Iterator,
I::Item: Borrow<[u8]>,
R: BufRead + ?Sized,
R: BufRead,
{
let n_elements = reader.read_compact_size().unwrap_or(0);
// map hashes to [0, n_elements << grp]
Expand Down Expand Up @@ -336,7 +336,7 @@ impl GcsFilterReader {
where
I: Iterator,
I::Item: Borrow<[u8]>,
R: BufRead + ?Sized,
R: BufRead,
{
let n_elements = reader.read_compact_size().unwrap_or(0);
// map hashes to [0, n_elements << grp]
Expand Down Expand Up @@ -462,7 +462,7 @@ impl GcsFilter {
/// Golomb-Rice decodes a number from a bit stream (parameter 2^k).
fn golomb_rice_decode<R>(&self, reader: &mut BitStreamReader<R>) -> Result<u64, io::Error>
where
R: BufRead + ?Sized,
R: BufRead,
{
let mut q = 0u64;
while reader.read(1)? == 1 {
Expand All @@ -485,7 +485,7 @@ pub struct BitStreamReader<'a, R: ?Sized> {
reader: &'a mut R,
}

impl<'a, R: BufRead + ?Sized> BitStreamReader<'a, R> {
impl<'a, R: BufRead> BitStreamReader<'a, R> {
/// Constructs a new [`BitStreamReader`] that reads bitwise from a given `reader`.
pub fn new(reader: &'a mut R) -> BitStreamReader<'a, R> {
BitStreamReader { buffer: [0u8], reader, offset: 8 }
Expand Down
12 changes: 6 additions & 6 deletions bitcoin/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ crate::internal_macros::define_extension_trait! {
}

impl Encodable for Version {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
self.to_consensus().consensus_encode(w)
}
}

impl Decodable for Version {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
Decodable::consensus_decode(r).map(Version::from_consensus)
}
}
Expand Down Expand Up @@ -322,7 +322,7 @@ fn block_base_size(transactions: &[Transaction]) -> usize {

impl Encodable for Block<Unchecked> {
#[inline]
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: io::Write>(&self, w: &mut W) -> Result<usize, io::Error> {
// TODO: Should we be able to encode without cloning?
// This is ok, we decode as unchecked anyway.
let block = self.clone().assume_checked(None);
Expand All @@ -332,7 +332,7 @@ impl Encodable for Block<Unchecked> {

impl Encodable for Block<Checked> {
#[inline]
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: io::Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = 0;
len += self.header().consensus_encode(w)?;

Expand All @@ -348,7 +348,7 @@ impl Encodable for Block<Checked> {

impl Decodable for Block<Unchecked> {
#[inline]
fn consensus_decode_from_finite_reader<R: io::BufRead + ?Sized>(
fn consensus_decode_from_finite_reader<R: io::BufRead>(
r: &mut R,
) -> Result<Block, encode::Error> {
let header = Decodable::consensus_decode_from_finite_reader(r)?;
Expand All @@ -358,7 +358,7 @@ impl Decodable for Block<Unchecked> {
}

#[inline]
fn consensus_decode<R: io::BufRead + ?Sized>(r: &mut R) -> Result<Block, encode::Error> {
fn consensus_decode<R: io::BufRead>(r: &mut R) -> Result<Block, encode::Error> {
let mut r = r.take(internals::ToU64::to_u64(encode::MAX_VEC_SIZE));
let header = Decodable::consensus_decode(&mut r)?;
let transactions = Decodable::consensus_decode(&mut r)?;
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ pub mod locktime {

impl Encodable for LockTime {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let v = self.to_consensus_u32();
v.consensus_encode(w)
}
}

impl Decodable for LockTime {
#[inline]
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
u32::consensus_decode(r).map(LockTime::from_consensus)
}
}
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/blockdata/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,21 @@ fn opcode_to_verify(opcode: Option<Opcode>) -> Option<Opcode> {

impl Encodable for Script {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
crate::consensus::encode::consensus_encode_with_size(self.as_bytes(), w)
}
}

impl Encodable for ScriptBuf {
#[inline]
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
self.as_script().consensus_encode(w)
}
}

impl Decodable for ScriptBuf {
#[inline]
fn consensus_decode_from_finite_reader<R: BufRead + ?Sized>(
fn consensus_decode_from_finite_reader<R: BufRead>(
r: &mut R,
) -> Result<Self, encode::Error> {
let v: Vec<u8> = Decodable::consensus_decode_from_finite_reader(r)?;
Expand Down
20 changes: 10 additions & 10 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,27 +639,27 @@ crate::internal_macros::define_extension_trait! {
}

impl Encodable for Version {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
self.0.consensus_encode(w)
}
}

impl Decodable for Version {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
Decodable::consensus_decode(r).map(Version)
}
}

impl_consensus_encoding!(TxOut, value, script_pubkey);

impl Encodable for OutPoint {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let len = self.txid.consensus_encode(w)?;
Ok(len + self.vout.consensus_encode(w)?)
}
}
impl Decodable for OutPoint {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
Ok(OutPoint {
txid: Decodable::consensus_decode(r)?,
vout: Decodable::consensus_decode(r)?,
Expand All @@ -668,7 +668,7 @@ impl Decodable for OutPoint {
}

impl Encodable for TxIn {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = 0;
len += self.previous_output.consensus_encode(w)?;
len += self.script_sig.consensus_encode(w)?;
Expand All @@ -678,7 +678,7 @@ impl Encodable for TxIn {
}
impl Decodable for TxIn {
#[inline]
fn consensus_decode_from_finite_reader<R: BufRead + ?Sized>(
fn consensus_decode_from_finite_reader<R: BufRead>(
r: &mut R,
) -> Result<Self, encode::Error> {
Ok(TxIn {
Expand All @@ -691,19 +691,19 @@ impl Decodable for TxIn {
}

impl Encodable for Sequence {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
self.0.consensus_encode(w)
}
}

impl Decodable for Sequence {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, encode::Error> {
Decodable::consensus_decode(r).map(Sequence)
}
}

impl Encodable for Transaction {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = 0;
len += self.version.consensus_encode(w)?;

Expand All @@ -727,7 +727,7 @@ impl Encodable for Transaction {
}

impl Decodable for Transaction {
fn consensus_decode_from_finite_reader<R: BufRead + ?Sized>(
fn consensus_decode_from_finite_reader<R: BufRead>(
r: &mut R,
) -> Result<Self, encode::Error> {
let version = Version::consensus_decode_from_finite_reader(r)?;
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::Script;
pub use primitives::witness::{Iter, Witness};

impl Decodable for Witness {
fn consensus_decode<R: BufRead + ?Sized>(r: &mut R) -> Result<Self, Error> {
fn consensus_decode<R: BufRead>(r: &mut R) -> Result<Self, Error> {
let witness_elements = r.read_compact_size()? as usize;
// Minimum size of witness element is 1 byte, so if the count is
// greater than MAX_VEC_SIZE we must return an error.
Expand Down Expand Up @@ -98,7 +98,7 @@ fn resize_if_needed(vec: &mut Vec<u8>, required_len: usize) {

impl Encodable for Witness {
// `self.content` includes the varints so encoding here includes them, as expected.
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
fn consensus_encode<W: Write>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut written = w.emit_compact_size(self.len())?;

for element in self.iter() {
Expand Down
Loading

0 comments on commit 789c578

Please sign in to comment.