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.

Trait Implementations§

Source§

impl<T> Read for ZReader<T>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more

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.