Move discard_exact into BufReadExt trait.

This commit is contained in:
KAMADA Ken'ichi 2020-08-12 23:35:00 +09:00
parent 0807862f0f
commit 4b18cbfe92
2 changed files with 14 additions and 9 deletions

View File

@ -29,7 +29,7 @@ use std::io::Read;
use crate::endian::{Endian, BigEndian};
use crate::error::Error;
use crate::util::discard_exact;
use crate::util::BufReadExt;
// PNG file signature [PNG12 12.12].
const PNG_SIG: [u8; 8] = *b"\x89PNG\x0d\x0a\x1a\x0a";
@ -77,7 +77,7 @@ fn get_exif_attr_sub<R>(reader: &mut R)
return Ok(data);
}
// Chunk data and CRC.
discard_exact(reader, len + 4)?;
reader.discard_exact(len + 4)?;
}
}

View File

@ -48,14 +48,19 @@ pub fn read64<R>(reader: &mut R) -> Result<u64, io::Error> where R: io::Read {
Ok(u64::from_be_bytes(buf))
}
pub fn discard_exact<R>(reader: &mut R, mut len: usize)
-> Result<(), io::Error> where R: io::BufRead {
while len > 0 {
let consume_len = reader.fill_buf()?.len().min(len);
reader.consume(consume_len);
len -= consume_len;
pub trait BufReadExt {
fn discard_exact(&mut self, len: usize) -> io::Result<()>;
}
impl<T> BufReadExt for T where T: io::BufRead {
fn discard_exact(&mut self, mut len: usize) -> io::Result<()> {
while len > 0 {
let consume_len = self.fill_buf()?.len().min(len);
self.consume(consume_len);
len -= consume_len;
}
Ok(())
}
Ok(())
}
// This function must not be called with more than 4 bytes.