Struct ZReader
pub struct ZReader<T> { /* private fields */ }
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
impl<T: ZByteReaderTrait> ZReader<T>
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.
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.
fn get_u32_be(&mut self) -> u32Read u32 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u32 read.
fn get_u32_le(&mut self) -> u32Read u32 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u32 read.
impl<T: ZByteReaderTrait> ZReader<T>
fn new(source: T) -> ZReader<T>Create a new reader from a source that implements the [ZByteReaderTrait]
fn consume(self) -> TDestroy this reader returning the underlying source of the bytes from which we were decoding
fn skip(&mut self, num: usize) -> Result<u64, ZByteIoError>Skip ahead ignoring
numbytesFor 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.ErrorIf something went wrong
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.ErrorIf something went wrong
fn seek(&mut self, from: ZSeekFrom) -> Result<u64, ZByteIoError>Move around a stream of bytes
This is analogous to the [std::io::Seek] trait with the same ergonomics only implemented to allow use in a
no_stdenvironmentArguments
from: The seek operation type.
Returns
Ok(u64): The new position from the start of the stream.- Error if something went wrong.
fn read_u8(&mut self) -> u8Read a single byte from the underlying stream
If an error occurs, it will return
0as default output hence it may be difficult to distinguish a0from the underlying source and a0from an error. For that there is [Self::read_u8_err]Returns.
- The next byte on the stream.
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
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.
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
Nvalue 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.
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]
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
positionbytes from the start of the bufferArguments
position: The current position to move the cursor.
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
fn position(&mut self) -> Result<u64, ZByteIoError>Return the current position of the inner reader or an error if that occurred when reading.
Like eof, the perf characteristics may vary depending on underlying reader
Returns
Ok(u64): The current position of the inner reader
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
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
fn read_all(&mut self, buf: &mut Vec<u8>) -> Result<usize, ZByteIoError>Read all bytes remaining in this input to sink until we hit eof
Returns
Ok(usize): The actual number of bytes added to the sinkErr()An error that occurred when reading bytes
impl<T: ZByteReaderTrait> ZReader<T>
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.
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.
fn get_u64_be(&mut self) -> u64Read u64 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u64 read.
fn get_u64_le(&mut self) -> u64Read u64 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u64 read.
impl<T: ZByteReaderTrait> ZReader<T>
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.
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.
fn get_u16_be(&mut self) -> u16Read u16 as a big endian integer Returning 0 if the underlying buffer does not have enough bytes for a u16 read.
fn get_u16_le(&mut self) -> u16Read u16 as a little endian integer Returning 0 if the underlying buffer does not have enough bytes for a u16 read.
Trait Implementations
impl<T> Read for ZReader<T>
where
T: ZByteReaderTrait,
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Auto Trait Implementations
impl<T> Freeze for ZReader<T>
where
T: Freeze,
impl<T> RefUnwindSafe for ZReader<T>
where
T: RefUnwindSafe,
impl<T> Send for ZReader<T>
where
T: Send,
impl<T> Sync for ZReader<T>
where
T: Sync,
impl<T> Unpin for ZReader<T>
where
T: Unpin,
impl<T> UnsafeUnpin for ZReader<T>
where
T: UnsafeUnpin,
impl<T> UnwindSafe for ZReader<T>
where
T: UnwindSafe,
Blanket Implementations
impl<T> Any for ZReader<T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for ZReader<T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for ZReader<T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for ZReader<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for ZReader<T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for ZReader<T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for ZReader<T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>