pub struct Writer { /* private fields */ }Expand description
An incremental writer to a Vec<u8>, with support for delayed labels.
One of the main points of interest is u32_le and the like, which read the
relevant primitives from the slice. In these docs, they are abbreviated to a single function
for simplicity.
The other point of interest is the place and label
functions. These allow writing delayed data that cannot be known before other data has been written;
in particular sizes and offsets.
Supported primitives are u8..=u128, i8..=i128, f32, f64.
The functions are suffixed with either _le or _be, for endianness. To use unsuffixed
versions, import either the Le or Be trait.
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn finish(self) -> Result<Vec<u8>>
pub fn finish(self) -> Result<Vec<u8>>
Finalizes all delayed labels and returns the resulting Vec<u8>.
Returns any error returned by the delays, which is usually if a label is not defined or is too large to fit in its slot.
Sourcepub fn reserve(&mut self, size: usize)
pub fn reserve(&mut self, size: usize)
Calls Vec::reserve on the underlying Vec.
Sourcepub fn array<const N: usize>(&mut self, data: [u8; N])
pub fn array<const N: usize>(&mut self, data: [u8; N])
Writes some data.
This function is redundant, and exists only for symmetry.
Sourcepub fn place(&mut self, label: Label)
pub fn place(&mut self, label: Label)
Places a label at the current position, so it can be referenced with labelN.
Placing the same label twice results in a panic.
Sourcepub fn labelN(&mut self, l: Label)
pub fn labelN(&mut self, l: Label)
Write the address of a label.
finish will throw an error if the resulting address does not fit in the type.
Sourcepub fn diffN(&mut self, start: Label, end: Label)
pub fn diffN(&mut self, start: Label, end: Label)
Write the difference between two labels.
finish will throw an error if the resulting value does not fit in the type.
Sourcepub fn delay<const N: usize, F>(&mut self, cb: F)
pub fn delay<const N: usize, F>(&mut self, cb: F)
Writes some bytes to be filled in later.
The given closure is called with a function that allows looking up labels. Other kinds of state are not currently officially allowed.