Stop relying on blanket Read impl for all &mut Read

This commit is contained in:
Matt Corallo 2023-09-11 17:56:39 +00:00
parent 6aa7ccf841
commit 2364e1a877
4 changed files with 15 additions and 19 deletions

View File

@ -271,9 +271,7 @@ impl GcsFilterReader {
I::Item: Borrow<[u8]>, I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
let mut decoder = reader; let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0));
let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0));
let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = let mut mapped =
@ -316,9 +314,7 @@ impl GcsFilterReader {
I::Item: Borrow<[u8]>, I::Item: Borrow<[u8]>,
R: io::Read + ?Sized, R: io::Read + ?Sized,
{ {
let mut decoder = reader; let n_elements: VarInt = Decodable::consensus_decode(reader).unwrap_or(VarInt(0));
let n_elements: VarInt = Decodable::consensus_decode(&mut decoder).unwrap_or(VarInt(0));
let reader = &mut decoder;
// map hashes to [0, n_elements << grp] // map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m; let nm = n_elements.0 * self.m;
let mut mapped = let mut mapped =
@ -442,7 +438,7 @@ impl GcsFilter {
/// Golomb-Rice decodes a number from a bit stream (parameter 2^k). /// 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> fn golomb_rice_decode<R>(&self, reader: &mut BitStreamReader<R>) -> Result<u64, io::Error>
where where
R: io::Read, R: io::Read + ?Sized,
{ {
let mut q = 0u64; let mut q = 0u64;
while reader.read(1)? == 1 { while reader.read(1)? == 1 {
@ -459,13 +455,13 @@ impl GcsFilter {
} }
/// Bitwise stream reader. /// Bitwise stream reader.
pub struct BitStreamReader<'a, R> { pub struct BitStreamReader<'a, R: ?Sized> {
buffer: [u8; 1], buffer: [u8; 1],
offset: u8, offset: u8,
reader: &'a mut R, 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`. /// Creates a new [`BitStreamReader`] that reads bitwise from a given `reader`.
pub fn new(reader: &'a mut R) -> BitStreamReader<'a, R> { pub fn new(reader: &'a mut R) -> BitStreamReader<'a, R> {
BitStreamReader { buffer: [0u8], reader, offset: 8 } BitStreamReader { buffer: [0u8], reader, offset: 8 }

View File

@ -662,8 +662,8 @@ struct ReadBytesFromFiniteReaderOpts {
/// This function relies on reader being bound in amount of data /// This function relies on reader being bound in amount of data
/// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`]. /// it returns for OOM protection. See [`Decodable::consensus_decode_from_finite_reader`].
#[inline] #[inline]
fn read_bytes_from_finite_reader<D: io::Read>( fn read_bytes_from_finite_reader<D: io::Read + ?Sized>(
mut d: D, d: &mut D,
mut opts: ReadBytesFromFiniteReaderOpts, mut opts: ReadBytesFromFiniteReaderOpts,
) -> Result<Vec<u8>, Error> { ) -> Result<Vec<u8>, Error> {
let mut ret = vec![]; let mut ret = vec![];
@ -1234,7 +1234,7 @@ mod tests {
for chunk_size in 1..20 { for chunk_size in 1..20 {
assert_eq!( assert_eq!(
read_bytes_from_finite_reader( read_bytes_from_finite_reader(
io::Cursor::new(&data), &mut io::Cursor::new(&data),
ReadBytesFromFiniteReaderOpts { len: data.len(), chunk_size } ReadBytesFromFiniteReaderOpts { len: data.len(), chunk_size }
) )
.unwrap(), .unwrap(),

View File

@ -76,7 +76,7 @@ impl PublicKey {
/// ///
/// This internally reads the first byte before reading the rest, so /// This internally reads the first byte before reading the rest, so
/// use of a `BufReader` is recommended. /// use of a `BufReader` is recommended.
pub fn read_from<R: io::Read>(mut reader: R) -> Result<Self, io::Error> { pub fn read_from<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, io::Error> {
let mut bytes = [0; 65]; let mut bytes = [0; 65];
reader.read_exact(&mut bytes[0..1])?; reader.read_exact(&mut bytes[0..1])?;
@ -914,11 +914,11 @@ mod tests {
// sanity checks // sanity checks
assert!(PublicKey::read_from(&mut cursor).is_err()); assert!(PublicKey::read_from(&mut cursor).is_err());
assert!(PublicKey::read_from(io::Cursor::new(&[])).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[])).is_err());
assert!(PublicKey::read_from(io::Cursor::new(&[0; 33][..])).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[0; 33][..])).is_err());
assert!(PublicKey::read_from(io::Cursor::new(&[2; 32][..])).is_err()); assert!(PublicKey::read_from(&mut io::Cursor::new(&[2; 32][..])).is_err());
assert!(PublicKey::read_from(io::Cursor::new(&[0; 65][..])).is_err()); assert!(PublicKey::read_from(&mut 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(&[4; 64][..])).is_err());
} }
#[test] #[test]

View File

@ -196,7 +196,7 @@ where
} }
// core2 doesn't have read_to_end // core2 doesn't have read_to_end
pub(crate) fn read_to_end<D: io::Read>(mut d: D) -> Result<Vec<u8>, io::Error> { pub(crate) fn read_to_end<D: io::Read + ?Sized>(d: &mut D) -> Result<Vec<u8>, io::Error> {
let mut result = vec![]; let mut result = vec![];
let mut buf = [0u8; 64]; let mut buf = [0u8; 64];
loop { loop {