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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -880,30 +880,20 @@ public static T SingleOrDefault<T, TArg>(this IReadOnlyList<T> list, TArg arg, F

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.

{
public Enumerator<T> GetEnumerator() => new(list);

IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public ReverseEnumerable<T> Reverse() => new(list);
}

public struct Enumerator<T>(IReadOnlyList<T> list) : IEnumerator<T>
public ref struct Enumerator<T>(IReadOnlyList<T> list)
{
private readonly IReadOnlyList<T> _list = list;
private int _index = 0;
private T _current = default!;

public readonly T Current => _current!;

readonly object IEnumerator.Current => _current!;

public readonly void Dispose()
{
}
public readonly T Current => _current;

public bool MoveNext()
{
Expand All @@ -924,24 +914,18 @@ public void Reset()
}
}

public readonly struct ReverseEnumerable<T>(IReadOnlyList<T> list) : IEnumerable<T>
public readonly ref struct ReverseEnumerable<T>(IReadOnlyList<T> list)
{
public ReverseEnumerator<T> GetEnumerator() => new(list);

IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public struct ReverseEnumerator<T> : IEnumerator<T>
public ref struct ReverseEnumerator<T>
{
private readonly IReadOnlyList<T> _list;
private int _index;
private T _current;

public readonly T Current => _current!;

readonly object IEnumerator.Current => _current!;
public readonly T Current => _current;

public ReverseEnumerator(IReadOnlyList<T> list)
{
Expand All @@ -950,10 +934,6 @@ public ReverseEnumerator(IReadOnlyList<T> list)
_current = default!;
}

public readonly void Dispose()
{
}

public bool MoveNext()
{
if (_index >= 0)
Expand Down