8 releases

1.0.0 Oct 4, 2020
0.8.2 Sep 13, 2021
0.7.1 Apr 18, 2021
0.5.1 Mar 29, 2021
0.1.1 Jun 25, 2019

#71 in Data structures

Download history 157/week @ 2021-06-06 165/week @ 2021-06-13 64/week @ 2021-06-20 67/week @ 2021-06-27 157/week @ 2021-07-04 141/week @ 2021-07-11 191/week @ 2021-07-18 258/week @ 2021-07-25 288/week @ 2021-08-01 220/week @ 2021-08-08 285/week @ 2021-08-15 193/week @ 2021-08-22 227/week @ 2021-08-29 239/week @ 2021-09-05 182/week @ 2021-09-12 147/week @ 2021-09-19

822 downloads per month
Used in 4 crates (3 directly)

LGPL-3.0

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.

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. Useful for kernels.

License

Licensed under GNU Lesser General Public License v3.0

Dependencies

~17KB