pub struct RingBuffer<T> { /* private fields */ }Expand description
A Single-Producer, Single-Consumer (SPSC) lock free ring buffer.
This queue is “wait-free” (bounded time) for both producer and consumer.
It does not block, but return Err or None if the queue is full or empty.
Implementations§
Source§impl<T> RingBuffer<T>
impl<T> RingBuffer<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new SPSC ring buffer with at least the given capacity The actual capacity will be rounded up to the next power of 2.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items currently in the buffer. This is a snapshot and maybe out of date immediately.
Sourcepub fn send(&self, item: T) -> Result<(), T>
pub fn send(&self, item: T) -> Result<(), T>
sends a item into a buffer
Fails if the buffer is full, returning an Err(item).
This is the Producer method.
pub fn recv(&self) -> Option<T>
Trait Implementations§
Source§impl<T> Drop for RingBuffer<T>
We must implement Drop to clean up any T a left in the buffer.
impl<T> Drop for RingBuffer<T>
We must implement Drop to clean up any T a left in the buffer.
impl<T: Send> Send for RingBuffer<T>
impl<T: Send> Sync for RingBuffer<T>
We can safely send the RingBuffer to other threads if T is Send
Unsafe is not Sync BUT WE know we are only accessing
the buffer safely from the single producer and single consumer.
The head and tail atomics prevent reading/writing the same slot.