Proper way to get a mutable struct for Memory<byte> / Span<byte>?

社会主义新天地 提交于 2019-12-30 07:02:10

问题


For a network protocol implementation I want to make use of the new Memory and Span classes to achieve zero-copy of the buffer while accessing the data through a struct.

I have the following contrived example:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Data
{
    public int IntValue;
    public short ShortValue;
    public byte ByteValue;
}

static void Prepare()
{
    var buffer = new byte[1024];
    var dSpan = MemoryMarshal.Cast<byte, Data>(buffer);
    ref var d = ref dSpan[0];

    d.ByteValue = 1;
    d.ShortValue = (2 << 8) + 3;
    d.IntValue = (4 << 24) + (5 << 16) + (6 << 8) + 7;
}

The result is that buffer is filled with 7, 6, 5, 4, 3, 2, 1, which is as desired, but I can hardly imagine that MemoryMarshal.Cast is the only way (bar anything requiring the unsafe keyword) to do this. I tried some other methods, but I can't figure out how to use them with either a ref struct (which can't be used as generic type argument) or how to get a struct that's in the actual buffer and not a copy (on which any mutations made are not reflected in the buffer).

Is there a some easier way to get this mutable struct from the buffer?


回答1:


Oof. It looks like MemoryMarshal.Cast is what used to be the NonPortableCast extension method (from: this commit), in which case - yes, that's the appropriate way to thunk between layouts of spans, most commonly (but not exclusively) like in this case - between byte and some struct.



来源:https://stackoverflow.com/questions/50546782/proper-way-to-get-a-mutable-struct-for-memorybyte-spanbyte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!