Crates.io | circular-buffer |
lib.rs | circular-buffer |
version | 0.1.9 |
source | src |
created_at | 2023-01-28 06:52:43.509539 |
updated_at | 2024-09-24 00:40:03.605939 |
description | Efficient, fixed-size, overwriting circular buffer |
homepage | |
repository | https://github.com/andreacorbellini/rust-circular-buffer |
max_upload_size | |
id | 770220 |
size | 212,444 |
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!
For a full list of changes between releases, visit GitHub.
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`