I have one simple goal in life: To write an array of unmanaged structs (T[]) to disc, asyncrounously, without making uneccessary, new allocations.
I can get a ReadonlySpan<T> from my T[]. And I can use MemoryMarshal.AsBytes() to turn that into a ReadonlySpan<byte>.
But...Span can't be used in async methods. Which is why Filestream.WriteAsync() only accepts ReadonlyMemory<byte>, instead.
But...To my knowledge, there's no way to convert from Span<T> to Memory<T>.
And if I try to use Memory<T> instead of Span<T> from the start, I'm also stuck. Because - to my knowledge - there's so way to convert from ReadonlyMemory<T> to ReadonlyMemory<byte> (like you can with MemoryMarshal.ToBytes() and Span<T>).
So...I'm stuck. I cannot figure out any way to take that T[] and write it to disc, without first copying it to a new byte[], using Buffer.BlockCopy() (which is what I'm trying to avoid).
Is there any way to do what I'm trying to do? I started off, assuming that this would be a very common situation - surely all games aren't making unnecessary copies of every struct, before writing it to disc...right?
I don't have much hair left to pull out, folks. Please help, if you have advice. 🙂 Thank you!