I have an array of unmanaged structs (T[]) that I’d like to write to disc. What is the most efficient way to do this, without creating any new allocations?
My initial thought was to somehow get a ReadOnlySpan<byte> pointing to the data, and then pass that to FileStream.Write(). However, while the data is entirely blittable, it’s not stored in a byte[], but a T[]. And converting from T[] to byte[] (or from ReadOnlySpan<T> to ReadOnlySpan<byte>) seems to require new allocations, or otherwise be potentially expensive.
Am I missing something? Is there a simple way to write a ReadOnlySpan<T> to disc, without a cast or new allocations?
Thanks for any help!