-
Notifications
You must be signed in to change notification settings - Fork 199
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
Flesh out PooledArrayBuilder<T>
a bit
#10606
Flesh out PooledArrayBuilder<T>
a bit
#10606
Conversation
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/PooledArrayBuilder`1.cs
Show resolved
Hide resolved
This change adds `First()`, `FirstOrDefault()`, `Last()`, `LastOrDefault()`, Single()`, and `SingleOrDefault()` extension methods for `IReadOnlyList<T>` along with a comprehensive set of overloads. In addition, I've added an `AsEnumerable()` that returns a struct that wraps `IReadOnlyList<T>` with a struct enumerator. This makes it easy to foreach over an `IReadOnlyList<T>` without allocating.
@333fred: This is ready for another look. |
@@ -880,30 +880,20 @@ public static T SingleOrDefault<T>(this IReadOnlyList<T> list, Func<T, bool> pre | |||
|
|||
public static Enumerable<T> AsEnumerable<T>(this IReadOnlyList<T> list) => new(list); | |||
|
|||
public readonly struct Enumerable<T>(IReadOnlyList<T> list) : IEnumerable<T> | |||
public readonly ref struct Enumerable<T>(IReadOnlyList<T> list) |
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.
What's the benefit of changing these to ref struct
types?
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.
It came out of @jjonescz's comment on a different PR here. Essentially, there's no reason to ever want to box these types. So, it's just a little extra protection. I don't think there's really much value in this change, but it was simple enough. It's just a little extra protection. After all, if the user wants to pass an IReadOnlyList<T>
to an IEnumerable<T>
, they can just pass it.
This change adds
First
,FirstOrDefault
,Last
,LastOrDefault
,Single
,SingleOrDefault
,Any
andAll
LINQ-style APIs toPooledArrayBuilder<T>
.