Struct StaticRingBuffer

Fixed-capacity FIFO for small bounded queues that should carry their storage inline with the owning object.

struct StaticRingBuffer(T, ulong Capacity);

Use StaticRingBuffer when the maximum queue size is known at compile time and a separate backing allocation is undesirable. It preserves FIFO order across physical wraparound and can expose the logical sequence as at most two contiguous borrowed segments.

Exactly length slots contain live T objects. Unused slots are raw storage and are not default-constructed merely because the buffer exists.

Copy construction is element-wise when T is copyable. Whole-buffer move construction preserves either T's D language move-constructor contract or, for classic relocation types, the moveEmplace/opPostMove contract. Identity assignment is currently disabled.

Constructors

NameDescription
this (rhs) Element-wise copy construction.
this (rhs) Element-wise whole-buffer move construction.

Methods

NameDescription
back () Returns a mutable reference to the logical back element.
clear () Destroys every live element and leaves the buffer empty.
empty () Reports whether the buffer contains no live elements.
firstSegment () Returns the first contiguous physical segment in logical FIFO order.
front () Returns a mutable reference to the logical front element.
full () Reports whether all Capacity slots contain live elements.
length () Returns the number of live elements.
opIndex (logicalIndex) Returns a mutable reference by logical FIFO index.
popFront () Removes and destroys the front element.
secondSegment () Returns the wrapped continuation after firstSegment.
tryPushBack (value) Appends one value without overwriting existing elements.

Parameters

NameDescription
T element type; nested/local struct types carrying hidden outer context/indirections are not supported in the v0.1 API
Capacity maximum number of live elements; must be greater than zero

Init

.init is a valid empty buffer with no live T objects.

Complexity

Front/back access, indexed access, push, and pop are O(1).

Allocation

Container bookkeeping and inline storage do not allocate. Operations of T itself may allocate.

Thread Safety

Instances are not synchronized. External synchronization is required for concurrent mutation or mutation concurrent with reads.

Validation

See docs/validation.md and evidence/performance/ring-buffer-wraparound.md.

Example

Keep a small FIFO entirely inside its owning object.

StaticRingBuffer!(int, 3) buffer;

// The checked push never overwrites an older queued value.
assert(buffer.tryPushBack(10));
assert(buffer.tryPushBack(20));

// FIFO removal exposes the oldest remaining value.
buffer.popFront();
assert(buffer.front == 20);

// Capacity is compile-time fixed and requires no backing allocation.
static assert(buffer.capacity == 3);