buffer.rs

Overview

This file provides buffer management for stream data transmission and reception within a networking context. It defines two primary buffer abstractions:

Both buffers interact closely with the stream layer, particularly the StreamInstance type, to manage data flow and lifecycle events.


Types and Structures

StreamRecvBuffer

A buffer for receiving data from a stream. It implements the [bytes::Buf] trait to provide read operations over a collection of buffers (msquic::ffi::QUIC_BUFFER). It tracks offsets and read cursors to efficiently handle partial consumption of buffers.

Fields

Methods

new
pub(crate) fn new<T: AsRef<[msquic::BufferRef]> + ?Sized>(offset: usize, buffers: &T, fin: bool) -> Self

Creates a new StreamRecvBuffer from a list of msquic::BufferRef buffers.

let recv_buffer = StreamRecvBuffer::new(stream_offset, &buffers, is_fin);
set_stream
pub(crate) fn set_stream(&mut self, stream: Arc<StreamInstance>)

Associates this buffer with a specific StreamInstance.

len
pub fn len(&self) -> usize

Returns the number of unread bytes remaining in the buffer.

is_empty
pub fn is_empty(&self) -> bool

Indicates whether the buffer has no remaining unread data.

as_slice_upto_size
pub fn as_slice_upto_size(&self, size: usize) -> &[u8]

Provides a read-only byte slice of at most size bytes from the current read position in the buffer.

get_bytes_upto_size
pub fn get_bytes_upto_size<'a>(&mut self, size: usize) -> Option<&'a [u8]>

Consumes and returns a slice of up to size bytes from the current read position, advancing the read cursor accordingly.

offset
pub fn offset(&self) -> usize

Returns the starting offset of this buffer's data in the stream.

range
pub fn range(&self) -> Range<usize>

Returns the byte range in the stream covered by this buffer: [offset, offset + len).

fin
pub fn fin(&self) -> bool

Indicates whether this buffer marks the end of the stream.


Trait Implementations for StreamRecvBuffer

Buf Trait

Implements [bytes::Buf] for StreamRecvBuffer to allow iterator-style consumption of buffer data.


Drop Implementation for StreamRecvBuffer

When a StreamRecvBuffer instance is dropped, it notifies the associated StreamInstance (if any) that the read operation is complete by calling stream.read_complete(self). This lifecycle hook allows the stream to manage buffer recycling or other cleanup operations.


WriteBuffer

An abstraction for preparing data to be written to a stream, supporting zero-copy slices and internally owned byte vectors.

Structure

Methods

new
pub(crate) fn new() -> Self

Creates a new, empty WriteBuffer.

from_raw
pub(crate) unsafe fn from_raw(inner: *const c_void) -> Self

Creates a WriteBuffer from a raw pointer, used for interoperation with FFI or external buffer management.

put_zerocopy
pub(crate) fn put_zerocopy(&mut self, buf: &Bytes) -> usize

Appends a zero-copy buffer (Bytes) to the WriteBuffer.

put_slice
pub(crate) fn put_slice(&mut self, slice: &[u8]) -> usize

Appends a slice of bytes to the internal owned buffer.

get_buffers
pub(crate) fn get_buffers(&mut self) -> (*const msquic::BufferRef, usize)

Prepares and returns a pointer and count of buffers suitable for sending via the underlying QUIC transport.

into_raw
pub(crate) fn into_raw(self) -> *mut c_void

Consumes the WriteBuffer and returns a raw pointer for FFI usage.

reset
pub(crate) fn reset(&mut self)

Clears all internal buffers, preparing this instance for reuse.


Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: Structure of buffer.rs

classDiagram
class StreamRecvBuffer {
-stream: Option<Arc<StreamInstance>>
-buffers: Vec<QUIC_BUFFER>
-offset: usize
-len: usize
-read_cursor: usize
-read_cursor_in_buffer: usize
-fin: bool
+new()
+set_stream()
+len()
+is_empty()
+as_slice_upto_size()
+get_bytes_upto_size()
+offset()
+range()
+fin()
+advance()
+chunk()
+remaining()
+chunks_vectored()
}
class WriteBuffer {
-0: Box<WriteBufferInner>
+new()
+from_raw()
+put_zerocopy()
+put_slice()
+get_buffers()
+into_raw()
+reset()
}
class WriteBufferInner {
-internal: Vec<u8>
-zerocopy: Vec<Bytes>
-buffers: Vec<BufferRef>
}
StreamRecvBuffer ..> StreamInstance : uses
WriteBufferInner ..> Bytes : contains
WriteBufferInner ..> BufferRef : contains