pub struct ZByteWriter<'a> { /* private fields */ }
Expand description

Encapsulates a simple Byte writer with support for Endian aware writes

Implementations§

source§

impl<'a> ZByteWriter<'a>

source

pub fn write(&mut self, buf: &[u8]) -> Result<usize, &'static str>

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<(), &'static str>

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: &'a mut [u8]) -> ZByteWriter<'a>

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 const fn bytes_left(&self) -> usize

Return number of unwritten bytes in this stream

Example
use zune_core::bytestream::ZByteWriter;
let mut storage = [0;10];

let writer = ZByteWriter::new(&mut storage);
assert_eq!(writer.bytes_left(),10); // no bytes were written
source

pub const fn position(&self) -> usize

Return the number of bytes the writer has written

use zune_core::bytestream::ZByteWriter;
let mut stream = ZByteWriter::new(&mut []);
assert_eq!(stream.position(),0);
source

pub fn write_u8_err(&mut self, byte: u8) -> Result<(), &'static str>

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

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

No space

use zune_core::bytestream::ZByteWriter;
let mut stream = ZByteWriter::new(&mut []);
assert!(stream.write_u8_err(32).is_err());
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

Should be combined with has

source

pub const fn has(&self, bytes: usize) -> bool

Check if the byte writer can support the following write

Example
use zune_core::bytestream::ZByteWriter;
let mut data = [0;10];
let mut stream = ZByteWriter::new(&mut data);
assert!(stream.has(5));
assert!(!stream.has(100));
source

pub const fn len(&self) -> usize

Get length of the underlying buffer.

source

pub const fn is_empty(&self) -> bool

Return true if the underlying buffer stream is empty

source

pub const fn eof(&self) -> bool

Return true whether or not we read to the end of the buffer and have no more bytes left.

If this is true, all non error variants will silently discard the byte and all error variants will return an error on writing a byte if any write occurs

source

pub fn rewind(&mut self, by: usize)

Rewind the position of the internal cursor back by by bytes

The position saturates at zero

Example
use zune_core::bytestream::ZByteWriter;
let bytes = &mut [1,2,4];
let mut stream = ZByteWriter::new(bytes);
stream.write_u16_be(23);
// now internal cursor is at position 2.
// lets rewind it
stream.rewind(usize::MAX);
assert_eq!(stream.position(),0);
source

pub fn skip(&mut self, by: usize)

Move the internal cursor forward some bytes

This saturates at maximum value of usize in your platform.

source

pub fn peek_at( &'a self, position: usize, num_bytes: usize ) -> Result<&'a [u8], &'static str>

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 set_position(&mut self, position: usize)

Set position for the internal cursor

Further calls to write bytes will proceed from the position set

source§

impl<'a> ZByteWriter<'a>

source

pub fn write_u64_be_err(&mut self, byte: u64) -> Result<(), &'static str>

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<(), &'static str>

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. Should be combined with the has method to ensure a write succeeds

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. Should be combined with the has method to ensure a write succeeds

source§

impl<'a> ZByteWriter<'a>

source

pub fn write_u32_be_err(&mut self, byte: u32) -> Result<(), &'static str>

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<(), &'static str>

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. Should be combined with the has method to ensure a write succeeds

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. Should be combined with the has method to ensure a write succeeds

source§

impl<'a> ZByteWriter<'a>

source

pub fn write_u16_be_err(&mut self, byte: u16) -> Result<(), &'static str>

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<(), &'static str>

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. Should be combined with the has method to ensure a write succeeds

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. Should be combined with the has method to ensure a write succeeds

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for ZByteWriter<'a>

§

impl<'a> Send for ZByteWriter<'a>

§

impl<'a> Sync for ZByteWriter<'a>

§

impl<'a> Unpin for ZByteWriter<'a>

§

impl<'a> !UnwindSafe for ZByteWriter<'a>

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

§

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

§

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.