Skip to content

OnixLabs.Core.IBinaryConvertible

matthew edited this page May 27, 2026 · 1 revision

IBinaryConvertible Interface

Defines a type that is convertible to a ReadOnlySpan<byte> or a ReadOnlyMemory<byte>.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Implements ISpanBinaryConvertible, IMemoryBinaryConvertible

Definition

public interface IBinaryConvertible : ISpanBinaryConvertible, IMemoryBinaryConvertible;

Remarks

IBinaryConvertible is a composition interface that inherits from both ISpanBinaryConvertible and IMemoryBinaryConvertible. It declares no members of its own; a type that implements it must provide both AsReadOnlySpan() and AsReadOnlyMemory(), and thereby advertises that it supports the full binary-conversion surface.

This is the interface most types in the library implement, because both representations are cheap to derive from the same backing bytes. The two narrower interfaces exist for cases where only one representation is meaningful, or where an API wants to express the minimum capability it requires — for instance, an API that only performs synchronous reads can constrain its parameter to ISpanBinaryConvertible rather than the broader IBinaryConvertible.

Note Implementers typically hold an immutable backing byte[] and return views over a defensive copy so that the original data cannot be mutated through the returned span or memory. Because ReadOnlySpan<byte> is a ref struct, it cannot be stored in a field, captured by a lambda, or used in async methods — use AsReadOnlyMemory() in those scenarios.

Within the wider library, ICryptoPrimitive<T> in OnixLabs.Security.Cryptography extends IBinaryConvertible, so all cryptographic primitives expose a consistent binary surface.

Examples

Implementing IBinaryConvertible

The type below wraps an immutable byte buffer and returns defensive copies, so neither the span nor the memory view can mutate the backing data.

using System;
using OnixLabs.Core;

public sealed class ByteBuffer : IBinaryConvertible
{
    private readonly byte[] value;

    public ByteBuffer(ReadOnlySpan<byte> value)
    {
        this.value = value.ToArray();
    }

    public ReadOnlySpan<byte> AsReadOnlySpan() => value.ToArray();

    public ReadOnlyMemory<byte> AsReadOnlyMemory() => value.ToArray();
}

Consuming both representations

var buffer = new ByteBuffer([0x01, 0x02, 0x03]);

// Span: efficient, synchronous, immediate consumption.
ReadOnlySpan<byte> span = buffer.AsReadOnlySpan();
int length = span.Length; // 3

// Memory: storable and safe to flow across async boundaries.
ReadOnlyMemory<byte> memory = buffer.AsReadOnlyMemory();
await WriteAsync(memory);

static async Task WriteAsync(ReadOnlyMemory<byte> data)
{
    // ReadOnlyMemory<byte> can be captured across await; a span cannot.
    await Task.Yield();
    Console.WriteLine($"Wrote {data.Length} bytes.");
}

Methods

Inherited from ISpanBinaryConvertible and IMemoryBinaryConvertible:

Method Returns Description
AsReadOnlySpan() ReadOnlySpan<byte> Gets the current instance as a new ReadOnlySpan<byte>.
AsReadOnlyMemory() ReadOnlyMemory<byte> Gets the current instance as a new ReadOnlyMemory<byte>.

AsReadOnlySpan()

ReadOnlySpan<byte> AsReadOnlySpan();

Gets the current instance as a new ReadOnlySpan<byte> instance.

Returns — The current instance as a new ReadOnlySpan<byte>.

Inherited from ISpanBinaryConvertible.


AsReadOnlyMemory()

ReadOnlyMemory<byte> AsReadOnlyMemory();

Gets the current instance as a new ReadOnlyMemory<byte> instance.

Returns — The current instance as a new ReadOnlyMemory<byte>.

Inherited from IMemoryBinaryConvertible.

See Also

ONIXLabs .NET Library


Clone this wiki locally