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
sourceimpl<T: Clone + Copy + Default> RbInspector for SpscRb<T>
impl<T: Clone + Copy + Default> RbInspector for SpscRb<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for SpscRb<T>
impl<T> Send for SpscRb<T> where
T: Send,
impl<T> Sync for SpscRb<T> where
T: Send,
impl<T> Unpin for SpscRb<T>
impl<T> !UnwindSafe for SpscRb<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more