Skip to main content

Mbuf

Struct Mbuf 

Source
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 of recv).

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

Source

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());
Source

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

pub fn data_size(&self) -> usize

Number of bytes that can be written before is_full trips, equal to chunk_size - MBUF_ESIZE. Mirrors mbuf_data_size.

§Examples
use dynomite::io::mbuf::{Mbuf, MBUF_SIZE, MBUF_ESIZE};
let buf = Mbuf::with_chunk_size(MBUF_SIZE);
assert_eq!(buf.data_size(), MBUF_SIZE - MBUF_ESIZE);
Source

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

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

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.

Source

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

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());
Source

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());
Source

pub fn rewind(&mut self)

Discard all buffered bytes and rewind both cursors to the origin. Mirrors mbuf_rewind.

Source

pub fn flags(&self) -> u32

Read the buffer’s flag bits.

Source

pub fn set_flag(&mut self, flag: u32, on: bool)

Set or clear individual flag bits.

Source

pub fn owner(&self) -> Option<OwnerId>

Return the optional owner connection id stamped on the buffer.

Source

pub fn set_owner(&mut self, owner: Option<OwnerId>)

Stamp or clear the owner connection id.

Source

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");
Source

pub fn writable(&mut self) -> &mut [u8]

Borrow the writable region ([last, end)).

Source

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");
Source

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");
Source

pub fn copy_from_slice(&mut self, src: &[u8])

Copy n bytes from src into the writable region without bounds-checking against partial copy. Panics if n exceeds remaining. Mirrors mbuf_copy.

§Examples
use dynomite::io::mbuf::Mbuf;
let mut buf = Mbuf::with_chunk_size(1024);
buf.copy_from_slice(b"abc");
assert_eq!(buf.readable(), b"abc");
Source

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");
Source

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");
Source

pub fn advance_pos(&mut self, n: usize)

Mark n bytes of the readable region as consumed by advancing pos. Useful when downstream code wrote directly into writable. Panics if n exceeds len.

Source

pub fn advance_last(&mut self, n: usize)

Mark n bytes of the writable region as filled by advancing last. Used after writing into writable directly (for example through an AsyncRead call). Panics if n exceeds remaining.

Trait Implementations§

Source§

impl Debug for Mbuf

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Mbuf

§

impl RefUnwindSafe for Mbuf

§

impl Send for Mbuf

§

impl Sync for Mbuf

§

impl Unpin for Mbuf

§

impl UnsafeUnpin for Mbuf

§

impl UnwindSafe for Mbuf

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,