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§
Sourcefn capacity(&self) -> usize
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.
Sourceunsafe fn advance_cursor(&mut self, n: usize)
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.
Sourceunsafe fn put_slice_at(&mut self, src: &[u8], offset: usize)
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.
Sourcefn has_remaining(&self, num_bytes: usize) -> bool
fn has_remaining(&self, num_bytes: usize) -> bool
Returns whether the buffer has sufficient remaining capacity to write an incoming value.
Provided Methods§
Sourcefn put_slot(&mut self, width: usize) -> Result<WriteSlot, EncodingError>
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.
Sourcefn fill_slot(&mut self, slot: WriteSlot, src: &[u8])
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.
Sourcefn put_slice(&mut self, src: &[u8]) -> Result<(), EncodingError>
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
.
Sourcefn put_u64_le(&mut self, n: u64) -> Result<(), EncodingError>
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
.
Sourcefn put_i64_le(&mut self, n: i64) -> Result<(), EncodingError>
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
.
Sourcefn put_f64(&mut self, n: f64) -> Result<(), EncodingError>
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
.