ZReader

Struct ZReader 

Source
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>

Source

pub fn new(source: T) -> ZReader<T>

Create a new reader from a source that implements the ZByteReaderTrait

Source

pub fn consume(self) -> T

Destroy this reader returning the underlying source of the bytes from which we were decoding

Source

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
Source

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
Source

pub 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_std environment

§Arguments
  • from: The seek operation type.
§Returns
  • Ok(u64): The new position from the start of the stream.
  • Error if something went wrong.
Source

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.
Source

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
Source

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.

Source

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.
Source

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

Source

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.
Source

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 in EOF, false if we can produce more bytes
  • Error if something went wrong
Source

pub 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
Source

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
Source

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>

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source

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.

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> UnwindSafe for ZReader<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.