pub struct Mbuf { /* private fields */ }Expand description
A single chunk of a connection’s I/O buffer chain.
An Mbuf exposes three regions:
[0, pos)- already-consumed bytes (drained by the parser).[pos, last)- readable bytes (parser input or pre-flight write).[last, end)- writable bytes (target ofrecv).
The slice [end, end_extra) of length MBUF_ESIZE is reserved
for crypto MAC and overflow guards. It is not visible through the
normal write API.
Implementations§
Source§impl Mbuf
impl Mbuf
Sourcepub fn with_chunk_size(chunk_size: usize) -> Self
pub fn with_chunk_size(chunk_size: usize) -> Self
Allocate a fresh chunk of the given total size. Used by the pool to fill its free list and by callers that need a one-off non-pooled buffer (the Stage 13 entropy path is a planned consumer).
chunk_size must be in [MBUF_MIN_SIZE, MBUF_MAX_SIZE].
§Examples
use dynomite::io::mbuf::{Mbuf, MBUF_SIZE};
let buf = Mbuf::with_chunk_size(MBUF_SIZE);
assert_eq!(buf.chunk_size(), MBUF_SIZE);
assert!(buf.is_empty());Sourcepub fn chunk_size(&self) -> usize
pub fn chunk_size(&self) -> usize
Total byte length of the chunk allocation.
§Examples
use dynomite::io::mbuf::{Mbuf, MBUF_SIZE};
let buf = Mbuf::with_chunk_size(MBUF_SIZE);
assert_eq!(buf.chunk_size(), MBUF_SIZE);Sourcepub fn usable_capacity(&self) -> usize
pub fn usable_capacity(&self) -> usize
Total byte addressable region including the trailing extra
area, equal to chunk_size. Mirrors mbuf_full_data_size from
the Stage 2 plan and the underlying end_extra boundary in the
C struct.
§Examples
use dynomite::io::mbuf::{Mbuf, MBUF_SIZE};
let buf = Mbuf::with_chunk_size(MBUF_SIZE);
assert_eq!(buf.usable_capacity(), MBUF_SIZE);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Bytes currently available to read from the buffer
(last - pos). Mirrors mbuf_length.
§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
buf.recv(b"abc");
assert_eq!(buf.len(), 3);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Maximum number of bytes the buffer can ever hold (the writable
region size, equal to data_size). Mirrors
mbuf_size in the reference’s higher-level usage.
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Bytes still writable into the buffer (end - last). Mirrors
mbuf_remaining_space.
§Examples
use dynomite::io::mbuf::{Mbuf, MBUF_SIZE, MBUF_ESIZE};
let buf = Mbuf::with_chunk_size(MBUF_SIZE);
assert_eq!(buf.remaining(), MBUF_SIZE - MBUF_ESIZE);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
True when no readable bytes remain. Mirrors mbuf_empty.
§Examples
use dynomite::io::mbuf::Mbuf;
let buf = Mbuf::with_chunk_size(1024);
assert!(buf.is_empty());Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
True when the writable region is full. Mirrors mbuf_full.
§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
while buf.remaining() > 0 {
buf.recv(&[0]);
}
assert!(buf.is_full());Sourcepub fn rewind(&mut self)
pub fn rewind(&mut self)
Discard all buffered bytes and rewind both cursors to the
origin. Mirrors mbuf_rewind.
Sourcepub fn owner(&self) -> Option<OwnerId>
pub fn owner(&self) -> Option<OwnerId>
Return the optional owner connection id stamped on the buffer.
Sourcepub fn readable(&self) -> &[u8] ⓘ
pub fn readable(&self) -> &[u8] ⓘ
Borrow the readable region ([pos, last)).
§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
buf.recv(b"abc");
assert_eq!(buf.readable(), b"abc");Sourcepub fn recv(&mut self, src: &[u8]) -> usize
pub fn recv(&mut self, src: &[u8]) -> usize
Copy bytes from src into the writable region. Stops when the
buffer fills. Returns the number of bytes copied. Mirrors
mbuf_recv / mbuf_copy for the inbound direction.
§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
assert_eq!(buf.recv(b"hello"), 5);
assert_eq!(buf.readable(), b"hello");Sourcepub fn send(&mut self, dst: &mut [u8]) -> usize
pub fn send(&mut self, dst: &mut [u8]) -> usize
Drain bytes from the readable region into dst. Returns the
number of bytes copied. Mirrors mbuf_send for the outbound
direction.
§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
buf.recv(b"hello");
let mut out = [0u8; 8];
let n = buf.send(&mut out);
assert_eq!(n, 5);
assert_eq!(&out[..n], b"hello");Sourcepub fn copy_from_slice(&mut self, src: &[u8])
pub fn copy_from_slice(&mut self, src: &[u8])
Sourcepub fn split_off(&mut self, at: usize, pool: &MbufPool) -> Option<Mbuf>
pub fn split_off(&mut self, at: usize, pool: &MbufPool) -> Option<Mbuf>
Split the buffer at offset at (relative to pos). The bytes
in [pos+at, last) are moved into a new mbuf taken from pool
and returned. The original keeps [pos, pos+at). Mirrors
mbuf_split (without the precopy callback - callers that
previously injected a header into the new buffer can do so on
the returned Mbuf before chaining it).
Returns None if at is greater than len or if
the moved tail would not fit in a fresh pool buffer.
§Examples
use dynomite::io::mbuf::MbufPool;
let pool = MbufPool::default();
let mut head = pool.get();
head.recv(b"hello world");
let tail = head.split_off(5, &pool).unwrap();
assert_eq!(head.readable(), b"hello");
assert_eq!(tail.readable(), b" world");Sourcepub fn append(&mut self, other: &Mbuf) -> usize
pub fn append(&mut self, other: &Mbuf) -> usize
Append the readable region of other into this buffer.
Returns the number of bytes appended (which may be less than
other.len() if this buffer fills first).
§Examples
use dynomite::io::mbuf::Mbuf;
let mut a = Mbuf::with_chunk_size(1024);
let mut b = Mbuf::with_chunk_size(1024);
a.recv(b"foo");
b.recv(b"bar");
a.append(&b);
assert_eq!(a.readable(), b"foobar");