SpanEncoders
Namespace: SnowBank.Data.Binary · class
Encoders that can encode primitive types into binary form
Methods
Encode
static void Encode<TEncoder, TValue>(in TValue value, ArrayPool<byte> pool, int maxSize, out ReadOnlySpan<byte> result, out byte[] buffer, out Range range)
Encodes a value, using a pool if necessary
value— Value to encodepool— Pool used to allocate buffersmaxSize— If specified, maximum allowed size for the buffer. If 0, allows the buffer to grow up to the maximum size supported by the CLR.result— Receives the span that points to the encoded resultbuffer— Receives the buffer allocated from the pool to hold the result, ornullif the result is already hosted somewhere else in memory.range— Receives the range inbufferthat containsresult(only if buffer is notnull)
The method will call TryGetSpan to extract any already encoded result.
If no span could be extracted, then it will call TryGetSizeHint, and rent a buffer from the pool with a safe initial size.
The method will call TryEncode repeatedly, with a larger and larger buffer, until it returns true, throws, or the buffer reaches the maximum allowed size.
ToSlice
static Slice ToSlice<TEncoder, TValue>(in TValue value)
Returns a Slice that contains the encoded binary form of the value, using the specified encoder
value— Value to encode
Returns: Slice that contains the encoded value.
static SliceOwner ToSlice<TEncoder, TValue>(in TValue value, ArrayPool<byte> pool)
Returns a SliceOwner that contains the encoded binary form of the value, using the specified encoder and a buffer rented from a pool.
value— Value to encodepool— Pool used to rent buffers (use the shared pool ifnull)
Returns: SliceOwner that contains the encoded value stored in buffer rented from the pool.
The caller MUST dispose the result once done, otherwise the rented buffer will not be returned to the pool.