Skip to main content

RingBuffer

Struct RingBuffer 

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

Zero-copy ring buffer for high-throughput streaming

§Safety

This structure uses MaybeUninit for zero-copy performance:

  • Values are written with write() before incrementing producer_seq
  • The assume_init_read() in try_consume() is safe because we check that producer_seq > current, ensuring data was written

§Why this ring also backs the SPSC channel

SpscRing and channel::spsc’s halves are backed by this type. There is one Lamport protocol here — a masked slot array, a Relaxed load of the owner’s own cursor, an Acquire load of the peer’s, a Release store back, and the element written in between — reached through two access disciplines:

  • This type is public, exposes the uncached entry points, and stays !Sync (see the Send impl below). Its &self methods mutate through UnsafeCell, so a shared &RingBuffer would let two safe threads race one end of the ring.
  • channel::spsc::SpscChannel is crate-private and is Sync, because its Arc must be Send to back 'static halves. Its safety argument is the non-Clone halves plus crate-private reach (ADR-024), which a public type cannot invoke. It adds the cached-index layer, a closed flag, and the spin-then-yield policy.

The bound is not shared: granting Sync here would be unsound for downstream users, and a public type cannot invoke the channel’s argument. The code is shared instead. The cached primitives live on this type as pub(crate) methods, so the discipline stays on the wrapper that can enforce it while the storage, the cursors, and the publication algebra exist exactly once (ADR-016 item 3).

Implementations§

Source§

impl<T> RingBuffer<T>

Source

pub fn new(capacity: usize) -> Self

Create a new ring buffer with given capacity

Source

pub fn try_produce(&self, value: T) -> Result<(), T>

Try to produce a value, handing it back when the ring is full.

Source

pub fn try_consume(&self) -> Option<T>

Try to consume a value

Source

pub fn capacity(&self) -> usize

Get the capacity of the ring buffer

Source

pub fn is_empty(&self) -> bool

Check if the ring buffer is empty

Source

pub fn is_full(&self) -> bool

Check if the ring buffer is full

Source

pub fn len(&self) -> usize

Get the number of items currently in the buffer

Trait Implementations§

Source§

impl<T> Drop for RingBuffer<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T: Send> Send for RingBuffer<T>

Auto Trait Implementations§

§

impl<T> !Freeze for RingBuffer<T>

§

impl<T> !RefUnwindSafe for RingBuffer<T>

§

impl<T> !Sync for RingBuffer<T>

§

impl<T> Unpin for RingBuffer<T>

§

impl<T> UnsafeUnpin for RingBuffer<T>

§

impl<T> UnwindSafe for RingBuffer<T>

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.