Struct rb::SpscRb [] [src]

pub struct SpscRb<T> { /* 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]

Resets the whole buffer to the default value of type T. The buffer is empty after this call. Read more

Creates a producer view inside the buffer.

Creates a consumer view inside the buffer.

impl<T: Clone + Default> RbInspector for SpscRb<T>
[src]

Returns true if the buffer is empty.

Returns true if the buffer is full.

Returns the total capacity of the ring buffer. This is the size with which the buffer was initialized. Read more

Returns the number of values that can be written until the buffer until it is full.

Returns the number of values from the buffer that are available to read.