pub struct ZReader<T: ZByteReaderTrait> { /* private fields */ }
Expand description
The image reader wrapper
This wraps anything that implements ZByteReaderTrait and extends the ability of the core trait methods by providing utilities like endian aware byte functions.
This prevents each implementation from providing its own
Implementations§
Source§impl<T: ZByteReaderTrait> ZReader<T>
impl<T: ZByteReaderTrait> ZReader<T>
Sourcepub fn new(source: T) -> ZReader<T>
pub fn new(source: T) -> ZReader<T>
Create a new reader from a source that implements the ZByteReaderTrait
Sourcepub fn consume(self) -> T
pub fn consume(self) -> T
Destroy this reader returning the underlying source of the bytes from which we were decoding
Sourcepub fn skip(&mut self, num: usize) -> Result<u64, ZByteIoError>
pub fn skip(&mut self, num: usize) -> Result<u64, ZByteIoError>
Skip ahead ignoring num
bytes
For more advanced seek methods see Self::seek that allows moving around via more advanced ways
§Arguments
- num: The number of bytes to skip.
§Returns
Ok(u64)
: The new position from the start of the stream.Error
If something went wrong
Sourcepub fn rewind(&mut self, num: usize) -> Result<u64, ZByteIoError>
pub fn rewind(&mut self, num: usize) -> Result<u64, ZByteIoError>
Move back from current position to a previous position
For more advanced seek methods see Self::seek that allows moving around via more advanced ways
§Arguments
num
: Positions to move before the current cursor
§Returns
Ok(u64)
: The new position from the start of the stream.Error
If something went wrong
Sourcepub fn seek(&mut self, from: ZSeekFrom) -> Result<u64, ZByteIoError>
pub fn seek(&mut self, from: ZSeekFrom) -> Result<u64, ZByteIoError>
Sourcepub fn read_u8(&mut self) -> u8
pub fn read_u8(&mut self) -> u8
Read a single byte from the underlying stream
If an error occurs, it will return 0
as default output
hence it may be difficult to distinguish a 0
from the underlying source
and a 0
from an error.
For that there is Self::read_u8_err
§Returns.
- The next byte on the stream.
Sourcepub fn read_u8_err(&mut self) -> Result<u8, ZByteIoError>
pub fn read_u8_err(&mut self) -> Result<u8, ZByteIoError>
Read a single byte returning an error if the read cannot be satisfied
§Returns
Ok(u8)
: The next byte- Error if the byte read could not be satisfied
Sourcepub fn peek_at(
&mut self,
position: usize,
num_bytes: usize,
) -> Result<&[u8], ZByteIoError>
pub fn peek_at( &mut self, position: usize, num_bytes: usize, ) -> Result<&[u8], ZByteIoError>
Look ahead position bytes and return a reference to num_bytes from that position, or an error if the peek would be out of bounds.
This doesn’t increment the position, bytes would have to be discarded at a later point.
Sourcepub fn read_fixed_bytes_or_error<const N: usize>(
&mut self,
) -> Result<[u8; N], ZByteIoError>
pub fn read_fixed_bytes_or_error<const N: usize>( &mut self, ) -> Result<[u8; N], ZByteIoError>
Read a fixed number of known bytes to a buffer and return the bytes or an error if it occurred.
The size of the N
value must be small enough to fit the stack space otherwise
this will cause a stack overflow :)
If you can ignore errors, you can use Self::read_fixed_bytes_or_zero
§Returns
Ok([u8;N])
: The bytes read from the source- An error if it occurred.
Sourcepub fn read_fixed_bytes_or_zero<const N: usize>(&mut self) -> [u8; N]
pub fn read_fixed_bytes_or_zero<const N: usize>(&mut self) -> [u8; N]
Read a fixed bytes to an array and if that is impossible, return an array containing zeros
If you want to handle errors, use Self::read_fixed_bytes_or_error
Sourcepub fn set_position(&mut self, position: usize) -> Result<(), ZByteIoError>
pub fn set_position(&mut self, position: usize) -> Result<(), ZByteIoError>
Move the cursor to a fixed position in the stream
This will move the cursor to exacltly position
bytes from the start of the buffer
§Arguments
position
: The current position to move the cursor.
Sourcepub fn eof(&mut self) -> Result<bool, ZByteIoError>
pub fn eof(&mut self) -> Result<bool, ZByteIoError>
Return true if the underlying buffer can no longer produce bytes
This call may be expensive depending on the underlying buffer type, e.g if it’s a file, we have to ask the os whether we have more contents, or in other words make a syscall.
Use that wisely
§Returns
Ok(bool)
: True if we are inEOF
, false if we can produce more bytes- Error if something went wrong
Sourcepub fn position(&mut self) -> Result<u64, ZByteIoError>
pub fn position(&mut self) -> Result<u64, ZByteIoError>
Sourcepub fn read_exact_bytes(&mut self, buf: &mut [u8]) -> Result<(), ZByteIoError>
pub fn read_exact_bytes(&mut self, buf: &mut [u8]) -> Result<(), ZByteIoError>
Read a fixed number of bytes from the underlying reader returning an error if that can’t be satisfied
Similar to [std::io::Read::read_exact]
§Returns
Ok(())
: If the read was successful- An error if the read was unsuccessful including failure to fill the whole bytes
Sourcepub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize, ZByteIoError>
pub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize, ZByteIoError>
Read some bytes from the inner reader, and return number of bytes read
The implementation may not read bytes enough to fill the buffer
Similar to [std::io::Read::read]
§Returns
Ok(usize)
: Number of bytes actually read to the buffer- An error if something went wrong
Source§impl<T: ZByteReaderTrait> ZReader<T>
impl<T: ZByteReaderTrait> ZReader<T>
Sourcepub fn get_u16_be_err(&mut self) -> Result<u16, ZByteIoError>
pub fn get_u16_be_err(&mut self) -> Result<u16, ZByteIoError>
Read u16 as a big endian integer Returning an error if the underlying buffer cannot support a u16 read.
Sourcepub fn get_u16_le_err(&mut self) -> Result<u16, ZByteIoError>
pub fn get_u16_le_err(&mut self) -> Result<u16, ZByteIoError>
Read u16 as a little endian integer Returning an error if the underlying buffer cannot support a u16 read.
Sourcepub fn get_u16_be(&mut self) -> u16
pub fn get_u16_be(&mut self) -> u16
Read u16 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u16 read.
Sourcepub fn get_u16_le(&mut self) -> u16
pub fn get_u16_le(&mut self) -> u16
Read u16 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u16 read.
Source§impl<T: ZByteReaderTrait> ZReader<T>
impl<T: ZByteReaderTrait> ZReader<T>
Sourcepub fn get_u32_be_err(&mut self) -> Result<u32, ZByteIoError>
pub fn get_u32_be_err(&mut self) -> Result<u32, ZByteIoError>
Read u32 as a big endian integer Returning an error if the underlying buffer cannot support a u32 read.
Sourcepub fn get_u32_le_err(&mut self) -> Result<u32, ZByteIoError>
pub fn get_u32_le_err(&mut self) -> Result<u32, ZByteIoError>
Read u32 as a little endian integer Returning an error if the underlying buffer cannot support a u32 read.
Sourcepub fn get_u32_be(&mut self) -> u32
pub fn get_u32_be(&mut self) -> u32
Read u32 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u32 read.
Sourcepub fn get_u32_le(&mut self) -> u32
pub fn get_u32_le(&mut self) -> u32
Read u32 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u32 read.
Source§impl<T: ZByteReaderTrait> ZReader<T>
impl<T: ZByteReaderTrait> ZReader<T>
Sourcepub fn get_u64_be_err(&mut self) -> Result<u64, ZByteIoError>
pub fn get_u64_be_err(&mut self) -> Result<u64, ZByteIoError>
Read u64 as a big endian integer Returning an error if the underlying buffer cannot support a u64 read.
Sourcepub fn get_u64_le_err(&mut self) -> Result<u64, ZByteIoError>
pub fn get_u64_le_err(&mut self) -> Result<u64, ZByteIoError>
Read u64 as a little endian integer Returning an error if the underlying buffer cannot support a u64 read.
Sourcepub fn get_u64_be(&mut self) -> u64
pub fn get_u64_be(&mut self) -> u64
Read u64 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u64 read.
Sourcepub fn get_u64_le(&mut self) -> u64
pub fn get_u64_le(&mut self) -> u64
Read u64 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u64 read.