Struct SegmentedBuffer
A bounded, single-threaded buffer whose contents remain segmented.
struct SegmentedBuffer(T);
SegmentedBuffer!T is useful when a consumer needs to append borrowed
chunks, expose the result as ordered chunks, and consume data from the
front without copying everything into one contiguous allocation. Each
segment is independently owned. Acquiring a later segment never moves an
earlier live segment, and consumed segments are retained for reuse.
The buffer is bounded by maximumLength. append accepts as much input as
fits and reports the number accepted. segment exposes the remaining data
in one logical segment. consume removes data from the front and
reset starts a new logical batch while retaining acquired capacity.
The type is single-threaded. It does not perform networking, retry, backpressure, flattening, or implicit geometric growth policy.
Example for a chunked byte consumer:
import containers : SegmentedByteBuffer;
auto received = SegmentedByteBuffer(64 * 1024, 16 * 1024 * 1024);
const accepted = received .append(incomingChunk);
assert(accepted == incomingChunk .length);
foreach (index; 0 .. received .segmentCount)
send(received .segment(index));
received .consume(bytesSent);
A segment slice borrows the buffer. Do not retain it across append,
consume, reset, owner move, or destruction.
Constructors
| Name | Description |
|---|---|
this
(segmentSize, maximumLength)
|
Creates an empty buffer. |
Methods
| Name | Description |
|---|---|
append
(values)
|
Appends as many values as fit and returns the number accepted. |
capacity
()
|
Maximum number of live elements held by the buffer. |
consume
(count)
|
Consumes up to count elements from the front without flattening.
|
empty
()
|
Whether no elements are currently available. |
full
()
|
Whether no further elements can be appended. |
length
()
|
Number of elements currently available for consumption. |
reset
()
|
Ends all live element lifetimes and starts an empty reusable batch. |
segment
(index)
|
Borrows one logical segment in consumption order. |
segmentCount
()
|
Number of logical segments currently containing available elements. |
segmentSize
()
|
Preferred maximum number of elements stored in one segment. |