Roslyn: Add Language Support for Pinning Span<T>

Created on 30 Oct 2017  路  5Comments  路  Source: dotnet/roslyn

Today, Span\

```c#
// this is how it works today
Span span = new byte[256];
fixed (byte* pSpan = &span.DangerousGetPinnableReference()) {
...
}

This proposal is to add language support that would allow for the following:
```c#
Span<byte> span = new byte[256];
fixed (byte* pSpan = span) {
   ...
}

Similarly for ReadOnlySpan\

cc: @VSadov, @davidfowl

Area-Compilers Area-Language Design Language-C#

Most helpful comment

I am working on a more general proposal for a pattern-based pinning/fixed.

It should be able to answer the interop question for types such as ImmutableArray, Span, heapable Utf8String, and so on, in a more general way. There is clearly a need.

All 5 comments

@VSadov What do you think about this?

I am working on a more general proposal for a pattern-based pinning/fixed.

It should be able to answer the interop question for types such as ImmutableArray, Span, heapable Utf8String, and so on, in a more general way. There is clearly a need.

YESSS!!

FYI: Proposal for the Pattern based fixed statement - https://github.com/dotnet/csharplang/pull/1100

This has been fixed in https://github.com/dotnet/roslyn/pull/25624

Now spans can expose GetPinnableReference that returns a [readonly]ref to the first element, or nullptr if span is empty.

Was this page helpful?
0 / 5 - 0 ratings