19 releases (9 breaking)

1.0.0 Oct 4, 2020
0.15.0 Sep 15, 2023
0.14.2 Jul 28, 2023
0.12.0 Jan 19, 2023
0.1.1 Jun 25, 2019

#51 in Data structures

Download history 6081/week @ 2024-01-03 6359/week @ 2024-01-10 10846/week @ 2024-01-17 10414/week @ 2024-01-24 11543/week @ 2024-01-31 8205/week @ 2024-02-07 8445/week @ 2024-02-14 9359/week @ 2024-02-21 12887/week @ 2024-02-28 12145/week @ 2024-03-06 13398/week @ 2024-03-13 13251/week @ 2024-03-20 11069/week @ 2024-03-27 9797/week @ 2024-04-03 10427/week @ 2024-04-10 7428/week @ 2024-04-17

41,101 downloads per month
Used in 26 crates (19 directly)

MIT license

105KB
2.5K SLoC

Ringbuffer

Github Workflows 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. This requires the alloc feature.
GrowableAllocRingBuffer Ringbuffer allocated on the heap at runtime. This ringbuffer can grow in size, and is implemented as an alloc::VecDeque internally. This requires the alloc feature.
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.back(), 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. Disabling this feature makes ringbuffer no_std.

License

Licensed under MIT License

No runtime deps