| 1 |
pub trait ByteSource: Sized { |
| 2 |
type Error: Sized; |
| 3 |
|
| 4 |
#[inline(always)] |
| 5 |
fn read_byte(&mut self) -> Result<u8, Self::Error> { |
| 6 |
let mut byte = 0; |
| 7 |
self.read_bytes(core::slice::from_mut(&mut byte)) |
| 8 |
.map(|()| byte) |
| 9 |
} |
| 10 |
|
| 11 |
fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>; |
| 12 |
|
| 13 |
fn skip_bytes(&mut self, bytes: u32) -> Result<(), Self::Error> { |
| 14 |
fn skip_chunks<const SIZE: usize, R>( |
| 15 |
reader: &mut R, |
| 16 |
mut bytes: usize, |
| 17 |
) -> Result<(), R::Error> |
| 18 |
where |
| 19 |
R: ByteSource, |
| 20 |
{ |
| 21 |
let mut buf = [0; SIZE]; |
| 22 |
while bytes > 0 { |
| 23 |
let to_read = bytes.min(SIZE); |
| 24 |
reader.read_bytes(&mut buf[0..to_read])?; |
| 25 |
bytes -= to_read; |
| 26 |
} |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
|
| 30 |
match bytes { |
| 31 |
0..256 => skip_chunks::<8, Self>(self, bytes as usize), |
| 32 |
256..1024 => skip_chunks::<256, Self>(self, bytes as usize), |
| 33 |
1024..4096 => skip_chunks::<1024, Self>(self, bytes as usize), |
| 34 |
_ => skip_chunks::<4096, Self>(self, bytes as usize), |
| 35 |
} |
| 36 |
} |
| 37 |
} |