Lock-free SPSC FIFO ring buffer with direct access to inner data.
Copy
).Read
and Write
implementation.std
and even without alloc
(using only statically-allocated memory).async
/.await
support.At first you need to create the ring buffer itself. HeapRb
is recommended but you may choose another one.
After the ring buffer is created it may be splitted into pair of Producer
and Consumer
.
Producer
is used to insert items to the ring buffer, Consumer
- to remove items from it.
For SharedRb
and its derivatives they can be used in different threads.
There are several types of ring buffers provided:
LocalRb
. Only for single-threaded use.SharedRb
. Can be shared between threads. Its derivatives:
HeapRb
. Contents are stored in dynamic memory. Recommended for use in most cases.StaticRb
. Contents can be stored in statically-allocated memory.SharedRb
needs to synchronize CPU cache between CPU cores. This synchronization has some overhead.
To avoid multiple unnecessary synchronizations you may use postponed mode of operation (see description for Producer
and Consumer
)
or methods that operates many items at once (Producer::push_slice
/Producer::push_iter
, Consumer::pop_slice
, etc.).
For single-threaded usage LocalRb
is recommended because it is faster than SharedRb
due to absence of CPU cache synchronization.
You may see typical performance of different methods in benchmarks:
bash
cargo +nightly bench --features bench
Nightly toolchain is required.
```rust use ringbuf::HeapRb;
let rb = HeapRb::
prod.push(0).unwrap(); prod.push(1).unwrap(); assert_eq!(prod.push(2), Err(2));
assert_eq!(cons.pop(), Some(0));
prod.push(2).unwrap();
asserteq!(cons.pop(), Some(1)); asserteq!(cons.pop(), Some(2)); assert_eq!(cons.pop(), None);
```
```rust use ringbuf::StaticRb;
const RBSIZE: usize = 1;
let mut rb = StaticRb::
asserteq!(prod.push(123), Ok(())); asserteq!(prod.push(321), Err(321));
asserteq!(cons.pop(), Some(123)); asserteq!(cons.pop(), None);
```
async
/.await
There is an experimental crate async-ringbuf
which is built on top of ringbuf
and implements asynchronous ring buffer operations.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.