5 releases (1 stable)

1.0.0 Jan 8, 2024
0.4.0 Jan 7, 2024
0.3.0 Jan 7, 2024
0.2.0 Jan 7, 2024
0.1.0 Jan 7, 2024

#5 in #buffering

MIT/Apache

7KB
90 lines

River Ring Buffer

Crates.io Version docs.rs (with version) GitHub Actions Workflow Status Crates.io License

A simple ring buffer implemented in Rust.

A ring buffer is a high performant data structure that is great for buffering streams of data.

My motivation for building this was to understand ring buffers as they're used extensively in new Linux Kernel feature such as eBPF and IO Uring.

Example usage

use river_ring_buffer::RingBuffer;

fn main() -> Result<(), &'static str> {
    let mut buffer = RingBuffer::new(3);

    buffer.put(1)?;
    buffer.put(2)?;
    buffer.put(3)?;

    buffer.read(); // Returns Some(1)
}

An error is returned when the buffer is full.

use river_ring_buffer::RingBuffer;

fn main() -> Result<(), &'static str> {
    let mut buffer = RingBuffer::new(3);

    buffer.put(1)?;
    buffer.put(2)?;
    buffer.put(3)?;
    buffer.put(4)?; // Returns Err(BufferFullError)   
}

No runtime deps