Files

50 lines
1.6 KiB
C#
Raw Permalink Normal View History

2025-08-06 19:16:10 +03:00
using System.Formats.Cbor;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using mROA.Implementation;
2025-08-16 18:30:05 +03:00
namespace mROA.Benchmark;
2025-08-06 19:16:10 +03:00
public static class CborExtensions
{
public static unsafe void WriteToCbor(this RequestId id, CborWriter writer)
{
Span<byte> span = stackalloc byte[16];
MemoryMarshal.Write(span, ref id);
writer.WriteByteString(span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void WriteToCborInline(this RequestId id, CborWriter writer)
{
Span<byte> span = stackalloc byte[16];
MemoryMarshal.Write(span, ref id);
writer.WriteByteString(span);
}
2025-08-07 23:39:42 +03:00
2025-08-06 19:16:10 +03:00
public static void WriteToDest(this RequestId id, Span<byte> destination)
{
MemoryMarshal.Write(destination, ref id);
}
2025-08-07 23:39:42 +03:00
2025-08-06 19:16:10 +03:00
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static unsafe void WriteToCborOpt(this RequestId id, CborWriter writer)
{
Span<byte> span = stackalloc byte[16];
MemoryMarshal.Write(span, ref id);
writer.WriteByteString(span);
}
2025-08-07 23:39:42 +03:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RequestId ReadFromTrueCbor(CborReader reader)
{
var enc = reader.ReadEncodedValue(true);
return MemoryMarshal.Read<RequestId>(enc.Span[1..]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RequestId ReadFromCbor(CborReader reader)
{
var enc = reader.ReadEncodedValue();
return MemoryMarshal.Read<RequestId>(enc.Span[1..]);
}
2025-08-06 19:16:10 +03:00
}