Repository navigation
OnixLabs.Core.ISpanBinaryConvertible
Defines a type that is convertible to an instance of ReadOnlySpan<byte>.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
public interface ISpanBinaryConvertible
{
ReadOnlySpan<byte> AsReadOnlySpan();
}ISpanBinaryConvertible exposes the underlying binary representation of a type as a ReadOnlySpan<byte>. A span is a stack-only, non-allocating window over contiguous memory, making it the most efficient representation for immediate, synchronous reads — no heap allocation is required beyond whatever the implementer already holds.
Because ReadOnlySpan<byte> is a ref struct, it carries important limitations: it cannot be stored in a field, captured by a lambda, boxed, or used across await boundaries. In those scenarios, use IMemoryBinaryConvertible instead, whose AsReadOnlyMemory() returns a heap-storable ReadOnlyMemory<byte> from which a span can always be obtained via its Span property.
IBinaryConvertible composes this interface with IMemoryBinaryConvertible. A type that implements IBinaryConvertible also satisfies ISpanBinaryConvertible, so any API constrained to ISpanBinaryConvertible accepts any binary-convertible type in the library.
Note Implementers typically hold an immutable backing
byte[]and return a view over a defensive copy so that the caller cannot mutate the original data through the returned span.
using System;
using OnixLabs.Core;
public sealed class ByteBuffer : ISpanBinaryConvertible
{
private readonly byte[] value;
public ByteBuffer(ReadOnlySpan<byte> value)
{
this.value = value.ToArray();
}
// Returns a view over a defensive copy; the caller cannot corrupt backing data.
public ReadOnlySpan<byte> AsReadOnlySpan() => value.ToArray();
}An API that only reads bytes synchronously can accept ISpanBinaryConvertible rather than the broader IBinaryConvertible, making its requirements explicit and keeping it compatible with any binary-convertible type.
static int CountNonZero(ISpanBinaryConvertible source)
{
ReadOnlySpan<byte> bytes = source.AsReadOnlySpan();
int count = 0;
foreach (byte b in bytes)
{
if (b != 0) count++;
}
return count;
}| Method | Returns | Description |
|---|---|---|
AsReadOnlySpan() |
ReadOnlySpan<byte> |
Gets the current instance as a new ReadOnlySpan<byte>. |
ReadOnlySpan<byte> AsReadOnlySpan();Gets the current ISpanBinaryConvertible instance as a new ReadOnlySpan<byte> instance.
Returns — The current instance as a new ReadOnlySpan<byte>.
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