pub trait MutableBuffer {
    // Required methods
    fn capacity(&self) -> usize;
    fn cursor(&self) -> usize;
    unsafe fn advance_cursor(&mut self, n: usize);
    unsafe fn put_slice_at(&mut self, src: &[u8], offset: usize);
    fn has_remaining(&self, num_bytes: usize) -> bool;

    // Provided methods
    fn put_slot(&mut self, width: usize) -> Result<WriteSlot, EncodingError> { ... }
    fn fill_slot(&mut self, slot: WriteSlot, src: &[u8]) { ... }
    fn put_slice(&mut self, src: &[u8]) -> Result<(), EncodingError> { ... }
    fn put_u64_le(&mut self, n: u64) -> Result<(), EncodingError> { ... }
    fn put_i64_le(&mut self, n: i64) -> Result<(), EncodingError> { ... }
    fn put_f64(&mut self, n: f64) -> Result<(), EncodingError> { ... }
}
Expand description

Analogous to bytes::BufMut with some additions to be able to write at specific offsets.

Required Methods§

source

fn capacity(&self) -> usize

Returns the number of total bytes this container can store. Shared memory buffers are not expected to resize and this should return the same value during the entire lifetime of the buffer.

source

fn cursor(&self) -> usize

Returns the current position into which the next write is expected.

source

unsafe fn advance_cursor(&mut self, n: usize)

Advance the write cursor by n bytes.

§Safety

This is marked unsafe because a malformed caller may cause a subsequent out-of-bounds write.

source

unsafe fn put_slice_at(&mut self, src: &[u8], offset: usize)

Write a copy of the src slice into the buffer, starting at the provided offset.

§Safety

Implementations are not expected to bounds check the requested copy, although they may do so and still satisfy this trait’s contract.

source

fn has_remaining(&self, num_bytes: usize) -> bool

Returns whether the buffer has sufficient remaining capacity to write an incoming value.

Provided Methods§

source

fn put_slot(&mut self, width: usize) -> Result<WriteSlot, EncodingError>

Advances the write cursor without immediately writing any bytes to the buffer. The returned struct offers the ability to later write to the provided portion of the buffer.

source

fn fill_slot(&mut self, slot: WriteSlot, src: &[u8])

Write src into the provided slot that was created at a previous point in the stream.

source

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

Writes the contents of the src buffer to self, starting at self.cursor() and advancing the cursor by src.len().

§Panics

This function panics if there is not enough remaining capacity in self.

source

fn put_u64_le(&mut self, n: u64) -> Result<(), EncodingError>

Writes an unsigned 64 bit integer to self in little-endian byte order.

Advances the cursor by 8 bytes.

§Examples
use bytes::BufMut;

let mut buf = vec![0; 8];
buf.put_u64_le_at(0x0102030405060708, 0);
assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

This function panics if there is not enough remaining capacity in self.

source

fn put_i64_le(&mut self, n: i64) -> Result<(), EncodingError>

Writes a signed 64 bit integer to self in little-endian byte order.

The cursor position is advanced by 8.

§Examples
use bytes::BufMut;

let mut buf = vec![0; 8];
buf.put_i64_le_at(0x0102030405060708, 0);
assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

This function panics if there is not enough remaining capacity in self.

source

fn put_f64(&mut self, n: f64) -> Result<(), EncodingError>

Writes a double-precision IEEE 754 floating point number to self.

The cursor position is advanced by 8.

§Examples
use bytes::BufMut;

let mut buf = vec![];
buf.put_i64_le(0x0102030405060708);
assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01");
§Panics

This function panics if there is not enough remaining capacity in self.

Implementations on Foreign Types§

source§

impl MutableBuffer for Cursor<&mut [u8]>

source§

fn has_remaining(&self, num_bytes: usize) -> bool

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

source§

impl MutableBuffer for Cursor<Vec<u8>>

source§

fn has_remaining(&self, num_bytes: usize) -> bool

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

source§

impl MutableBuffer for Cursor<ResizableBuffer>

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

fn has_remaining(&self, _num_bytes: usize) -> bool

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

source§

impl<'a, T: MutableBuffer + ?Sized> MutableBuffer for &'a mut T

source§

fn has_remaining(&self, num_bytes: usize) -> bool

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

source§

impl<T: MutableBuffer + ?Sized> MutableBuffer for Box<T>

source§

fn has_remaining(&self, num_bytes: usize) -> bool

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

source§

impl<const N: usize> MutableBuffer for Cursor<[u8; N]>

source§

fn has_remaining(&self, num_bytes: usize) -> bool

source§

fn capacity(&self) -> usize

source§

fn cursor(&self) -> usize

source§

unsafe fn advance_cursor(&mut self, n: usize)

source§

unsafe fn put_slice_at(&mut self, to_put: &[u8], offset: usize)

Implementors§