Skip to content

Commit

Permalink
Add ImmutableArrayBuilder<T>.AsEnumerable()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jun 20, 2023
1 parent d6eb5cc commit bcb327d
Showing 1 changed file with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -88,6 +90,18 @@ public readonly T[] ToArray()
return this.writer!.WrittenSpan.ToArray();
}

/// <summary>
/// Gets an <see cref="IEnumerable{T}"/> instance for the current builder.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> instance for the current builder.</returns>
/// <remarks>
/// The builder should not be mutated while an enumerator is in use.
/// </remarks>
public readonly IEnumerable<T> AsEnumerable()
{
return this.writer!;
}

/// <inheritdoc/>
public override readonly string ToString()
{
Expand All @@ -107,7 +121,7 @@ public void Dispose()
/// <summary>
/// A class handling the actual buffer writing.
/// </summary>
private sealed class Writer : IDisposable
private sealed class Writer : ICollection<T>, IDisposable
{
/// <summary>
/// The underlying <typeparamref name="T"/> array.
Expand Down Expand Up @@ -142,6 +156,9 @@ public ReadOnlySpan<T> WrittenSpan
get => new(this.array!, 0, this.index);
}

/// <inheritdoc/>
bool ICollection<T>.IsReadOnly => true;

/// <inheritdoc cref="ImmutableArrayBuilder{T}.Add"/>
public void Add(T value)
{
Expand Down Expand Up @@ -173,6 +190,48 @@ public void Dispose()
}
}

/// <inheritdoc/>
void ICollection<T>.Clear()
{
throw new NotSupportedException();
}

/// <inheritdoc/>
bool ICollection<T>.Contains(T item)
{
throw new NotSupportedException();
}

/// <inheritdoc/>
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
Array.Copy(this.array!, 0, array, arrayIndex, this.index);
}

/// <inheritdoc/>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
T?[] array = this.array!;
int length = this.index;

for (int i = 0; i < length; i++)
{
yield return array[i]!;
}
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}

/// <inheritdoc/>
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}

/// <summary>
/// Ensures that <see cref="array"/> has enough free space to contain a given number of new items.
/// </summary>
Expand Down

0 comments on commit bcb327d

Please sign in to comment.