Skip to content

Interoptopus and DisableRuntimeMarshalling #293

Description

@OleRoss

I want to use DisableRuntimeMarshallingAttribute in my apps. However, when I have rust Vec<T> then code like this is generated:

/// Gets the element at the given index.
public unsafe ushort this[int i]
{
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    get
    {
        if (i >= Count) throw new IndexOutOfRangeException();
        if (_ptr == IntPtr.Zero) throw new NullReferenceException();
        return Marshal.PtrToStructure<ushort>(new IntPtr(_ptr.ToInt64() + i * sizeof(ushort)));
    }
}

The Marshal.PtrToStructure raises the following warning:

CA1421: 'System.Runtime.InteropServices.Marshal.PtrToStructure(nint)' uses runtime marshaling even when 'DisableRuntimeMarshallingAttribute' is applied. Use features like 'sizeof' and pointers directly to ensure accurate results.

Is there a reason you dont generate the following for normal, blittable structs?

return ((ushort*)_ptr.ToPointer())[i];

I would be happy to provide a PR if that's a change you would be comfortable with

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions