Skip to content

Commit

Permalink
Merge pull request #719 from CommunityToolkit/dev/builder-enumerable
Browse files Browse the repository at this point in the history
Remove unnecessary temporary array allocation
  • Loading branch information
Sergio0694 authored Jun 21, 2023
2 parents d6eb5cc + 9c607b0 commit 34f82fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
.AddArgumentListArguments(
Argument(fieldExpression),
Argument(IdentifierName("value")))),
Block(setterStatements.ToArray()));
Block(setterStatements.AsEnumerable()));

// Prepare the forwarded attributes, if any
ImmutableArray<AttributeListSyntax> forwardedAttributes =
Expand Down
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 34f82fe

Please sign in to comment.