Skip to main content

RingBuffer

Struct RingBuffer 

Source
pub struct RingBuffer { /* private fields */ }
Expand description

Thread-safe, in-process circular buffer for streaming binary frames.

Frames are variable-length byte slices stored with a u32 LE length prefix. The buffer enforces a byte budget; when a push would exceed the budget the oldest frames are silently dropped (lossy back-pressure).

§Thread safety

All public methods take &self and synchronize via an internal Mutex. Contention is expected to be low: typically one producer thread and one consumer (the custom protocol handler draining on a fetch call).

Implementations§

Source§

impl RingBuffer

Source

pub fn new(capacity: usize) -> Self

Create a ring buffer with the given byte capacity.

§Panics

Panics if capacity is less than DRAIN_FRAME_OVERHEAD + 1 (5 bytes), since at least a 1-byte frame must be storable.

Source

pub fn with_default_capacity() -> Self

Create a ring buffer with the default capacity (64 KB).

Source

pub fn push(&self, frame: &[u8]) -> usize

Push a frame into the buffer.

If the frame (plus its 4-byte length prefix) would exceed the byte budget, the oldest frames are dropped until there is room. Returns the number of frames that were dropped to make space.

If the frame itself is larger than the total capacity it is silently discarded and the return value is 0.

Source

pub fn push_checked(&self, frame: &[u8]) -> PushOutcome

Push a frame with a richer outcome report.

Like push, but returns PushOutcome::TooLarge when the frame can never fit, instead of silently returning 0.

Source

pub fn drain_all(&self) -> Vec<u8>

Drain all buffered frames into a single binary blob and clear the buffer.

§Wire format
[u32 LE frame_count]
[u32 LE len_1][bytes_1]
[u32 LE len_2][bytes_2]
...

Returns an empty Vec if the buffer is empty.

Source

pub fn try_pop(&self) -> Option<Vec<u8>>

Read one frame from the front of the buffer (FIFO).

Returns None if the buffer is empty.

Source

pub fn frame_count(&self) -> usize

Number of frames currently buffered.

Source

pub fn bytes_used(&self) -> usize

Number of bytes currently used (including per-frame length prefixes).

Source

pub fn capacity(&self) -> usize

Total byte capacity of the buffer.

Source

pub fn clear(&self)

Clear all buffered frames.

Trait Implementations§

Source§

impl Debug for RingBuffer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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, 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.