Skip to content

Commit bcadbd7

Browse files
committed
Added IBufferWriter<byte>.AsStream extension
1 parent ab73d61 commit bcadbd7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Microsoft.Toolkit.HighPerformance/Extensions/IBufferWriterExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
using System;
66
using System.Buffers;
7+
using System.Diagnostics.Contracts;
8+
using System.IO;
79
using System.Runtime.CompilerServices;
810
using System.Runtime.InteropServices;
11+
using Microsoft.Toolkit.HighPerformance.Streams;
912

1013
namespace Microsoft.Toolkit.HighPerformance.Extensions
1114
{
@@ -14,6 +17,19 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
1417
/// </summary>
1518
public static class IBufferWriterExtensions
1619
{
20+
/// <summary>
21+
/// Returns a <see cref="Stream"/> that can be used to write to a target <see cref="IBufferWriter{T}"/> of <see cref="byte"/> instance.
22+
/// </summary>
23+
/// <param name="writer">The target <see cref="Memory{T}"/> of <see cref="byte"/> instance.</param>
24+
/// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
25+
/// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
26+
[Pure]
27+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28+
public static Stream AsStream(this IBufferWriter<byte> writer)
29+
{
30+
return new IBufferWriterStream(writer);
31+
}
32+
1733
/// <summary>
1834
/// Writes a value of a specified type into a target <see cref="IBufferWriter{T}"/> instance.
1935
/// </summary>

0 commit comments

Comments
 (0)