Skip to content

Commit d60e67c

Browse files
committed
Added [Memory|Span]Owner<T>.DangerousGetArray
1 parent c6c6602 commit d60e67c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Microsoft.Toolkit.HighPerformance/Buffers/MemoryOwner{T}.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ public ref T DangerousGetReference()
208208
return ref array!.DangerousGetReferenceAt(this.start);
209209
}
210210

211+
/// <summary>
212+
/// Gets an <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.
213+
/// </summary>
214+
/// <returns>An <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.</returns>
215+
/// <remarks>
216+
/// This method is meant to be used when working with APIs that only accept an array as input, and should be used with caution.
217+
/// In particular, the returned array is rented from an array pool, and it is responsibility of the caller to ensure that it's
218+
/// not used after the current <see cref="MemoryOwner{T}"/> instance is disposed. Doing so is considered undefined behavior,
219+
/// as the same array might be in use within another <see cref="MemoryOwner{T}"/> instance.
220+
/// </remarks>
221+
[Pure]
222+
public ArraySegment<T> DangerousGetArray()
223+
{
224+
T[]? array = this.array;
225+
226+
if (array is null)
227+
{
228+
ThrowObjectDisposedException();
229+
}
230+
231+
return new ArraySegment<T>(array!, this.start, this.length);
232+
}
233+
211234
/// <summary>
212235
/// Slices the buffer currently in use and returns a new <see cref="MemoryOwner{T}"/> instance.
213236
/// </summary>

Microsoft.Toolkit.HighPerformance/Buffers/SpanOwner{T}.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ public ref T DangerousGetReference()
157157
return ref this.array.DangerousGetReference();
158158
}
159159

160+
/// <summary>
161+
/// Gets an <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.
162+
/// </summary>
163+
/// <returns>An <see cref="ArraySegment{T}"/> instance wrapping the underlying <typeparamref name="T"/> array in use.</returns>
164+
/// <remarks>
165+
/// This method is meant to be used when working with APIs that only accept an array as input, and should be used with caution.
166+
/// In particular, the returned array is rented from an array pool, and it is responsibility of the caller to ensure that it's
167+
/// not used after the current <see cref="SpanOwner{T}"/> instance is disposed. Doing so is considered undefined behavior,
168+
/// as the same array might be in use within another <see cref="SpanOwner{T}"/> instance.
169+
/// </remarks>
170+
[Pure]
171+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172+
public ArraySegment<T> DangerousGetArray()
173+
{
174+
return new ArraySegment<T>(array!, 0, this.length);
175+
}
176+
160177
/// <summary>
161178
/// Implements the duck-typed <see cref="IDisposable.Dispose"/> method.
162179
/// </summary>

0 commit comments

Comments
 (0)