Span<T> constructor #51824
-
The constructor for span currently does not have an overload for [System.CLSCompliant(false)]
public Span (void* pointer, nint length); and public ref T this[nint index] { get; } it only has for [System.CLSCompliant(false)]
public Span (void* pointer, int length); and public ref T this[int index] { get; } This becomes a problem when, for instance, needing to interact with a large block of unmanaged memory such as bytes. Any Thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
See #12221
|
Beta Was this translation helpful? Give feedback.
-
As workaround you can get the reference / pointer out of the span, and work on that with Either public byte M(Span<byte> span, nint idx)
{
ref byte byteRef = ref MemoryMarshal.GetReference(span);
return Unsafe.AddByteOffset(ref byteRef, idx);
} or public unsafe byte M(Span<byte> span, nint idx)
{
// Note: this is only valid for unmanaged memory (or when you can guarantee that the span is contructed out of not-moveable
// memory, i.e. no GC-tracked memory. Otherwise fixed / pinning has to be used.
byte* ptr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
return ptr[idx];
} That way it's also possible to create new spans out of the big-one you start with. So in essence you could create your own specific span-wrapper: internal unsafe ref struct UnmanagedSpanWrapper // naming is hard
{
private readonly Span<byte> _span;
private readonly byte* _ptr;
public UnmanagedSpanWrapper(Span<byte> span)
{
_span = span;
_ptr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
}
public byte this[nint index] // would need some kind of bound-checks too...
{
get => _ptr[index];
set => _ptr[index] = value;
}
public Span<byte> Slice(nint offset, int length)
{
return new Span<byte>(_ptr + offset, length);
}
// ...
} etc. |
Beta Was this translation helpful? Give feedback.
See #12221
nint
/nuint
didn't exists whenSpan<>
was designed. Unfortunately, changingint
tonint
now is a breaking change, and introducing a new Span-like type withnint
means we'll have to add hundreds (or likely thousands) of new overloads across the BCL.