pub struct DgramBuffer { /* private fields */ }Expand description
A simple buffer for working with datagrams.
This is a thin wrapper around a Vec<u8> but it maintains an offset / read
position into the vector to the actual start of the datagram. This enables
users to preallocate headroom in front of the actual datagram to prepend
additional headers, or to advance the cursor to consume such prefixes.
Implementations§
Source§impl DgramBuffer
impl DgramBuffer
Sourcepub fn from_slice(data: &[u8]) -> Self
pub fn from_slice(data: &[u8]) -> Self
Creates a DgramBuffer by copying data; the read cursor starts at
the beginning.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty DgramBuffer pre-allocated for at least capacity
bytes.
Sourcepub fn with_capacity_and_headroom(capacity: usize, headroom: usize) -> Self
pub fn with_capacity_and_headroom(capacity: usize, headroom: usize) -> Self
Creates a DgramBuffer pre-allocated for capacity bytes with
headroom zero bytes reserved at the front for later prefix insertion
via try_add_prefix. The read cursor is positioned after the
headroom. Panics if headroom > capacity.
Sourcepub fn from_vec_with_headroom(v: Vec<u8>, headroom: usize) -> Self
pub fn from_vec_with_headroom(v: Vec<u8>, headroom: usize) -> Self
Wraps an existing Vec<u8>, treating the first headroom bytes as
reserved prefix space. The read cursor is positioned after the
headroom. Panics if headroom > v.len().
Sourcepub fn truncate(&mut self, count: usize)
pub fn truncate(&mut self, count: usize)
Truncates the readable portion to count bytes, discarding any data
beyond that point. If count is greater or equal to the buffer’s current
length, this has no effect.
Sourcepub fn advance(&mut self, count: usize)
pub fn advance(&mut self, count: usize)
Advances the cursor by count bytes; panics if count exceeds
the number of readable bytes.
Sourcepub fn try_add_prefix(&mut self, prefix: &[u8]) -> Result<(), ()>
pub fn try_add_prefix(&mut self, prefix: &[u8]) -> Result<(), ()>
Prepends prefix into the headroom region, sliding the read cursor
backwards. Returns Err(()) if the headroom is smaller than
prefix.len().
Sourcepub fn splice_headroom(&mut self, headroom: usize)
pub fn splice_headroom(&mut self, headroom: usize)
Ensures that at least headroom bytes of headroom are available,
potentially shifting elements and reallocating. If headroom is
less than the existing headroom, this method does nothing.
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Returns the readable bytes as a mutable slice (i.e. data[pos..]).
Sourcepub fn spare_capacity(&self) -> usize
pub fn spare_capacity(&self) -> usize
Returns the number of bytes the underlying Vec can accept before
reallocating (i.e. capacity - len).
Sourcepub fn into_parts(self) -> (Vec<u8>, usize)
pub fn into_parts(self) -> (Vec<u8>, usize)
Consumes the buffer and returns the inner Vec<u8> and the current
read position as (data, pos).
Trait Implementations§
Source§impl AsMut<[u8]> for DgramBuffer
impl AsMut<[u8]> for DgramBuffer
Source§impl AsRef<[u8]> for DgramBuffer
impl AsRef<[u8]> for DgramBuffer
Source§impl BufMut for DgramBuffer
impl BufMut for DgramBuffer
Source§fn remaining_mut(&self) -> usize
fn remaining_mut(&self) -> usize
Source§unsafe fn advance_mut(&mut self, cnt: usize)
unsafe fn advance_mut(&mut self, cnt: usize)
Source§fn chunk_mut(&mut self) -> &mut UninitSlice
fn chunk_mut(&mut self) -> &mut UninitSlice
BufMut::remaining_mut(). Note that this can be shorter than the
whole remainder of the buffer (this allows non-continuous implementation). Read more§fn has_remaining_mut(&self) -> bool
fn has_remaining_mut(&self) -> bool
self for more bytes. Read more§fn put_u16(&mut self, n: u16)
fn put_u16(&mut self, n: u16)
self in big-endian byte order. Read more§fn put_u16_le(&mut self, n: u16)
fn put_u16_le(&mut self, n: u16)
self in little-endian byte order. Read more§fn put_u16_ne(&mut self, n: u16)
fn put_u16_ne(&mut self, n: u16)
self in native-endian byte order. Read more§fn put_i16(&mut self, n: i16)
fn put_i16(&mut self, n: i16)
self in big-endian byte order. Read more§fn put_i16_le(&mut self, n: i16)
fn put_i16_le(&mut self, n: i16)
self in little-endian byte order. Read more§fn put_i16_ne(&mut self, n: i16)
fn put_i16_ne(&mut self, n: i16)
self in native-endian byte order. Read more§fn put_u32(&mut self, n: u32)
fn put_u32(&mut self, n: u32)
self in big-endian byte order. Read more§fn put_u32_le(&mut self, n: u32)
fn put_u32_le(&mut self, n: u32)
self in little-endian byte order. Read more§fn put_u32_ne(&mut self, n: u32)
fn put_u32_ne(&mut self, n: u32)
self in native-endian byte order. Read more§fn put_i32(&mut self, n: i32)
fn put_i32(&mut self, n: i32)
self in big-endian byte order. Read more§fn put_i32_le(&mut self, n: i32)
fn put_i32_le(&mut self, n: i32)
self in little-endian byte order. Read more§fn put_i32_ne(&mut self, n: i32)
fn put_i32_ne(&mut self, n: i32)
self in native-endian byte order. Read more§fn put_u64(&mut self, n: u64)
fn put_u64(&mut self, n: u64)
self in the big-endian byte order. Read more§fn put_u64_le(&mut self, n: u64)
fn put_u64_le(&mut self, n: u64)
self in little-endian byte order. Read more§fn put_u64_ne(&mut self, n: u64)
fn put_u64_ne(&mut self, n: u64)
self in native-endian byte order. Read more§fn put_i64(&mut self, n: i64)
fn put_i64(&mut self, n: i64)
self in the big-endian byte order. Read more§fn put_i64_le(&mut self, n: i64)
fn put_i64_le(&mut self, n: i64)
self in little-endian byte order. Read more§fn put_i64_ne(&mut self, n: i64)
fn put_i64_ne(&mut self, n: i64)
self in native-endian byte order. Read more§fn put_u128(&mut self, n: u128)
fn put_u128(&mut self, n: u128)
self in the big-endian byte order. Read more§fn put_u128_le(&mut self, n: u128)
fn put_u128_le(&mut self, n: u128)
self in little-endian byte order. Read more§fn put_u128_ne(&mut self, n: u128)
fn put_u128_ne(&mut self, n: u128)
self in native-endian byte order. Read more§fn put_i128(&mut self, n: i128)
fn put_i128(&mut self, n: i128)
self in the big-endian byte order. Read more§fn put_i128_le(&mut self, n: i128)
fn put_i128_le(&mut self, n: i128)
self in little-endian byte order. Read more§fn put_i128_ne(&mut self, n: i128)
fn put_i128_ne(&mut self, n: i128)
self in native-endian byte order. Read more§fn put_uint(&mut self, n: u64, nbytes: usize)
fn put_uint(&mut self, n: u64, nbytes: usize)
self in big-endian byte order. Read more§fn put_uint_le(&mut self, n: u64, nbytes: usize)
fn put_uint_le(&mut self, n: u64, nbytes: usize)
self in the little-endian byte order. Read more§fn put_uint_ne(&mut self, n: u64, nbytes: usize)
fn put_uint_ne(&mut self, n: u64, nbytes: usize)
self in the native-endian byte order. Read more§fn put_int_le(&mut self, n: i64, nbytes: usize)
fn put_int_le(&mut self, n: i64, nbytes: usize)
§fn put_int_ne(&mut self, n: i64, nbytes: usize)
fn put_int_ne(&mut self, n: i64, nbytes: usize)
§fn put_f32(&mut self, n: f32)
fn put_f32(&mut self, n: f32)
self in big-endian byte order. Read more§fn put_f32_le(&mut self, n: f32)
fn put_f32_le(&mut self, n: f32)
self in little-endian byte order. Read more§fn put_f32_ne(&mut self, n: f32)
fn put_f32_ne(&mut self, n: f32)
self in native-endian byte order. Read more§fn put_f64(&mut self, n: f64)
fn put_f64(&mut self, n: f64)
self in big-endian byte order. Read more§fn put_f64_le(&mut self, n: f64)
fn put_f64_le(&mut self, n: f64)
self in little-endian byte order. Read more§fn put_f64_ne(&mut self, n: f64)
fn put_f64_ne(&mut self, n: f64)
self in native-endian byte order. Read moreSource§impl Clone for DgramBuffer
impl Clone for DgramBuffer
Source§fn clone(&self) -> DgramBuffer
fn clone(&self) -> DgramBuffer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more