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 incrementingproducer_seq - The
assume_init_read()intry_consume()is safe because we check thatproducer_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 theSendimpl below). Its&selfmethods mutate throughUnsafeCell, so a shared&RingBufferwould let two safe threads race one end of the ring. channel::spsc::SpscChannelis crate-private and isSync, because itsArcmust beSendto back'statichalves. Its safety argument is the non-Clonehalves plus crate-private reach (ADR-024), which a public type cannot invoke. It adds the cached-index layer, aclosedflag, 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>
impl<T> RingBuffer<T>
Sourcepub fn try_produce(&self, value: T) -> Result<(), T>
pub fn try_produce(&self, value: T) -> Result<(), T>
Try to produce a value, handing it back when the ring is full.
Sourcepub fn try_consume(&self) -> Option<T>
pub fn try_consume(&self) -> Option<T>
Try to consume a value