Skip to main content

WriteBuf

Struct WriteBuf 

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

Cursor over a mutable byte slice with a tracked write position.

§Example

use wire_codec::WriteBuf;

let mut storage = [0u8; 4];
let mut buf = WriteBuf::new(&mut storage);
buf.write_u16_be(0xDEAD).unwrap();
buf.write_u16_be(0xBEEF).unwrap();
assert_eq!(buf.position(), 4);
assert_eq!(&storage, &[0xDE, 0xAD, 0xBE, 0xEF]);

While a WriteBuf is alive it mutably borrows the storage, so read the encoded bytes back through WriteBuf::written (or capture WriteBuf::position in a local first) rather than indexing the storage directly:

use wire_codec::WriteBuf;

let mut storage = [0u8; 8];
let mut buf = WriteBuf::new(&mut storage);
buf.write_u8(1).unwrap();
let bytes: &[u8] = buf.written();
assert_eq!(bytes, &[1]);

Implementations§

Source§

impl<'a> WriteBuf<'a>

Source

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

Wrap bytes in a new cursor positioned at offset zero.

Source

pub fn capacity(&self) -> usize

Total capacity of the backing slice.

Source

pub fn position(&self) -> usize

Current write offset within the backing slice.

Source

pub fn remaining(&self) -> usize

Number of bytes still available for writing.

Source

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

Borrow the prefix that has been written so far.

Source

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

Append src to the buffer and advance the cursor.

§Errors

Returns Error::BufferFull if fewer than src.len() bytes remain.

Source

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

Write a single byte.

§Errors

Returns Error::BufferFull if no capacity remains.

Source

pub fn write_i8(&mut self, value: i8) -> Result<()>

Write a single i8 (two’s complement).

§Errors

Returns Error::BufferFull if no capacity remains.

Source

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

Write a big-endian u16.

§Errors

Returns Error::BufferFull if fewer than 2 bytes remain.

Source

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

Write a little-endian u16.

§Errors

Returns Error::BufferFull if fewer than 2 bytes remain.

Source

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

Write a big-endian u32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

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

Write a little-endian u32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

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

Write a big-endian u64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

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

Write a little-endian u64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

pub fn write_u128_be(&mut self, value: u128) -> Result<()>

Write a big-endian u128.

§Errors

Returns Error::BufferFull if fewer than 16 bytes remain.

Source

pub fn write_u128_le(&mut self, value: u128) -> Result<()>

Write a little-endian u128.

§Errors

Returns Error::BufferFull if fewer than 16 bytes remain.

Source

pub fn write_i16_be(&mut self, value: i16) -> Result<()>

Write a big-endian i16.

§Errors

Returns Error::BufferFull if fewer than 2 bytes remain.

Source

pub fn write_i16_le(&mut self, value: i16) -> Result<()>

Write a little-endian i16.

§Errors

Returns Error::BufferFull if fewer than 2 bytes remain.

Source

pub fn write_i32_be(&mut self, value: i32) -> Result<()>

Write a big-endian i32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

pub fn write_i32_le(&mut self, value: i32) -> Result<()>

Write a little-endian i32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

pub fn write_i64_be(&mut self, value: i64) -> Result<()>

Write a big-endian i64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

pub fn write_i64_le(&mut self, value: i64) -> Result<()>

Write a little-endian i64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

pub fn write_i128_be(&mut self, value: i128) -> Result<()>

Write a big-endian i128.

§Errors

Returns Error::BufferFull if fewer than 16 bytes remain.

Source

pub fn write_i128_le(&mut self, value: i128) -> Result<()>

Write a little-endian i128.

§Errors

Returns Error::BufferFull if fewer than 16 bytes remain.

Source

pub fn write_f32_be(&mut self, value: f32) -> Result<()>

Write a big-endian IEEE 754 f32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

pub fn write_f32_le(&mut self, value: f32) -> Result<()>

Write a little-endian IEEE 754 f32.

§Errors

Returns Error::BufferFull if fewer than 4 bytes remain.

Source

pub fn write_f64_be(&mut self, value: f64) -> Result<()>

Write a big-endian IEEE 754 f64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

pub fn write_f64_le(&mut self, value: f64) -> Result<()>

Write a little-endian IEEE 754 f64.

§Errors

Returns Error::BufferFull if fewer than 8 bytes remain.

Source

pub fn into_written(self) -> &'a mut [u8] ⓘ

Consume the cursor and return the prefix that has been written.

Trait Implementations§

Source§

impl<'a> Debug for WriteBuf<'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 WriteBuf<'a>

§

impl<'a> Forget for WriteBuf<'a>

§

impl<'a> Freeze for WriteBuf<'a>

§

impl<'a> Move for WriteBuf<'a>

§

impl<'a> RefUnwindSafe for WriteBuf<'a>

§

impl<'a> Send for WriteBuf<'a>

§

impl<'a> Sync for WriteBuf<'a>

§

impl<'a> Unpin for WriteBuf<'a>

§

impl<'a> UnsafeUnpin for WriteBuf<'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.