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) -> u32

Read 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) -> u32

Read 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) -> T

Destroy 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 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
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
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.
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.
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 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.
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 position bytes from the start of the buffer

Arguments

  • 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 in EOF, 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 sink
  • Err() 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) -> u64

Read 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) -> u64

Read 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) -> u16

Read 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) -> u16

Read 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) -> T

Returns the argument unchanged.

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

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses 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>