Struct ZWriter

pub struct ZWriter<T: ZByteWriterTrait> { /* private fields */ }

Encapsulates a simple Byte writer with support for Endian aware writes

Implementations

impl<T: ZByteWriterTrait> ZWriter<T>

fn write_u32_be_err(&mut self, byte: u32) -> Result<(), ZByteIoError>

Write u32 as a big endian integer Returning an error if the underlying buffer cannot support a u32 write.

fn write_u32_le_err(&mut self, byte: u32) -> Result<(), ZByteIoError>

Write u32 as a little endian integer Returning an error if the underlying buffer cannot support a u32 write.

fn write_u32_be(&mut self, byte: u32)

Write u32 as a big endian integer Or don't write anything if the reader cannot support a u32 write.

fn write_u32_le(&mut self, byte: u32)

Write u32 as a little endian integer Or don't write anything if the reader cannot support a u32 write.

impl<T: ZByteWriterTrait> ZWriter<T>

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

Write bytes from the buf into the bytestream and return how many bytes were written

Arguments

  • buf: The bytes to be written to the bytestream

Returns

  • Ok(usize) - Number of bytes written This number may be less than buf.len() if the length of the buffer is greater than the internal bytestream length

If you want to be sure that all bytes were written, see write_all

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

Write all bytes from buf into the bytestream and return and panic if not all bytes were written to the bytestream

Arguments

  • buf: The bytes to be written into the bytestream

Returns

  • Ok(()): Indicates all bytes were written into the bytestream
  • Err(&static str): In case all the bytes could not be written to the stream
fn new(data: T) -> ZWriter<T>

Create a new bytestream writer Bytes are written from the start to the end and not assumptions are made of the nature of the underlying stream

Arguments

fn write_u8_err(&mut self, byte: u8) -> Result<(), ZByteIoError>

Write a single byte into the bytestream or error out if there is not enough space

Example

use zune_core::bytestream::ZWriter;
let mut buf = [0;10];
let mut stream  =  ZWriter::new(&mut buf[..]);
assert!(stream.write_u8_err(34).is_ok());

No space

use zune_core::bytestream::ZWriter;
let mut no_space = [];
let mut stream = ZWriter::new(&mut no_space[..]);
assert!(stream.write_u8_err(32).is_err());
fn write_const_bytes<const N: usize>(&mut self, byte: &[u8; N]) -> Result<(), ZByteIoError>

Write a fixed compile time known number of bytes to the sink

This is provided since some implementations can optimize such writes by eliminating some redundant code.

fn write_u8(&mut self, byte: u8)

Write a single byte in the stream or don't write anything if the buffer is full and cannot support the byte read

fn bytes_written(&self) -> usize

Return the number of bytes written by this encoder

The encoder keeps information of how many bytes were written and this method returns that value.

Returns

Number of bytes written

fn reserve(&mut self, additional: usize) -> Result<(), ZByteIoError>

Reserve some additional space to write.

Some sinks like Vec<u8> allow reallocation and to prevent too much reallocation one can use this to reserve additional space to encode

Example

use zune_core::bytestream::ZWriter;
let space_needed = 10; // Assume the image will fit into 10 bytes
let mut output = Vec::new();
let mut sink = ZWriter::new(&mut output);
// now reserve some space
sink.reserve(space_needed).unwrap();
// at this point, we can assume that ZWriter allocated space for output
fn inner(self) -> T

Consume the writer and return the inner sink we were writing to.

After this, the writer can no longer be used

fn inner_ref(&self) -> &T

Return an immutable reference to the inner sink

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

Return a mutable reference to the inner sink

impl<T: ZByteWriterTrait> ZWriter<T>

fn write_u16_be_err(&mut self, byte: u16) -> Result<(), ZByteIoError>

Write u16 as a big endian integer Returning an error if the underlying buffer cannot support a u16 write.

fn write_u16_le_err(&mut self, byte: u16) -> Result<(), ZByteIoError>

Write u16 as a little endian integer Returning an error if the underlying buffer cannot support a u16 write.

fn write_u16_be(&mut self, byte: u16)

Write u16 as a big endian integer Or don't write anything if the reader cannot support a u16 write.

fn write_u16_le(&mut self, byte: u16)

Write u16 as a little endian integer Or don't write anything if the reader cannot support a u16 write.

impl<T: ZByteWriterTrait> ZWriter<T>

fn write_u64_be_err(&mut self, byte: u64) -> Result<(), ZByteIoError>

Write u64 as a big endian integer Returning an error if the underlying buffer cannot support a u64 write.

fn write_u64_le_err(&mut self, byte: u64) -> Result<(), ZByteIoError>

Write u64 as a little endian integer Returning an error if the underlying buffer cannot support a u64 write.

fn write_u64_be(&mut self, byte: u64)

Write u64 as a big endian integer Or don't write anything if the reader cannot support a u64 write.

fn write_u64_le(&mut self, byte: u64)

Write u64 as a little endian integer Or don't write anything if the reader cannot support a u64 write.

Auto Trait Implementations

impl<T> Freeze for ZWriter<T> where T: Freeze,

impl<T> RefUnwindSafe for ZWriter<T> where T: RefUnwindSafe,

impl<T> Send for ZWriter<T> where T: Send,

impl<T> Sync for ZWriter<T> where T: Sync,

impl<T> Unpin for ZWriter<T> where T: Unpin,

impl<T> UnsafeUnpin for ZWriter<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for ZWriter<T> where T: UnwindSafe,

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> From<T> for ZWriter<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for ZWriter<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 ZWriter<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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