Struct StaticRingBuffer

Stores up to Capacity FIFO elements in inline storage.

struct StaticRingBuffer(T, ulong Capacity);

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 () Returns 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 () Returns whether all Capacity slots contain live elements.
length () Returns the number of live elements.
opIndex (logicalIndex) Returns a mutable reference to an element 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/context-bearing struct types are unsupported because generic raw-storage construction cannot portably supply their hidden lexical context
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

StaticRingBuffer!(int, 3) buffer;
assert(buffer.tryPushBack(10));
assert(buffer.tryPushBack(20));
buffer.popFront();
assert(buffer.front == 20);