Skip to content

Commit 60e17b4

Browse files
committed
Added [ReadOnly]Span.[Try]CopyTo RefEnumerable APIs
1 parent a2ecb5f commit 60e17b4

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlySpanExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,33 @@ public static int GetDjb2HashCode<T>(this ReadOnlySpan<T> span)
380380

381381
return SpanHelper.GetDjb2HashCode(ref r0, length);
382382
}
383+
384+
/// <summary>
385+
/// Copies the contents of a given <see cref="ReadOnlySpan{T}"/> into destination <see cref="RefEnumerable{T}"/> instance.
386+
/// </summary>
387+
/// <typeparam name="T">The type of items in the input <see cref="ReadOnlySpan{T}"/> instance.</typeparam>
388+
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> instance.</param>
389+
/// <param name="destination">The <see cref="RefEnumerable{T}"/> instance to copy items into.</param>
390+
/// <exception cref="ArgumentException">
391+
/// Thrown when the destination <see cref="RefEnumerable{T}"/> is shorter than the source <see cref="ReadOnlySpan{T}"/>.
392+
/// </exception>
393+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
394+
public static void CopyTo<T>(this ReadOnlySpan<T> span, RefEnumerable<T> destination)
395+
{
396+
destination.CopyFrom(span);
397+
}
398+
399+
/// <summary>
400+
/// Attempts to copy the contents of a given <see cref="ReadOnlySpan{T}"/> into destination <see cref="RefEnumerable{T}"/> instance.
401+
/// </summary>
402+
/// <typeparam name="T">The type of items in the input <see cref="ReadOnlySpan{T}"/> instance.</typeparam>
403+
/// <param name="span">The input <see cref="ReadOnlySpan{T}"/> instance.</param>
404+
/// <param name="destination">The <see cref="RefEnumerable{T}"/> instance to copy items into.</param>
405+
/// <returns>Whether or not the operation was successful.</returns>
406+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
407+
public static bool TryCopyTo<T>(this ReadOnlySpan<T> span, RefEnumerable<T> destination)
408+
{
409+
return destination.TryCopyFrom(span);
410+
}
383411
}
384412
}

Microsoft.Toolkit.HighPerformance/Extensions/SpanExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,34 @@ public static int GetDjb2HashCode<T>(this Span<T> span)
271271
return SpanHelper.GetDjb2HashCode(ref r0, length);
272272
}
273273

274+
/// <summary>
275+
/// Copies the contents of a given <see cref="Span{T}"/> into destination <see cref="RefEnumerable{T}"/> instance.
276+
/// </summary>
277+
/// <typeparam name="T">The type of items in the input <see cref="Span{T}"/> instance.</typeparam>
278+
/// <param name="span">The input <see cref="Span{T}"/> instance.</param>
279+
/// <param name="destination">The <see cref="RefEnumerable{T}"/> instance to copy items into.</param>
280+
/// <exception cref="ArgumentException">
281+
/// Thrown when the destination <see cref="RefEnumerable{T}"/> is shorter than the source <see cref="Span{T}"/>.
282+
/// </exception>
283+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
284+
public static void CopyTo<T>(this Span<T> span, RefEnumerable<T> destination)
285+
{
286+
destination.CopyFrom(span);
287+
}
288+
289+
/// <summary>
290+
/// Attempts to copy the contents of a given <see cref="Span{T}"/> into destination <see cref="RefEnumerable{T}"/> instance.
291+
/// </summary>
292+
/// <typeparam name="T">The type of items in the input <see cref="Span{T}"/> instance.</typeparam>
293+
/// <param name="span">The input <see cref="Span{T}"/> instance.</param>
294+
/// <param name="destination">The <see cref="RefEnumerable{T}"/> instance to copy items into.</param>
295+
/// <returns>Whether or not the operation was successful.</returns>
296+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
297+
public static bool TryCopyTo<T>(this Span<T> span, RefEnumerable<T> destination)
298+
{
299+
return destination.TryCopyFrom(span);
300+
}
301+
274302
/// <summary>
275303
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the given reference is out of range.
276304
/// </summary>

0 commit comments

Comments
 (0)