ddn.encoding.binary
ddn.encoding.binary — endian-aware byte cursor for wire and file formats.
One reader and one writer for all DDN wire and file formats, with the byte order as a run-time value (the X protocol negotiates it per connection) or as a compile-time parameter (Wayland is little-endian only and pays no per-call order branch; SSH is big-endian only). Failures are Result values carrying a BinaryError, never exceptions; the fixed-capacity forms are @safe pure nothrow @nogc and CTFE-capable, so generated codecs can be validated at compile time.
Surface:
- ByteOrder —
LITTLEorBIG; native reportsthe host order.
- BinaryReader — run-time-order sequential reader over a
const(ubyte)[]. Byte slices returned bygetBytesandgetPrefixedalias the input buffer: nothing is copied, which protocols that hash the exact received bytes (SSH) rely on; the input must outlive the slices. - FixedBinaryReader — the same reader with the order
fixed at compile time.
- BinaryWriter — run-time-order writer over a
caller-provided fixed-capacity
ubyte[];@nogc; non-committing on capacity failure (the buffer prefix and the position are unchanged when an operation fails). - FixedBinaryWriter — the same writer with the order
fixed at compile time.
- AppenderBinaryWriter /
FixedAppenderBinaryWriter — growable writers that extend a GC-allocated buffer (the only allocating form; the allocation is the point).
- encode / decode — one-off scalar conversions
to and from fixed-size byte arrays.
Fixed-order design choice (the DL-4 binary-cursor task): the fixed-order forms are separate aliases over a shared implementation instead of a ByteOrder.RUNTIME enum member, so ByteOrder stays a two-value type that every consumer can switch over exhaustively, and the run-time-order path (X) is a plain struct with no template noise in diagnostics.
float and double are read and written as their IEEE-754 binary32 / binary64 bit patterns (real is not a wire type and is rejected at compile time). In CTFE the conversion uses exact frexp/ldexp arithmetic (D forbids bit reinterpretation at compile time) and NaN payloads are canonicalised; the two paths agree bit-exactly for every other value.
Cursors are small value types: a copy shares the underlying buffer but keeps an independent position.
Example:
import ddn.encoding.binary;
ubyte[32] buf;
auto w = BinaryWriter(buf[], ByteOrder.BIG);
assert(w.put!uint(0x29b7f4aa).ok);
assert(w.putPrefixed!ushort(cast(const(ubyte)[]) "hi").ok);
assert(w.written == [0x29, 0xb7, 0xf4, 0xaa, 0x00, 0x02, 'h', 'i']);
auto r = BinaryReader(w.written, ByteOrder.BIG);
assert(r.get!uint.value == 0x29b7f4aa);
assert(r.getPrefixed!ushort(255).value == cast(const(ubyte)[]) "hi");
assert(r.requireFullyConsumed().ok);Types 9
Byte order of a wire or file format.
Failure codes of the binary cursor.
Recoverable binary cursor failure with bounded context.
offset is the buffer position at which the failed operation started (for patch, the patch target; for getPrefixed, the length prefix). No strings, no pointers: returnable in @nogc nothrow contexts everywhere.
BinaryErrorCode codeWhich failure happened.size_t offsetBuffer position of the failed operation.Sequential reader over a const(ubyte)[] with the byte order chosen at run time (the X path). Returned byte slices alias the input.
Sequential reader with the byte order fixed at compile time, so the hot path carries no per-call order branch (the Wayland path). Instantiate as FixedBinaryReader!(ByteOrder.LITTLE).
Writer over a caller-provided fixed-capacity ubyte[] with the byte order chosen at run time. Writes are non-committing on capacity failure and never allocate (@nogc).
Fixed-capacity writer with the byte order fixed at compile time. Instantiate as FixedBinaryWriter!(ByteOrder.LITTLE).
Growable writer with the byte order chosen at run time. The buffer is extended through the GC as needed (documented allocation; use the fixed-capacity forms where @nogc is required).
Growable writer with the byte order fixed at compile time. Instantiate as FixedAppenderBinaryWriter!(ByteOrder.BIG).
Functions 3
ubyte[T.sizeof] encode(T)(T value, ByteOrder order) if (isWireScalar!T) @safe pure nothrow @nogcEncodes one scalar of type `T` as `T.sizeof` bytes in byte order `order` — the one-off form of the writer's `put`.Result!(T, BinaryError) decode(T)(scope const(ubyte)[] src, ByteOrder order) if (isWireScalar!T) @safe pure nothrow @nogcDecodes one scalar of type `T` from the start of `src` in byte order `order` — the one-off form of the reader's `get`. Bytes beyond `T.sizeof` are ignored.