7
7
8
8
using System ;
9
9
using System . Buffers ;
10
+ using System . Collections ;
11
+ using System . Collections . Generic ;
10
12
using System . Collections . Immutable ;
11
13
using System . Diagnostics . CodeAnalysis ;
12
14
using System . Runtime . CompilerServices ;
@@ -88,6 +90,18 @@ public readonly T[] ToArray()
88
90
return this . writer ! . WrittenSpan . ToArray ( ) ;
89
91
}
90
92
93
+ /// <summary>
94
+ /// Gets an <see cref="IEnumerable{T}"/> instance for the current builder.
95
+ /// </summary>
96
+ /// <returns>An <see cref="IEnumerable{T}"/> instance for the current builder.</returns>
97
+ /// <remarks>
98
+ /// The builder should not be mutated while an enumerator is in use.
99
+ /// </remarks>
100
+ public readonly IEnumerable < T > AsEnumerable ( )
101
+ {
102
+ return this . writer ! ;
103
+ }
104
+
91
105
/// <inheritdoc/>
92
106
public override readonly string ToString ( )
93
107
{
@@ -107,7 +121,7 @@ public void Dispose()
107
121
/// <summary>
108
122
/// A class handling the actual buffer writing.
109
123
/// </summary>
110
- private sealed class Writer : IDisposable
124
+ private sealed class Writer : ICollection < T > , IDisposable
111
125
{
112
126
/// <summary>
113
127
/// The underlying <typeparamref name="T"/> array.
@@ -142,6 +156,9 @@ public ReadOnlySpan<T> WrittenSpan
142
156
get => new ( this . array ! , 0 , this . index ) ;
143
157
}
144
158
159
+ /// <inheritdoc/>
160
+ bool ICollection < T > . IsReadOnly => true ;
161
+
145
162
/// <inheritdoc cref="ImmutableArrayBuilder{T}.Add"/>
146
163
public void Add ( T value )
147
164
{
@@ -173,6 +190,48 @@ public void Dispose()
173
190
}
174
191
}
175
192
193
+ /// <inheritdoc/>
194
+ void ICollection < T > . Clear ( )
195
+ {
196
+ throw new NotSupportedException ( ) ;
197
+ }
198
+
199
+ /// <inheritdoc/>
200
+ bool ICollection < T > . Contains ( T item )
201
+ {
202
+ throw new NotSupportedException ( ) ;
203
+ }
204
+
205
+ /// <inheritdoc/>
206
+ void ICollection < T > . CopyTo ( T [ ] array , int arrayIndex )
207
+ {
208
+ Array . Copy ( this . array ! , 0 , array , arrayIndex , this . index ) ;
209
+ }
210
+
211
+ /// <inheritdoc/>
212
+ IEnumerator < T > IEnumerable < T > . GetEnumerator ( )
213
+ {
214
+ T ? [ ] array = this . array ! ;
215
+ int length = this . index ;
216
+
217
+ for ( int i = 0 ; i < length ; i ++ )
218
+ {
219
+ yield return array [ i ] ! ;
220
+ }
221
+ }
222
+
223
+ /// <inheritdoc/>
224
+ IEnumerator IEnumerable . GetEnumerator ( )
225
+ {
226
+ return ( ( IEnumerable < T > ) this ) . GetEnumerator ( ) ;
227
+ }
228
+
229
+ /// <inheritdoc/>
230
+ bool ICollection < T > . Remove ( T item )
231
+ {
232
+ throw new NotSupportedException ( ) ;
233
+ }
234
+
176
235
/// <summary>
177
236
/// Ensures that <see cref="array"/> has enough free space to contain a given number of new items.
178
237
/// </summary>
0 commit comments