ISpanDecoder<TValue>

Namespace: SnowBank.Data.Binary · interface

Defines methods to decode the binary representation of instances of type TValue from a span

Remarks

This type is expected to be implemented on structs ONLY, in order to achieve the best performance by using JIT inlining and other optimizations as much as possible.

Methods

TryDecode

static bool TryDecode(ReadOnlySpan<byte> source, out TValue value)

Decodes the value from the input buffer, if it contains enough data

  • source — Source buffer with bytes to decode
  • value — Receives the decoded value, if the operation was successful

Returns: true if the value was successfully decoded, false if the buffer does not contain a full representation of the value, or an exception if the decoding failed for other reasons.

The caller should first call this method with the first chunk of data available. As long as the method returns false, the caller should wait for more data to arrive before calling it again, until it returns true.

Please note that the method MUST NOT return false for a reason other than the buffer not containing enough data, otherwise the caller may end up in an infinite retry loop, passing a larger and larger buffer.

If the data is incomplete, but the available data is already malformed, the method SHOULD throw an exception, in order to avoid waiting for more data before throwing.