15 releases (6 breaking)

1.0.0 Oct 4, 2020
0.12.0 Jan 19, 2023
0.11.1 Dec 12, 2022
0.10.0 Oct 26, 2022
0.1.1 Jun 25, 2019

#60 in Data structures

Download history 2405/week @ 2022-12-08 2225/week @ 2022-12-15 1088/week @ 2022-12-22 1332/week @ 2022-12-29 1955/week @ 2023-01-05 1262/week @ 2023-01-12 1701/week @ 2023-01-19 2018/week @ 2023-01-26 2116/week @ 2023-02-02 3305/week @ 2023-02-09 9105/week @ 2023-02-16 2811/week @ 2023-02-23 2229/week @ 2023-03-02 1919/week @ 2023-03-09 2388/week @ 2023-03-16 2623/week @ 2023-03-23

9,458 downloads per month
Used in 18 crates (13 directly)

MIT license

74KB
1.5K SLoC

Ringbuffer

Github Workflows Codecov Docs.rs Crates.io

The ringbuffer crate provides safe fixed size circular buffers (ringbuffers) in rust.

Implementations for three kinds of ringbuffers, with a mostly similar API are provided:

type description
AllocRingBuffer Ringbuffer allocated on the heap at runtime. This ringbuffer is still fixed size and requires alloc.
ConstGenericRingBuffer Ringbuffer which uses const generics to allocate on the stack.

All of these ringbuffers also implement the RingBuffer trait for their shared API surface.

MSRV: Rust 1.59

Usage

use ringbuffer::{AllocRingBuffer, RingBuffer};
fn main() {
    let mut buffer = AllocRingBuffer::with_capacity(2);

    // First entry of the buffer is now 5.
    buffer.push(5);

    // The last item we pushed is 5
    assert_eq!(buffer.get(-1), Some(&5));

    // Second entry is now 42.
    buffer.push(42);
    assert_eq!(buffer.peek(), Some(&5));
    assert!(buffer.is_full());

    // Because capacity is reached the next push will be the first item of the buffer.
    buffer.push(1);
    assert_eq!(buffer.to_vec(), vec![42, 1]);
}

Features

name default description
alloc Disable this feature to remove the dependency on alloc. The feature is compatible with no_std.

License

Licensed under MIT License

No runtime deps