| Crates.io | ring-buffer-macro |
| lib.rs | ring-buffer-macro |
| version | 0.1.0 |
| created_at | 2025-12-24 21:23:11.435092+00 |
| updated_at | 2025-12-24 21:23:11.435092+00 |
| description | A procedural macro for creating ring buffer (circular buffer) data structures at compile time |
| homepage | |
| repository | https://github.com/0xsouravm/ring-buffer-macro |
| max_upload_size | |
| id | 2003867 |
| size | 42,936 |
A procedural macro for creating ring buffer (circular buffer) data structures at compile time
A ring buffer is a fixed-size FIFO (First-In-First-Out) data structure that efficiently reuses memory by wrapping around when it reaches the end. This macro generates all necessary fields and methods at compile time with zero runtime overhead.
CloneAdd this to your Cargo.toml:
[dependencies]
ring-buffer-macro = "0.1.0"
use ring_buffer_macro::ring_buffer;
#[ring_buffer(5)]
struct IntBuffer {
data: Vec<i32>,
}
fn main() {
let mut buf = IntBuffer::new();
buf.enqueue(1).unwrap();
buf.enqueue(2).unwrap();
buf.enqueue(3).unwrap();
assert_eq!(buf.dequeue(), Some(1));
assert_eq!(buf.dequeue(), Some(2));
assert_eq!(buf.len(), 1);
}
The #[ring_buffer(capacity)] attribute macro transforms your struct:
#[ring_buffer(5)]
struct Buffer {
data: Vec<i32>,
}
Adds fields:
capacity: usize - Maximum elementshead: usize - Read positiontail: usize - Write positionsize: usize - Current countGenerates methods:
new() - Creates empty bufferenqueue(item) - Adds item, returns Err(item) if fulldequeue() - Removes oldest item (requires T: Clone)is_full() - Checks if at capacityis_empty() - Checks if emptylen() - Current element countcapacity() - Maximum capacityclear() - Removes all elementsdata of type Vec<T>T must implement CloneContributions are welcome! Please open an issue or submit a pull request.
Licensed under the MIT License. See LICENSE for details.