Encodes a single RelishValue to bytes.
Resets the internal position and encodes the value, returning a slice of the internal buffer containing only the bytes needed for this value. Each call produces fresh bytes independent of previous calls.
The RelishValue to encode
A Result containing the encoded bytes, or an EncodeError on failure
Reusable binary encoder with pre-allocated buffer for high-performance encoding.
The Encoder class provides stateful encoding with an internal buffer that grows dynamically as needed. Reusing a single Encoder instance avoids repeated buffer allocation overhead for batch encoding operations.
Example
Remarks
The Encoder maintains a resizable internal buffer that grows when needed to accommodate larger values. The buffer is NOT cleared between calls; only the position is reset. This design prioritizes performance over memory footprint for the common case of encoding multiple similar-sized values.
Integer validation: The encoder performs defense-in-depth validation on all integer values, re-checking ranges and rejecting non-integer floats even if value constructors are somehow bypassed. This ensures wire format integrity.
Initialize with a custom size for better memory efficiency if encoding many small values:
new Encoder(256)instead of the default 1024 bytes.