IBinaryFormattable<TSelf>
Represents an object that can be converted to and restored from the binary representation.
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Buffers
{
public interface IBinaryFormattable<[System.Runtime.CompilerServices.Nullable(1)] TSelf> where TSelf : IBinaryFormattable<TSelf>
{
int Size { get; }
void Format(ref SpanWriter<byte> output);
[return: System.Runtime.CompilerServices.Nullable(1)]
TSelf Parse(ref SpanReader<byte> input);
unsafe bool TryParse(ReadOnlySpan<byte> input, [System.Runtime.CompilerServices.Nullable(2)] [NotNullWhen(true)] out TSelf result)
{
int size = ((TSelf*)input.Length)->Size;
if ((int) >= size) {
TSelf val = ((IBinaryFormattable<TSelf>)(&result)).Parse(input);
*(TSelf*)(long)(IntPtr)(void*) = val;
return true;
}
result = default(TSelf);
return false;
}
MemoryOwner<byte> Format([System.Runtime.CompilerServices.Nullable(1)] TSelf value, [System.Runtime.CompilerServices.Nullable(2)] MemoryAllocator<byte> allocator = null)
{
MemoryOwner<byte> result = MemoryAllocator.Invoke<byte>(length: ((IBinaryFormattable<TSelf>)allocator).Size, allocator: (MemoryAllocator<byte>), exactSize: true);
SpanWriter<byte> output = new SpanWriter<byte>(result.Memory.Span);
value.Format(ref output);
return result;
}
void Format([System.Runtime.CompilerServices.Nullable(1)] TSelf value, Span<byte> output)
{
SpanWriter<byte> output2 = new SpanWriter<byte>(output);
value.Format(ref output2);
}
unsafe bool TryFormat([System.Runtime.CompilerServices.Nullable(1)] TSelf value, Span<byte> output)
{
int size = ((TSelf*)output.Length)->Size;
if ((int) >= size) {
Span<byte> output2 = output;
((IBinaryFormattable<TSelf>)).Format(value, output2);
return true;
}
return false;
}
[return: System.Runtime.CompilerServices.Nullable(1)]
TSelf Parse(ReadOnlySpan<byte> input)
{
SpanReader<byte> input2 = new SpanReader<byte>(input);
return ((IBinaryFormattable<TSelf>)).Parse(ref input2);
}
[return: System.Runtime.CompilerServices.Nullable(1)]
unsafe TSelf Parse([In] [IsReadOnly] ref ReadOnlySequence<byte> input)
{
ReadOnlySpan<byte> firstSpan = input.FirstSpan;
int size = ((TSelf*)firstSpan.Length)->Size;
if ((int) < size)
return ((IBinaryFormattable<TSelf>)).<Parse>g__ParseSlow|9_0(ref input);
firstSpan = input.FirstSpan;
int size2 = ((TSelf*)null)->Size;
ReadOnlySpan<byte> input2 = ((ReadOnlySpan<byte>)).Slice((int)(&firstSpan), size2);
return ((IBinaryFormattable<TSelf>)).Parse(input2);
}
}
}