Repository navigation
OnixLabs.Core.IBinaryConvertible
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
|
public interface IBinaryConvertible : ISpanBinaryConvertible, IMemoryBinaryConvertible;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. BecauseReadOnlySpan<byte>is aref struct, it cannot be stored in a field, captured by a lambda, or used inasyncmethods — useAsReadOnlyMemory()in those scenarios.
Within the wider library, ICryptoPrimitive<T> in OnixLabs.Security.Cryptography extends IBinaryConvertible, so all cryptographic primitives expose a consistent binary surface.
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();
}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.");
}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>. |
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.
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.
ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions