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]);

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_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 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> Freeze 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>

§

impl<'a> !UnwindSafe 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 = 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.