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>
impl<T: ZByteWriterTrait> ZWriter<T>
Sourcepub fn write(&mut self, buf: &[u8]) -> Result<usize, ZByteIoError>
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 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<(), ZByteIoError>
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 bytestreamErr(&static str)
: In case all the bytes could not be written to the stream
Sourcepub fn new(data: T) -> ZWriter<T>
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
Sourcepub fn write_u8_err(&mut self, byte: u8) -> Result<(), ZByteIoError>
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());
Sourcepub fn write_const_bytes<const N: usize>(
&mut self,
byte: &[u8; N],
) -> Result<(), ZByteIoError>
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.
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
Sourcepub fn bytes_written(&self) -> usize
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
Sourcepub fn reserve(&mut self, additional: usize) -> Result<(), ZByteIoError>
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§impl<T: ZByteWriterTrait> ZWriter<T>
impl<T: ZByteWriterTrait> ZWriter<T>
Sourcepub fn write_u64_be_err(&mut self, byte: u64) -> Result<(), ZByteIoError>
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.
Sourcepub fn write_u64_le_err(&mut self, byte: u64) -> Result<(), ZByteIoError>
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.
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.
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.
Source§impl<T: ZByteWriterTrait> ZWriter<T>
impl<T: ZByteWriterTrait> ZWriter<T>
Sourcepub fn write_u32_be_err(&mut self, byte: u32) -> Result<(), ZByteIoError>
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.
Sourcepub fn write_u32_le_err(&mut self, byte: u32) -> Result<(), ZByteIoError>
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.
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.
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.
Source§impl<T: ZByteWriterTrait> ZWriter<T>
impl<T: ZByteWriterTrait> ZWriter<T>
Sourcepub fn write_u16_be_err(&mut self, byte: u16) -> Result<(), ZByteIoError>
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.
Sourcepub fn write_u16_le_err(&mut self, byte: u16) -> Result<(), ZByteIoError>
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.
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.
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.