Writer

Struct Writer 

Source
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

Source

pub fn new() -> Self

Constructs a new Writer.

Source

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.

Source

pub fn len(&self) -> usize

Returns the number of bytes written so far, including delayed ones.

Source

pub fn is_empty(&self) -> bool

Returns whether any bytes have been written so far.

Source

pub fn reserve(&mut self, size: usize)

Calls Vec::reserve on the underlying Vec.

Source

pub fn capacity(&self) -> usize

Returns the capacity of the underlying Vec.

Source

pub fn T(&mut self, val: T)

Write a primitive from the input.

Source

pub fn slice(&mut self, data: &[u8])

Writes some data.

Source

pub fn array<const N: usize>(&mut self, data: [u8; N])

Writes some data.

This function is redundant, and exists only for symmetry.

Source

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.

Source

pub fn here(&mut self) -> Label

Creates and places a label at the current position.

Source

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.

Source

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.

Source

pub fn delay<const N: usize, F>(&mut self, cb: F)
where F: FnOnce(&DelayContext<'_>) -> Result<[u8; N], Box<dyn Error + Send + Sync>> + 'static,

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.

Source

pub fn ptrN(&mut self) -> Writer

Creates a new Writer and delays a pointer to it.

This is a shorthand for the common pattern of { let mut g = Writer::new(); f.delayN(g.here()); g }.

Source

pub fn align(&mut self, size: usize)

Writes null bytes until the length is a multiple of size.

Source

pub fn append(&mut self, other: Writer)

Concatenates two Writers, including labels.

Trait Implementations§

Source§

impl Default for Writer

Source§

fn default() -> Writer

Returns the “default value” for a type. Read more
Source§

impl Be for Writer

Source§

impl Le for Writer

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl !RefUnwindSafe for Writer

§

impl !Send for Writer

§

impl !Sync for Writer

§

impl Unpin for Writer

§

impl !UnwindSafe for Writer

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.