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 + Copy + Default> SpscRb<T>
[src]

[src]

Trait Implementations

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

[src]

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

[src]

Creates a producer view inside the buffer.

[src]

Creates a consumer view inside the buffer.

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

[src]

Returns true if the buffer is empty.

[src]

Returns true if the buffer is full.

[src]

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

[src]

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

[src]

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

Auto Trait Implementations

impl<T> Send for SpscRb<T> where
    T: Send

impl<T> Sync for SpscRb<T> where
    T: Send