using MemoryPack; using System.Buffers; namespace Daybreak.API.Interop; public static class MemoryPackExtensions { /// Serialize into . /// Number of bytes written. public static int SerializeToSpan(T value, Span destination) { var dummy = DummyBufferWriter.Instance; using var state = MemoryPackWriterOptionalStatePool.Rent(null); var writer = new MemoryPackWriter(ref dummy, destination, state); writer.WriteValue(value); writer.Flush(); return writer.WrittenCount; } private sealed class DummyBufferWriter : IBufferWriter { public static readonly DummyBufferWriter Instance = new(); public void Advance(int count) { /* ignore – we never overflow destination */ } public Memory GetMemory(int sizeHint = 0) => throw new NotSupportedException(); public Span GetSpan(int sizeHint = 0) => throw new NotSupportedException(); } }