Struct gospel::write::Writer

source ·
pub struct Writer { /* private fields */ }
Expand description

An incremental writer to a Vec<u8>, with support for delayed labels.

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 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 label(&mut self, label: Label)

Places a label at the current position, so it can be referenced with delay.

source

pub fn here(&mut self) -> Label

Creates and places a label at the current position.

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 append(&mut self, other: Writer)

Concatenates two Writers, including labels.

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

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

source§

impl Writer

Functions for writing primitives to the stream. The underlying functions are hidden from the docs for brevity.

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.

source

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

Write a primitive from the input.

source

pub fn delayN(&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 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§

impl Writer

source

pub fn u8_le(&mut self, val: u8)

source

pub fn u16_le(&mut self, val: u16)

source

pub fn u32_le(&mut self, val: u32)

source

pub fn u64_le(&mut self, val: u64)

source

pub fn u128_le(&mut self, val: u128)

source

pub fn i8_le(&mut self, val: i8)

source

pub fn i16_le(&mut self, val: i16)

source

pub fn i32_le(&mut self, val: i32)

source

pub fn i64_le(&mut self, val: i64)

source

pub fn i128_le(&mut self, val: i128)

source

pub fn f32_le(&mut self, val: f32)

source

pub fn f64_le(&mut self, val: f64)

source

pub fn delay8_le(&mut self, label: Label)

source

pub fn delay16_le(&mut self, label: Label)

source

pub fn delay32_le(&mut self, label: Label)

source

pub fn delay64_le(&mut self, label: Label)

source

pub fn delay128_le(&mut self, label: Label)

source

pub fn ptr8_le(&mut self) -> Self

source

pub fn ptr16_le(&mut self) -> Self

source

pub fn ptr32_le(&mut self) -> Self

source

pub fn ptr64_le(&mut self) -> Self

source

pub fn ptr128_le(&mut self) -> Self

source§

impl Writer

source

pub fn u8_be(&mut self, val: u8)

source

pub fn u16_be(&mut self, val: u16)

source

pub fn u32_be(&mut self, val: u32)

source

pub fn u64_be(&mut self, val: u64)

source

pub fn u128_be(&mut self, val: u128)

source

pub fn i8_be(&mut self, val: i8)

source

pub fn i16_be(&mut self, val: i16)

source

pub fn i32_be(&mut self, val: i32)

source

pub fn i64_be(&mut self, val: i64)

source

pub fn i128_be(&mut self, val: i128)

source

pub fn f32_be(&mut self, val: f32)

source

pub fn f64_be(&mut self, val: f64)

source

pub fn delay8_be(&mut self, label: Label)

source

pub fn delay16_be(&mut self, label: Label)

source

pub fn delay32_be(&mut self, label: Label)

source

pub fn delay64_be(&mut self, label: Label)

source

pub fn delay128_be(&mut self, label: Label)

source

pub fn ptr8_be(&mut self) -> Self

source

pub fn ptr16_be(&mut self) -> Self

source

pub fn ptr32_be(&mut self) -> Self

source

pub fn ptr64_be(&mut self) -> Self

source

pub fn ptr128_be(&mut self) -> Self

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 !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.