-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
With Span<T>.DangerousCreate gone, there are no APIs to create a Span<T> from a T[,] array (on .NET Standard 2.0) #27690
Comments
MemoryMarshal.CreateSpan is the new method name, and is available in .NET Core 2.1. It's not available in the portable implementation because it's not implementable safely without runtime support (it would have similar problems to what you cite regarding passing around pointers). |
Hi @stephentoub - thank you for your quick reply!
Thanks again for your help! |
Sorry, I misread your question. I thought you were asking about a constructor that takes |
For reference, see https://github.com/dotnet/corefx/issues/26139#issuecomment-357412474. |
@stephentoub no worries, and glad to hear that specific method is possible on .NET Standard 2.0 too then! It also explains why it was indeed working before. And yeah I don't mind if the validation isn't done automatically, even though I can see why that method was removed if you saw many users not using it correctly. Is there a way to actually replicate that somehow? IIRC it was accessing some |
As pointed out in the referenced issue, there isn't an obvious way to replicate this with existing APIs without using reflections, so I'll just go ahead and try to refactor my code where possible. Thanks again! |
I think it is unfortunate the I think the lack of this method is problematic for interoperability with the What if I wanted to create a span for a "section" of an object? Can't (if I can I'm all ears 😄). Managed arrays, strings and native pointers are not the only scope of spans. Maybe I have missed a way to do this. EDIT: Really I would have preferred for the low-level "slow" Span creation to be available |
As a side note for public static class MemoryMarshal
{
public static ReadOnlySpan<T> CreateReadOnlySpan<T>(ref T reference, int length);
public static Span<T> CreateSpan<T>(ref T reference, int length);
} I think it is unfortunate that these methods were designed to work with fast Span and not all possible span versions (incl. slow Span) and hence all runtimes. The lack of |
Hello, I'm working on a .NET Standard 2.0 library (this one) and up until now I've used the old
Span<T>
APIs from theSystem.Memory
assembly version 4.0.1.0 (from what I can see by Ctrl+click onSpan<T>
), and everything worked great.I went back to update the library with the latest NuGet packages, and noticed that (according to #24562) the
Span<T>.DangerousCreate(object, ref T, int)
API has been removed. I tried to look for it underMemoryMarshal
, but it isn't there at least on .NET Standard 2.0.As a result, simple methods like these ones are not working anymore:
Same for similar methods to extract a chunk of contiguous rows from a
T[,]
array.I'm using 2D arrays all over this library, and all the current
Span<T>
APIs I see only work with classic 1D arrays. I can't use the constructor that takes avoid*
as if I used something likenew Span<T>(Unsafe.AsPointer(ref array[0, 0]))
the GC would wouldn't be able to track theref
once it gets forwarded as a pointer only.I understand that the original
DangerousCreate(object, ref T, int)
had to me moved/removed and that some users were not using that API correctly, but is there some other way to still do what that API did now that it's gone? I mean, I can't see a way to properly work with 2D arrays and spans now that the API in question is no longer available.Thanks! 😄
The text was updated successfully, but these errors were encountered: