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
c# feature: new method AsSpan() on RepeatedField<T> #6538
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -543,6 +543,20 @@ public override string ToString() | |
} | ||
} | ||
|
||
#if NETSTANDARD2_0 | ||
/// <summary> | ||
/// Creates a new span over the RepeatedField. This is the most | ||
/// efficient way to read and write values to the RepeatedField because | ||
/// there are no internal null or bounds checks. You are responsible | ||
/// for ensuring no null values are written to the span. | ||
/// </summary> | ||
/// <returns>Span representation of the RepeatedField</returns> | ||
public Span<T> AsSpan() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's say I wanted to populate the repeated field with 1 million values. How do I resize the empty RepeatedField to the right size using the public API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have another PR for that, but I will need to modify it slightly. It preallocates the array but doesn't increase count, so, as you point out, it isn't going to work. It will still be nice to have for reads. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to first know what it the full API you're proposing. Isolated like this, it doesn't make much sense. |
||
{ | ||
return array.AsSpan(0, count); | ||
} | ||
#endif | ||
|
||
#region Explicit interface implementation for IList and ICollection. | ||
bool IList.IsFixedSize => false; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the lack of null check is dangerous and relying on users to always do the right thing is also dangerous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++ allows you to memcpy into RepeatedField. But that's C++. I suppose C# users expect the null checks and such.