Move discard_exact into BufReadExt trait.
This commit is contained in:
parent
0807862f0f
commit
4b18cbfe92
|
@ -29,7 +29,7 @@ use std::io::Read;
|
||||||
|
|
||||||
use crate::endian::{Endian, BigEndian};
|
use crate::endian::{Endian, BigEndian};
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::util::discard_exact;
|
use crate::util::BufReadExt;
|
||||||
|
|
||||||
// PNG file signature [PNG12 12.12].
|
// PNG file signature [PNG12 12.12].
|
||||||
const PNG_SIG: [u8; 8] = *b"\x89PNG\x0d\x0a\x1a\x0a";
|
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);
|
return Ok(data);
|
||||||
}
|
}
|
||||||
// Chunk data and CRC.
|
// Chunk data and CRC.
|
||||||
discard_exact(reader, len + 4)?;
|
reader.discard_exact(len + 4)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/util.rs
19
src/util.rs
|
@ -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))
|
Ok(u64::from_be_bytes(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn discard_exact<R>(reader: &mut R, mut len: usize)
|
pub trait BufReadExt {
|
||||||
-> Result<(), io::Error> where R: io::BufRead {
|
fn discard_exact(&mut self, len: usize) -> io::Result<()>;
|
||||||
while len > 0 {
|
}
|
||||||
let consume_len = reader.fill_buf()?.len().min(len);
|
|
||||||
reader.consume(consume_len);
|
impl<T> BufReadExt for T where T: io::BufRead {
|
||||||
len -= consume_len;
|
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.
|
// This function must not be called with more than 4 bytes.
|
||||||
|
|
Loading…
Reference in New Issue