@@ -244,6 +244,59 @@ public Span<T> GetSpan(int sizeHint = 0)
244
244
return this . array . AsSpan ( this . index ) ;
245
245
}
246
246
247
+ /// <summary>
248
+ /// Gets an <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.
249
+ /// </summary>
250
+ /// <returns>An <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.</returns>
251
+ /// <exception cref="ObjectDisposedException">Thrown when the buffer in use has already been disposed.</exception>
252
+ /// <remarks>
253
+ /// This method is meant to be used when working with APIs that only accept an array as input, and should be used with caution.
254
+ /// In particular, the returned array is rented from an array pool, and it is responsibility of the caller to ensure that it's
255
+ /// not used after the current <see cref="ArrayPoolBufferWriter{T}"/> instance is disposed. Doing so is considered undefined
256
+ /// behavior, as the same array might be in use within another <see cref="ArrayPoolBufferWriter{T}"/> instance.
257
+ /// </remarks>
258
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
259
+ public ArraySegment < T > DangerousGetArray ( )
260
+ {
261
+ T [ ] ? array = this . array ;
262
+
263
+ if ( array is null )
264
+ {
265
+ ThrowObjectDisposedException ( ) ;
266
+ }
267
+
268
+ return new ( array ! , 0 , this . index ) ;
269
+ }
270
+
271
+ /// <inheritdoc/>
272
+ public void Dispose ( )
273
+ {
274
+ T [ ] ? array = this . array ;
275
+
276
+ if ( array is null )
277
+ {
278
+ return ;
279
+ }
280
+
281
+ this . array = null ;
282
+
283
+ this . pool . Return ( array ) ;
284
+ }
285
+
286
+ /// <inheritdoc/>
287
+ public override string ToString ( )
288
+ {
289
+ // See comments in MemoryOwner<T> about this
290
+ if ( typeof ( T ) == typeof ( char ) &&
291
+ this . array is char [ ] chars )
292
+ {
293
+ return new ( chars , 0 , this . index ) ;
294
+ }
295
+
296
+ // Same representation used in Span<T>
297
+ return $ "CommunityToolkit.HighPerformance.Buffers.ArrayPoolBufferWriter<{ typeof ( T ) } >[{ this . index } ]";
298
+ }
299
+
247
300
/// <summary>
248
301
/// Ensures that <see cref="array"/> has enough free space to contain a given number of new items.
249
302
/// </summary>
@@ -296,35 +349,6 @@ private void ResizeBuffer(int sizeHint)
296
349
this . pool . Resize ( ref this . array , ( int ) minimumSize ) ;
297
350
}
298
351
299
- /// <inheritdoc/>
300
- public void Dispose ( )
301
- {
302
- T [ ] ? array = this . array ;
303
-
304
- if ( array is null )
305
- {
306
- return ;
307
- }
308
-
309
- this . array = null ;
310
-
311
- this . pool . Return ( array ) ;
312
- }
313
-
314
- /// <inheritdoc/>
315
- public override string ToString ( )
316
- {
317
- // See comments in MemoryOwner<T> about this
318
- if ( typeof ( T ) == typeof ( char ) &&
319
- this . array is char [ ] chars )
320
- {
321
- return new ( chars , 0 , this . index ) ;
322
- }
323
-
324
- // Same representation used in Span<T>
325
- return $ "CommunityToolkit.HighPerformance.Buffers.ArrayPoolBufferWriter<{ typeof ( T ) } >[{ this . index } ]";
326
- }
327
-
328
352
/// <summary>
329
353
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the requested count is negative.
330
354
/// </summary>
0 commit comments