Skip to content
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

Merged
merged 16 commits into from
Jul 16, 2024

Conversation

DustinCampbell
Copy link
Member

This change adds First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault, Any and All LINQ-style APIs to PooledArrayBuilder<T>.

@DustinCampbell DustinCampbell requested review from a team as code owners July 10, 2024 19:10
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.
@DustinCampbell
Copy link
Member Author

@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)
Copy link
Member

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?

Copy link
Member Author

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.

@DustinCampbell DustinCampbell merged commit 9ff1e8b into dotnet:main Jul 16, 2024
12 checks passed
@DustinCampbell DustinCampbell deleted the pooledarraybuilder-tweaks branch July 16, 2024 02:11
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants