A Ring Buffer implementation with a fixed-size buffer.
const RingBuffer = @import("ring-buffer").RingBuffer;
var buffer = try RingBuffer([]const u8, .{ .capacity = 2 }).init();
try buffer.write("hello");
try buffer.write("circular");
_ = try buffer.read(); // hello
_ = try buffer.read(); // circular
try buffer.write("world");
_ = try buffer.read(); // world
zig fetch --save https://github.com/ruy-dan/ring-buffer/archive/refs/heads/main.tar.gz
build.zig.zon
.dependencies = .{
.ring_buffer = .{
.url = "https://github.com/ruy-dan/ring-buffer/archive/refs/heads/main.tar.gz",
// .hash
}
}
build.zig
const ring_buffer = b.dependency("ring_buffer", .{
.target = target,
.optimize = optimize,
});
exe.addModule("ring-buffer", ring_buffer.module("ring-buffer"));
zig build run
pub fn RingBuffer(comptime T: type, opts: Opts) !type
Creates a new RingBuffer with a fixed-size buffer. The capacity can be set through opts. Otherwise, the default capacity is set to 1024. Returns an error if the capacity is not a power of two.
const Opts = struct { capacity: usize = 1024 };
zig test test.zig
Returns true
if the ring buffer is empty, false
otherwise.
Returns true
if the ring buffer is full, false
otherwise.
Returns the number of elements in the ring buffer.
Writes an item to the ring buffer. Returns an error if the buffer is full.
Reads an item from the ring buffer. Returns an error if the buffer is empty.
Clears the ring buffer.
Prints the contents of the ring buffer for debugging.
MIT