#buffer #queue #fifo #small

no-std circular-buffer

Efficient, fixed-size, overwriting circular buffer

8 releases

0.1.7 Mar 17, 2024
0.1.6 Nov 26, 2023
0.1.3 Aug 28, 2023
0.1.1 Feb 1, 2023
0.1.0 Jan 28, 2023

#150 in Data structures

Download history 557/week @ 2023-12-23 1153/week @ 2023-12-30 906/week @ 2024-01-06 724/week @ 2024-01-13 1035/week @ 2024-01-20 1512/week @ 2024-01-27 1134/week @ 2024-02-03 1164/week @ 2024-02-10 1052/week @ 2024-02-17 1151/week @ 2024-02-24 1749/week @ 2024-03-02 2568/week @ 2024-03-09 2486/week @ 2024-03-16 2838/week @ 2024-03-23 3615/week @ 2024-03-30 3315/week @ 2024-04-06

12,652 downloads per month
Used in 8 crates (7 directly)

BSD-3-Clause

175KB
3K SLoC

Circular Buffer for Rust

Crate Documentation License

This is a Rust crate that implements a circular buffer, also known as cyclic buffer, circular queue or ring.

This circular buffer has a fixed maximum capacity, does not automatically grow, and once its maximum capacity is reached, elements at the start of the buffer are overwritten. It's useful for implementing fast FIFO (first in, first out) and LIFO (last in, first out) queues with a fixed memory capacity.

For more information and examples, check out the documentation!

Changelog

For a full list of changes between releases, visit GitHub.

Example

use circular_buffer::CircularBuffer;

// Initialize a new, empty circular buffer with a capacity of 5 elements
let mut buf = CircularBuffer::<5, u32>::new();

// Add a few elements
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf, [1, 2, 3]);

// Add more elements to fill the buffer capacity completely
buf.push_back(4);
buf.push_back(5);
assert_eq!(buf, [1, 2, 3, 4, 5]);

// Adding more elements than the buffer can contain causes the front elements to be
// automatically dropped
buf.push_back(6);
assert_eq!(buf, [2, 3, 4, 5, 6]); // `1` got dropped to make room for `6`

No runtime deps