-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: {ReadOnly}Span(ref T) constructor #67445
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationAs part of the C# 11 work to enable ref fields, it will be safe to create a {ReadOnly}Span from a ref (with an implicit length of 1), because the compiler will track the lifetime of the ref and ensure the created span can't outlive it. Creating spans for a single item is quite common, and we should have public constructors for this situation. This has been proposed by others offline... I agree with it and am just opening the issue for it so we can make forward progress on it. API Proposalnamespace System
{
public ref struct Span<T>
{
+ public Span(ref T reference);
}
public ref struct ReadOnlySpan<T>
{
+ public ReadOnlySpan(ref T reference);
}
} API Usageint i = ...;
CallMethodThatTakesASpan(new ReadOnlySpan<T>(ref i)); Alternative DesignsNo response RisksNo response
|
Examples for all the places in corelib where we create length-1 spans around a single reference are in #67447. |
Believe this should be |
Fixed |
Is this (the |
That's why I'd gone with ref (that, and more generally I really dislike not having to specify that the thing you're passing in is being passed by ref, e.g. I wish you had to write |
This is explicitly supported by the language hence it doesn't pose a safety concern.
That is something that we can consider adding to the language. One simple idea that came to my mind as I was reading this is allowing two syntaxes on parameters:
Could also tweak the second idea to be an error but then you're removing your ability to upgrade existing APIs. |
+1 |
Why is |
That should be |
Editing in haste. |
Yes please. I would love this and it would solve one of the biggest issues I have with |
I've linked this issue in the proposal as well, but just to notify folks in here too - we now have an official proposal on csharplang for |
namespace System;
public partial ref struct Span<T>
{
public Span(ref T reference);
}
public partial ref struct ReadOnlySpan<T>
{
public ReadOnlySpan(in T reference);
} |
(Just noting we should not expose these until the C# safety work is completed.) |
Are there really safety concerns with passing an rvalue to the ReadOnlySpan constructor or is just inefficient? Does it basically just box the value? |
Its not, IMO, a safety concern, but rather one of intent. Basically there are two considerations around In one hand you have cases where it being a On the other hand, you have cases where it being a In the same way, |
The safety work I was referring to above wasn't about rvalues; it was about the C# compiler tracking ref lifetime. If with the C# compiler today we were to expose these constructors, you could do: public Span<int> DoNotDoThis()
{
int i = 0;
return new Span<int>(ref i);
} the compiler would happily compile that, and it would be really bad. |
I don't always peek into the stack, but when I do, I make sure it's into a stack frame that was unwound |
Does this issue include changes in the JIT or VM or just exposing the private/internal constructors + appropriate tests? |
This can't be implemented until the relevant language changes are in. |
@deeprobin this issue just covers the API changes around |
Thank you 👍 |
Background and motivation
As part of the C# 11 work to enable ref fields, it will be safe to create a {ReadOnly}Span from a ref (with an implicit length of 1), because the compiler will track the lifetime of the ref and ensure the created span can't outlive it. Creating spans for a single item is quite common, and we should have public constructors for this situation.
This has been proposed by others offline... I agree with it and am just opening the issue for it so we can make forward progress on it.
cc: @jaredpar, @AaronRobinsonMSFT, @tannergooding
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: