10 releases

1.0.0 Oct 4, 2020
0.8.5 Aug 26, 2022
0.8.4 Apr 7, 2022
0.8.3 Oct 21, 2021
0.1.1 Jun 25, 2019

#81 in Data structures

Download history 1693/week @ 2022-06-10 1587/week @ 2022-06-17 2445/week @ 2022-06-24 1682/week @ 2022-07-01 1988/week @ 2022-07-08 1770/week @ 2022-07-15 2413/week @ 2022-07-22 1487/week @ 2022-07-29 1646/week @ 2022-08-05 1679/week @ 2022-08-12 1337/week @ 2022-08-19 1213/week @ 2022-08-26 932/week @ 2022-09-02 2513/week @ 2022-09-09 1794/week @ 2022-09-16 1304/week @ 2022-09-23

6,678 downloads per month
Used in 9 crates (7 directly)

MIT license

68KB
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.55

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

Dependencies

~20KB