Struct rb::SpscRb
[−]
[src]
pub struct SpscRb<T> { // some fields omitted }
A thread-safe Single-Producer-Single-Consumer RingBuffer
- blocking and non-blocking IO
- mutually exclusive access for producer and consumer
- no use of
unsafe
- never under- or overflows
use std::thread; use rb::*; let rb = SpscRb::new(1024); let (prod, cons) = (rb.producer(), rb.consumer()); thread::spawn(move || { let gen = || {(-16..16+1).cycle().map(|x| x as f32/16.0)}; loop { let data = gen().take(32).collect::<Vec<f32>>(); prod.write(&data).unwrap(); } }); let mut data = Vec::with_capacity(1024); let mut buf = [0.0f32; 256]; while data.len() < 1024 { let cnt = cons.read_blocking(&mut buf).unwrap(); data.extend_from_slice(&buf[..cnt]); }
Methods
impl<T: Clone + Default> SpscRb<T>
[src]
Trait Implementations
impl<T: Clone + Default> RB<T> for SpscRb<T>
[src]
fn clear(&self)
Resets the whole buffer to the default value of type T
. The buffer is empty after this call. Read more
fn skip_pending(&self) -> Result<usize>
Skips all pending values. Technically it sets the read pointer of the producer to the position of the conumers write pointer. Read more
fn skip(&self, cnt: usize) -> Result<usize>
Skips cnt
number of elements. Read more
fn producer(&self) -> Producer<T>
Creates a producer view inside the buffer.
fn consumer(&self) -> Consumer<T>
Creates a consumer view inside the buffer.
impl<T: Clone + Default> RbInspector for SpscRb<T>
[src]
fn is_empty(&self) -> bool
Returns true if the buffer is empty.
fn is_full(&self) -> bool
Returns true if the buffer is full.
fn capacity(&self) -> usize
Returns the total capacity of the ring buffer. This is the size with which the buffer was initialized. Read more
fn slots_free(&self) -> usize
Returns the number of values that can be written until the buffer until it is full.
fn count(&self) -> usize
Returns the number of values from the buffer that are available to read.