[API Proposal]: Implement IEnumerator<T> on ref struct enumerators #105276
Open
Description
Background and motivation
We've shipped multiple ref struct enumerators. Previously they couldn't implement IEnumerator<T>
, but now they can. We should have them do so.
API Proposal
namespace System
{
public static class MemoryExtensions
{
public ref struct SpanSplitEnumerator<T>
+ : IEnumerator<Range>
}
public readonly ref struct ReadOnlySpan<T>
{
public ref struct Enumerator
+ : IEnumerator<T>
}
public readonly ref struct Span<T>
{
public ref struct Enumerator
+ : IEnumerator<T>
}
}
namespace System.Text
{
public ref partial struct SpanLineEnumerator
+ : IEnumerator<ReadOnlySpan<char>>
public ref partial struct SpanRuneEnumerator
+ : IEnumerator<Rune>
}
namespace System.Numerics.Tensors
{
public readonly ref struct ReadOnlyTensorSpan<T>
{
public ref struct Enumerator :
+ : IEnumerator<T>
}
public readonly ref struct TensorSpan<T>
{
public ref struct Enumerator :
+ : IEnumerator<T>
}
}
namespace System.Text.RegularExpressions
{
public ref struct ValueMatchEnumerator
+ : IEnumerator<ValueMatch>
public ref struct ValueSplitEnumerator
+ : IEnumerator<Range>
}
Notes:
- All remaining members of the interface not already exposed publicly will be implemented explicitly.
- Where the T is a ref struct, the IEnumerator.Current will throw NotSupportedException.
- Where possible with minimal interruption, IEnumerator.Reset will work, otherwise it'll throw NotSupportedException.
- IDisposable.Dispose nops on all of them.
API Usage
static int Sum<TEnumerator>(TEnumerator enumerator) where TEnumerator : IEnumerator<int>
{
int sum = 0;
while (enumerator.MoveNext()) sum += enumerator.Current;
return sum;
}
Alternative Designs
No response
Risks
No response
Activity