10 releases

0.1.9 Sep 24, 2024
0.1.8 Sep 23, 2024
0.1.7 Mar 17, 2024
0.1.6 Nov 26, 2023
0.1.0 Jan 28, 2023

#73 in Data structures

Download history 5474/week @ 2024-07-28 5883/week @ 2024-08-04 5275/week @ 2024-08-11 5071/week @ 2024-08-18 7255/week @ 2024-08-25 5503/week @ 2024-09-01 5397/week @ 2024-09-08 6223/week @ 2024-09-15 8973/week @ 2024-09-22 7635/week @ 2024-09-29 8606/week @ 2024-10-06 8590/week @ 2024-10-13 8821/week @ 2024-10-20 8014/week @ 2024-10-27 8419/week @ 2024-11-03 8340/week @ 2024-11-10

34,078 downloads per month
Used in 15 crates (13 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`

Dependencies