Skip to content

Commit db31e98

Browse files
committed
Added [ReadOnly]Memory<T>.AsBytes extensions
1 parent 79665cd commit db31e98

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

Microsoft.Toolkit.HighPerformance/Extensions/MemoryExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
1616
/// </summary>
1717
public static class MemoryExtensions
1818
{
19+
/// <summary>
20+
/// Casts a <see cref="Memory{T}"/> of one primitive type <typeparamref name="T"/> to <see cref="Memory{T}"/> of bytes.
21+
/// </summary>
22+
/// <typeparam name="T">The type if items in the source <see cref="Memory{T}"/>.</typeparam>
23+
/// <param name="memory">The source <see cref="Memory{T}"/>, of type <typeparamref name="T"/>.</param>
24+
/// <returns>A <see cref="Memory{T}"/> of bytes.</returns>
25+
/// <exception cref="OverflowException">
26+
/// Thrown if the <see cref="Memory{T}.Length"/> property of the new <see cref="Memory{T}"/> would exceed <see cref="int.MaxValue"/>.
27+
/// </exception>
28+
[Pure]
29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30+
public static Memory<byte> AsBytes<T>(this Memory<T> memory)
31+
where T : unmanaged
32+
{
33+
return MemoryMarshal.AsMemory(((ReadOnlyMemory<T>)memory).Cast<T, byte>());
34+
}
35+
1936
/// <summary>
2037
/// Casts a <see cref="Memory{T}"/> of one primitive type <typeparamref name="TFrom"/> to another primitive type <typeparamref name="TTo"/>.
2138
/// </summary>

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlyMemoryExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
1919
/// </summary>
2020
public static class ReadOnlyMemoryExtensions
2121
{
22+
/// <summary>
23+
/// Casts a <see cref="ReadOnlyMemory{T}"/> of one primitive type <typeparamref name="T"/> to <see cref="ReadOnlyMemory{T}"/> of bytes.
24+
/// </summary>
25+
/// <typeparam name="T">The type if items in the source <see cref="ReadOnlyMemory{T}"/>.</typeparam>
26+
/// <param name="memory">The source <see cref="ReadOnlyMemory{T}"/>, of type <typeparamref name="T"/>.</param>
27+
/// <returns>A <see cref="ReadOnlyMemory{T}"/> of bytes.</returns>
28+
/// <exception cref="OverflowException">
29+
/// Thrown if the <see cref="ReadOnlyMemory{T}.Length"/> property of the new <see cref="ReadOnlyMemory{T}"/> would exceed <see cref="int.MaxValue"/>.
30+
/// </exception>
31+
[Pure]
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public static ReadOnlyMemory<byte> AsBytes<T>(this ReadOnlyMemory<T> memory)
34+
where T : unmanaged
35+
{
36+
return Cast<T, byte>(memory);
37+
}
38+
2239
/// <summary>
2340
/// Casts a <see cref="ReadOnlyMemory{T}"/> of one primitive type <typeparamref name="TFrom"/> to another primitive type <typeparamref name="TTo"/>.
2441
/// </summary>

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlySpanExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,10 @@ public static int Count<T>(this ReadOnlySpan<T> span, T value)
176176

177177
/// <summary>
178178
/// Casts a <see cref="ReadOnlySpan{T}"/> of one primitive type <typeparamref name="T"/> to <see cref="ReadOnlySpan{T}"/> of bytes.
179-
/// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.
180179
/// </summary>
181180
/// <typeparam name="T">The type if items in the source <see cref="ReadOnlySpan{T}"/>.</typeparam>
182181
/// <param name="span">The source slice, of type <typeparamref name="T"/>.</param>
183182
/// <returns>A <see cref="ReadOnlySpan{T}"/> of bytes.</returns>
184-
/// <exception cref="ArgumentException">
185-
/// Thrown when <typeparamref name="T"/> contains pointers.
186-
/// </exception>
187183
/// <exception cref="OverflowException">
188184
/// Thrown if the <see cref="ReadOnlySpan{T}.Length"/> property of the new <see cref="ReadOnlySpan{T}"/> would exceed <see cref="int.MaxValue"/>.
189185
/// </exception>

Microsoft.Toolkit.HighPerformance/Extensions/SpanExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ public static ref T DangerousGetReferenceAt<T>(this Span<T> span, int i)
5050

5151
/// <summary>
5252
/// Casts a <see cref="Span{T}"/> of one primitive type <typeparamref name="T"/> to <see cref="Span{T}"/> of bytes.
53-
/// That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.
5453
/// </summary>
5554
/// <typeparam name="T">The type if items in the source <see cref="Span{T}"/>.</typeparam>
5655
/// <param name="span">The source slice, of type <typeparamref name="T"/>.</param>
5756
/// <returns>A <see cref="Span{T}"/> of bytes.</returns>
58-
/// <exception cref="ArgumentException">
59-
/// Thrown when <typeparamref name="T"/> contains pointers.
60-
/// </exception>
6157
/// <exception cref="OverflowException">
6258
/// Thrown if the <see cref="Span{T}.Length"/> property of the new <see cref="Span{T}"/> would exceed <see cref="int.MaxValue"/>.
6359
/// </exception>

0 commit comments

Comments
 (0)