pub struct SpscRing<T: Copy + Default> { /* private fields */ }Expand description
Lock-free single-producer single-consumer ring buffer.
The hot path for audio data. No heap allocation after initialization. Uses atomic head/tail pointers with cache-line padding to prevent false sharing.
§Performance
- Wait-free on the fast path (atomic store/load only)
- Power-of-two capacity for bitwise modulo (single-cycle AND vs multi-cycle DIV)
- Bounded memory, no allocation after init
§Safety
This buffer is safe for concurrent use by exactly one producer and one consumer. Multiple producers or multiple consumers will cause data races.
Implementations§
Source§impl<T: Copy + Default> SpscRing<T>
impl<T: Copy + Default> SpscRing<T>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new ring buffer with the given capacity.
Capacity is rounded up to the next power of two for efficient modulo.
§Panics
Panics if capacity is 0.
Sourcepub fn write(&self, data: &[T]) -> usize
pub fn write(&self, data: &[T]) -> usize
Write samples into the ring buffer.
Returns the number of samples actually written (may be less than
data.len() if the buffer is nearly full).
This is the producer method — call from exactly one thread.
Trait Implementations§
impl<T: Copy + Default + Send> Send for SpscRing<T>
impl<T: Copy + Default + Send> Sync for SpscRing<T>
Auto Trait Implementations§
impl<T> !Freeze for SpscRing<T>
impl<T> RefUnwindSafe for SpscRing<T>where
T: RefUnwindSafe,
impl<T> Unpin for SpscRing<T>
impl<T> UnwindSafe for SpscRing<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more