ZWriter

Struct ZWriter 

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

Encapsulates a simple Byte writer with support for Endian aware writes

Implementations§

Source§

impl<T: ZByteWriterTrait> ZWriter<T>

Source

pub 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

Source

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

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

pub 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());
Source

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

Source

pub 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

Source

pub 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

Source

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

pub 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

Source

pub fn inner_ref(&self) -> &T

Return an immutable reference to the inner sink

Source

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

Return a mutable reference to the inner sink

Source§

impl<T: ZByteWriterTrait> ZWriter<T>

Source

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

Source

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

Source

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

Source

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

Source§

impl<T: ZByteWriterTrait> ZWriter<T>

Source

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

Source

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

Source

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

Source

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

Source§

impl<T: ZByteWriterTrait> ZWriter<T>

Source

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

Source

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

Source

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

Source

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

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> UnwindSafe for ZWriter<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.