Struct rb::SpscRb

source · []
pub struct SpscRb<T> { /* private fields */ }
Expand description

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]);
}

Implementations

Trait Implementations

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.

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.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.