Struct zune_core::bytestream::ZByteWriter
source · 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>
impl<'a> ZByteWriter<'a>
sourcepub fn write(&mut self, buf: &[u8]) -> Result<usize, &'static str>
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 thanbuf.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
sourcepub fn write_all(&mut self, buf: &[u8]) -> Result<(), &'static str>
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 bytestreamErr(&static str)
: In case all the bytes could not be written to the stream
sourcepub fn new(data: &'a mut [u8]) -> ZByteWriter<'a>
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
sourcepub const fn bytes_left(&self) -> usize
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
sourcepub const fn position(&self) -> usize
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);
sourcepub fn write_u8_err(&mut self, byte: u8) -> Result<(), &'static str>
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());
sourcepub fn write_u8(&mut self, byte: u8)
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
sourcepub const fn has(&self, bytes: usize) -> bool
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));
sourcepub const fn eof(&self) -> bool
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
sourcepub fn rewind(&mut self, by: usize)
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);
sourcepub fn skip(&mut self, by: usize)
pub fn skip(&mut self, by: usize)
Move the internal cursor forward some bytes
This saturates at maximum value of usize in your platform.
sourcepub fn peek_at(
&'a self,
position: usize,
num_bytes: usize
) -> Result<&'a [u8], &'static str>
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.
sourcepub fn set_position(&mut self, position: usize)
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>
impl<'a> ZByteWriter<'a>
sourcepub fn write_u64_be_err(&mut self, byte: u64) -> Result<(), &'static str>
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.
sourcepub fn write_u64_le_err(&mut self, byte: u64) -> Result<(), &'static str>
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.
sourcepub fn write_u64_be(&mut self, byte: u64)
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
sourcepub fn write_u64_le(&mut self, byte: u64)
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>
impl<'a> ZByteWriter<'a>
sourcepub fn write_u32_be_err(&mut self, byte: u32) -> Result<(), &'static str>
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.
sourcepub fn write_u32_le_err(&mut self, byte: u32) -> Result<(), &'static str>
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.
sourcepub fn write_u32_be(&mut self, byte: u32)
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
sourcepub fn write_u32_le(&mut self, byte: u32)
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>
impl<'a> ZByteWriter<'a>
sourcepub fn write_u16_be_err(&mut self, byte: u16) -> Result<(), &'static str>
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.
sourcepub fn write_u16_le_err(&mut self, byte: u16) -> Result<(), &'static str>
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.
sourcepub fn write_u16_be(&mut self, byte: u16)
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
sourcepub fn write_u16_le(&mut self, byte: u16)
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