This ring buffer stores read and write indices while being able to utilise the full backing slice by incrementing the indices modulo twice the slice’s length and reducing indices modulo the slice’s length on slice access. This means that whether the ring buffer is full or empty can be distinguished by looking at the difference between the read and write indices without adding an extra boolean flag or having to reserve a slot in the buffer.

This ring buffer has not been implemented with thread safety in mind, and therefore should not be assumed to be suitable for use cases involving separate reader and writer threads.

Fields

data: []u8,
read_index: usize,
write_index: usize,

Types

Functions

fn deinit(self: *RingBuffer, allocator: Allocator) void

Free the data backing a RingBuffer; must be passed the same Allocator as `i…

Free the data backing a RingBuffer; must be passed the same Allocator as init().

fn init(allocator: Allocator, capacity: usize) Allocator.Error!RingBuffer

Allocate a new RingBuffer; deinit() should be called to free the buffer.

fn isEmpty(self: RingBuffer) bool

Returns true if the ring buffer is empty and false otherwise.

fn isFull(self: RingBuffer) bool

Returns true if the ring buffer is full and false otherwise.

fn len(self: RingBuffer) usize

Returns the length

fn mask(self: RingBuffer, index: usize) usize

Returns index modulo the length of the backing slice.

fn mask2(self: RingBuffer, index: usize) usize

Returns index modulo twice the length of the backing slice.

fn read(self: *RingBuffer) ?u8

Consume a byte from the ring buffer and return it. Returns null if the ring b…

Consume a byte from the ring buffer and return it. Returns null if the ring buffer is empty.

fn readAssumeLength(self: *RingBuffer) u8

Consume a byte from the ring buffer and return it; asserts that the buffer is n…

Consume a byte from the ring buffer and return it; asserts that the buffer is not empty.

fn sliceAt(self: RingBuffer, start_unmasked: usize, length: usize) Slice

Returns a Slice for the region of the ring buffer starting at `self.mask(star…

Returns a Slice for the region of the ring buffer starting at self.mask(start_unmasked) with the specified length.

fn sliceLast(self: RingBuffer, length: usize) Slice

Returns a Slice for the last length bytes written to the ring buffer. Does …

Returns a Slice for the last length bytes written to the ring buffer. Does not check that any bytes have been written into the region.

fn write(self: *RingBuffer, byte: u8) Error!void

Write byte into the ring buffer. Returns error.Full if the ring buffer is f…

Write byte into the ring buffer. Returns error.Full if the ring buffer is full.

fn writeAssumeCapacity(self: *RingBuffer, byte: u8) void

Write byte into the ring buffer. If the ring buffer is full, the oldest byte …

Write byte into the ring buffer. If the ring buffer is full, the oldest byte is overwritten.

fn writeSlice(self: *RingBuffer, bytes: []const u8) Error!void

Write bytes into the ring buffer. Returns error.Full if the ring buffer doe…

Write bytes into the ring buffer. Returns error.Full if the ring buffer does not have enough space, without writing any data.

fn writeSliceAssumeCapacity(self: *RingBuffer, bytes: []const u8) void

Write bytes into the ring buffer. If there is not enough space, older bytes w…

Write bytes into the ring buffer. If there is not enough space, older bytes will be overwritten.

Error Sets