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]>,
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<R>(&self, reader: &mut BitStreamReader<R>) -> Result<u64, io::Error>
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 }

View File

@ -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<D: io::Read>(
mut d: D,
fn read_bytes_from_finite_reader<D: io::Read + ?Sized>(
d: &mut D,
mut opts: ReadBytesFromFiniteReaderOpts,
) -> Result<Vec<u8>, 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(),

View File

@ -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<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];
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]

View File

@ -196,7 +196,7 @@ where
}
// 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 buf = [0u8; 64];
loop {