I have following struct:
[StructLayout(LayoutKind.Sequential)]
struct Message
{
int Header;
int Data;
}
and I want to send it over the wire, without allocations (using SendAsync(ReadOnlyMemory<byte>)) call.
How can I get the Memory<byte> from given struct?
I ended up in having Span<byte>, and then got stuck.
var message = new Message {
Header = 1, Data = 3
};
var bytes = MemoryMarshal.AsBytes(
MemoryMarshal.CreateReadOnlySpan(ref message, 1)
);
Is there any way how to put the struct directly into stream without any allocations?
I'm on netcoreapp2.1.
Something like:
Note I'm using
Span<T>here. You can do mostly the same stuff withMemory<T>if needed - but you need a backing array or similar, which will usually require an allocation - if not of the array, then of a customMemoryManager<T>:Essentially, you are very close here:
The trick, though, is to use
MemoryMarshal.Cast<TFrom, byte>(...)to get a a span of bytes.