diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index 61b3c6e2..49347977 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -271,9 +271,7 @@ impl GcsFilterReader { I::Item: Borrow<[u8]>, R: io::Read + ?Sized, { - let mut decoder = reader; - let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0)); - let reader = &mut decoder; + let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0)); // map hashes to [0, n_elements << grp] let nm = n_elements.0 * self.m; let mut mapped = @@ -316,9 +314,7 @@ impl GcsFilterReader { I::Item: Borrow<[u8]>, R: io::Read + ?Sized, { - let mut decoder = reader; - let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0)); - let reader = &mut decoder; + let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0)); // map hashes to [0, n_elements << grp] let nm = n_elements.0 * self.m; let mut mapped = @@ -442,7 +438,7 @@ impl GcsFilter { /// Golomb-Rice decodes a number from a bit stream (parameter 2^k). fn golomb_rice_decode(&self, reader: &mut BitStreamReader) -> Result where - R: io::Read, + R: io::Read + ?Sized, { let mut q = 0u64; while reader.read(1)? == 1 { @@ -459,13 +455,13 @@ impl GcsFilter { } /// Bitwise stream reader. -pub struct BitStreamReader<'a, R> { +pub struct BitStreamReader<'a, R: ?Sized> { buffer: [u8; 1], offset: u8, reader: &'a mut R, } -impl<'a, R: io::Read> BitStreamReader<'a, R> { +impl<'a, R: io::Read + ?Sized> BitStreamReader<'a, R> { /// Creates 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 } diff --git a/bitcoin/src/consensus/encode.rs b/bitcoin/src/consensus/encode.rs index 42d11a1e..6d0a3a7b 100644 --- a/bitcoin/src/consensus/encode.rs +++ b/bitcoin/src/consensus/encode.rs @@ -662,8 +662,8 @@ struct ReadBytesFromFiniteReaderOpts { /// This function relies on reader being bound in amount of data /// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`]. #[inline] -fn read_bytes_from_finite_reader( - mut d: D, +fn read_bytes_from_finite_reader( + d: &mut D, mut opts: ReadBytesFromFiniteReaderOpts, ) -> Result, Error> { let mut ret = vec![]; @@ -1234,7 +1234,7 @@ mod tests { for chunk_size in 1..20 { assert_eq!( read_bytes_from_finite_reader( - io::Cursor::new(&data), + &mut io::Cursor::new(&data), ReadBytesFromFiniteReaderOpts { len: data.len(), chunk_size } ) .unwrap(), diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 7ba63a89..01ff2b13 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -76,7 +76,7 @@ impl PublicKey { /// /// This internally reads the first byte before reading the rest, so /// use of a `BufReader` is recommended. - pub fn read_from(mut reader: R) -> Result { + pub fn read_from(reader: &mut R) -> Result { let mut bytes = [0; 65]; reader.read_exact(&mut bytes[0..1])?; @@ -914,11 +914,11 @@ mod tests { // sanity checks assert!(PublicKey::read_from(&mut cursor).is_err()); - assert!(PublicKey::read_from(io::Cursor::new(&[])).is_err()); - assert!(PublicKey::read_from(io::Cursor::new(&[0; 33][..])).is_err()); - assert!(PublicKey::read_from(io::Cursor::new(&[2; 32][..])).is_err()); - assert!(PublicKey::read_from(io::Cursor::new(&[0; 65][..])).is_err()); - assert!(PublicKey::read_from(io::Cursor::new(&[4; 64][..])).is_err()); + assert!(PublicKey::read_from(&mut io::Cursor::new(&[])).is_err()); + assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 33][..])).is_err()); + assert!(PublicKey::read_from(&mut io::Cursor::new(&[2; 32][..])).is_err()); + assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 65][..])).is_err()); + assert!(PublicKey::read_from(&mut io::Cursor::new(&[4; 64][..])).is_err()); } #[test] diff --git a/bitcoin/src/psbt/raw.rs b/bitcoin/src/psbt/raw.rs index effd805b..85275835 100644 --- a/bitcoin/src/psbt/raw.rs +++ b/bitcoin/src/psbt/raw.rs @@ -196,7 +196,7 @@ where } // core2 doesn't have read_to_end -pub(crate) fn read_to_end(mut d: D) -> Result, io::Error> { +pub(crate) fn read_to_end(d: &mut D) -> Result, io::Error> { let mut result = vec![]; let mut buf = [0u8; 64]; loop {