Repository navigation
OnixLabs.Core.IMemoryBinaryConvertible
Defines a type that is convertible to an instance of ReadOnlyMemory<byte>.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
public interface IMemoryBinaryConvertible
{
ReadOnlyMemory<byte> AsReadOnlyMemory();
}IMemoryBinaryConvertible exposes the underlying binary representation of a type as a ReadOnlyMemory<byte>. Unlike ReadOnlySpan<byte>, ReadOnlyMemory<byte> is an ordinary value type: it can be stored in fields, captured by lambdas, and flowed through async methods. This makes it the right choice wherever the span limitations apply — for example, when the binary data needs to be held beyond the current stack frame or passed across an await boundary.
A ReadOnlySpan<byte> can always be derived from a ReadOnlyMemory<byte> via its .Span property when synchronous, stack-based consumption is needed.
IBinaryConvertible composes this interface with ISpanBinaryConvertible. A type that implements IBinaryConvertible also satisfies IMemoryBinaryConvertible, so any API constrained to IMemoryBinaryConvertible 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 memory.
using System;
using OnixLabs.Core;
public sealed class ByteBuffer : IMemoryBinaryConvertible
{
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 ReadOnlyMemory<byte> AsReadOnlyMemory() => value.ToArray();
}ReadOnlyMemory<byte> can be captured and awaited, unlike ReadOnlySpan<byte>.
var buffer = new ByteBuffer([0x01, 0x02, 0x03]);
ReadOnlyMemory<byte> memory = buffer.AsReadOnlyMemory();
await WriteAsync(memory);
static async Task WriteAsync(ReadOnlyMemory<byte> data)
{
// ReadOnlyMemory<byte> can be stored and captured across await; a span cannot.
await Task.Yield();
Console.WriteLine($"Wrote {data.Length} bytes.");
// A span can still be obtained synchronously when needed.
ReadOnlySpan<byte> span = data.Span;
}| Method | Returns | Description |
|---|---|---|
AsReadOnlyMemory() |
ReadOnlyMemory<byte> |
Gets the current instance as a new ReadOnlyMemory<byte>. |
ReadOnlyMemory<byte> AsReadOnlyMemory();Gets the current IMemoryBinaryConvertible instance as a new ReadOnlyMemory<byte> instance.
Returns — The current instance as a new ReadOnlyMemory<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