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<'_>
impl Writer<'_>
Sourcepub fn record(
&mut self,
tag: Tag,
version: u16,
body: impl FnOnce(&mut Self) -> Result<()>,
) -> Result<()>
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>
impl<'a> Writer<'a>
Sourcepub fn raw(&mut self, v: &[u8]) -> Result<()>
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.
Sourcepub fn align(&mut self) -> Result<()>
pub fn align(&mut self) -> Result<()>
Writes zeroes up to the next alignment boundary.
§Errors
Returns Error::BufferFull if there is no room.
Sourcepub fn var_bytes(&mut self, v: &[u8]) -> Result<()>
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.
Sourcepub fn patch_u32(&mut self, at: usize, v: u32) -> Result<()>
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.