Skip to main content

Writer

Struct Writer 

Source
pub struct Writer<'a> { /* private fields */ }
Expand description

A cursor that writes ABI values into a byte slice.

The writer never grows its buffer. A caller that does not know how big a record will be should size the buffer with Writer::position on a dry run, or just use a buffer big enough for the largest record the ABI allows.

Implementations§

Source§

impl Writer<'_>

Source

pub fn record( &mut self, tag: Tag, version: u16, body: impl FnOnce(&mut Self) -> Result<()>, ) -> Result<()>

Writes a record, filling in its length once the body has been written.

§Errors

Returns Error::BufferFull if the buffer runs out, Error::LengthOverflow if the body is longer than a u32 can describe, or whatever the body itself returns.

Source§

impl<'a> Writer<'a>

Source

pub fn new(buf: &'a mut [u8]) -> Self

Starts writing at the beginning of buf.

Source

pub const fn position(&self) -> usize

How many bytes have been written.

Source

pub const fn remaining(&self) -> usize

How much room is left.

Source

pub fn written(&self) -> &[u8]

Everything written so far.

Source

pub fn u8(&mut self, v: u8) -> Result<()>

Writes one byte.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn u16(&mut self, v: u16) -> Result<()>

Writes a little-endian u16.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn u32(&mut self, v: u32) -> Result<()>

Writes a little-endian u32.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn u64(&mut self, v: u64) -> Result<()>

Writes a little-endian u64.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn raw(&mut self, v: &[u8]) -> Result<()>

Writes bytes with no length prefix and no padding.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn align(&mut self) -> Result<()>

Writes zeroes up to the next alignment boundary.

§Errors

Returns Error::BufferFull if there is no room.

Source

pub fn var_bytes(&mut self, v: &[u8]) -> Result<()>

Writes a length-prefixed byte string and pads it out.

§Errors

Returns Error::BufferFull if there is no room, or Error::LengthOverflow if the slice is longer than a u32 can describe.

Source

pub fn var_str(&mut self, v: &str) -> Result<()>

Writes a length-prefixed string.

§Errors

Returns the same errors as Writer::var_bytes.

Source

pub fn patch_u32(&mut self, at: usize, v: u32) -> Result<()>

Overwrites a u32 that was already written, at absolute offset at.

This exists so a record can reserve its length field, write its payload, and then fill the length in once it is known.

§Errors

Returns Error::BufferFull if at is not inside what has been written.

Trait Implementations§

Source§

impl<'a> Debug for Writer<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Writer<'a>

§

impl<'a> Freeze for Writer<'a>

§

impl<'a> RefUnwindSafe for Writer<'a>

§

impl<'a> Send for Writer<'a>

§

impl<'a> Sync for Writer<'a>

§

impl<'a> Unpin for Writer<'a>

§

impl<'a> UnsafeUnpin for Writer<'a>

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.