File tree Expand file tree Collapse file tree 6 files changed +149
-10
lines changed
Microsoft.Toolkit.HighPerformance Expand file tree Collapse file tree 6 files changed +149
-10
lines changed Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ using System . Diagnostics . Contracts ;
6+ using System . IO ;
7+ using System . Runtime . CompilerServices ;
8+ using Microsoft . Toolkit . HighPerformance . Buffers ;
9+ using Microsoft . Toolkit . HighPerformance . Streams ;
10+ using Microsoft . Toolkit . HighPerformance . Streams . Sources ;
11+
12+ namespace Microsoft . Toolkit . HighPerformance . Extensions
13+ {
14+ /// <summary>
15+ /// Helpers for working with the <see cref="ArrayPoolBufferWriter{T}"/> type.
16+ /// </summary>
17+ public static class ArrayPoolBufferWriterExtensions
18+ {
19+ /// <summary>
20+ /// Returns a <see cref="Stream"/> that can be used to write to a target an <see cref="ArrayPoolBufferWriter{T}"/> of <see cref="byte"/> instance.
21+ /// </summary>
22+ /// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance.</param>
23+ /// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
24+ /// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
25+ [ Pure ]
26+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
27+ public static Stream AsStream ( this ArrayPoolBufferWriter < byte > writer )
28+ {
29+ return new IBufferWriterStream < ArrayBufferWriterOwner > ( new ArrayBufferWriterOwner ( writer ) ) ;
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 99using System . Runtime . CompilerServices ;
1010using System . Runtime . InteropServices ;
1111using Microsoft . Toolkit . HighPerformance . Streams ;
12+ using Microsoft . Toolkit . HighPerformance . Streams . Sources ;
1213
1314namespace Microsoft . Toolkit . HighPerformance . Extensions
1415{
@@ -18,16 +19,16 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
1819 public static class IBufferWriterExtensions
1920 {
2021 /// <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+ /// Returns a <see cref="Stream"/> that can be used to write to a target an <see cref="IBufferWriter{T}"/> of <see cref="byte"/> instance.
2223 /// </summary>
23- /// <param name="writer">The target <see cref="Memory {T}"/> of <see cref="byte "/> instance.</param>
24+ /// <param name="writer">The target <see cref="IBufferWriter {T}"/> instance.</param>
2425 /// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
2526 /// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
2627 [ Pure ]
2728 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
2829 public static Stream AsStream ( this IBufferWriter < byte > writer )
2930 {
30- return new IBufferWriterStream ( writer ) ;
31+ return new IBufferWriterStream < IBufferWriterOwner > ( new IBufferWriterOwner ( writer ) ) ;
3132 }
3233
3334 /// <summary>
Original file line number Diff line number Diff line change 1111
1212namespace Microsoft . Toolkit . HighPerformance . Streams
1313{
14- /// <inheritdoc cref="IBufferWriterStream"/>
15- internal sealed partial class IBufferWriterStream
14+ /// <inheritdoc cref="IBufferWriterStream{TWriter} "/>
15+ internal sealed partial class IBufferWriterStream < TWriter >
1616 {
1717 /// <inheritdoc/>
1818 public override void CopyTo ( Stream destination , int bufferSize )
Original file line number Diff line number Diff line change @@ -14,23 +14,25 @@ namespace Microsoft.Toolkit.HighPerformance.Streams
1414 /// <summary>
1515 /// A <see cref="Stream"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
1616 /// </summary>
17- internal sealed partial class IBufferWriterStream : Stream
17+ /// <typeparam name="TWriter">The type of buffer writer to use.</typeparam>
18+ internal sealed partial class IBufferWriterStream < TWriter > : Stream
19+ where TWriter : struct , IBufferWriter < byte >
1820 {
1921 /// <summary>
20- /// The target <see cref="IBufferWriter{T} "/> instance to use.
22+ /// The target <typeparamref name="TWriter "/> instance to use.
2123 /// </summary>
22- private readonly IBufferWriter < byte > bufferWriter ;
24+ private readonly TWriter bufferWriter ;
2325
2426 /// <summary>
2527 /// Indicates whether or not the current instance has been disposed
2628 /// </summary>
2729 private bool disposed ;
2830
2931 /// <summary>
30- /// Initializes a new instance of the <see cref="IBufferWriterStream"/> class.
32+ /// Initializes a new instance of the <see cref="IBufferWriterStream{TWriter} "/> class.
3133 /// </summary>
3234 /// <param name="bufferWriter">The target <see cref="IBufferWriter{T}"/> instance to use.</param>
33- public IBufferWriterStream ( IBufferWriter < byte > bufferWriter )
35+ public IBufferWriterStream ( TWriter bufferWriter )
3436 {
3537 this . bufferWriter = bufferWriter ;
3638 }
Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ using System ;
6+ using System . Buffers ;
7+ using System . Runtime . CompilerServices ;
8+ using Microsoft . Toolkit . HighPerformance . Buffers ;
9+
10+ namespace Microsoft . Toolkit . HighPerformance . Streams . Sources
11+ {
12+ /// <summary>
13+ /// An <see cref="IBufferWriter{T}"/> implementation wrapping an <see cref="ArrayPoolBufferWriter{T}"/> instance.
14+ /// </summary>
15+ internal readonly struct ArrayBufferWriterOwner : IBufferWriter < byte >
16+ {
17+ /// <summary>
18+ /// The wrapped <see cref="ArrayPoolBufferWriter{T}"/> array.
19+ /// </summary>
20+ private readonly ArrayPoolBufferWriter < byte > writer ;
21+
22+ /// <summary>
23+ /// Initializes a new instance of the <see cref="ArrayBufferWriterOwner"/> struct.
24+ /// </summary>
25+ /// <param name="writer">The wrapped <see cref="ArrayPoolBufferWriter{T}"/> instance.</param>
26+ public ArrayBufferWriterOwner ( ArrayPoolBufferWriter < byte > writer )
27+ {
28+ this . writer = writer ;
29+ }
30+
31+ /// <inheritdoc/>
32+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
33+ public void Advance ( int count )
34+ {
35+ this . writer . Advance ( count ) ;
36+ }
37+
38+ /// <inheritdoc/>
39+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
40+ public Memory < byte > GetMemory ( int sizeHint = 0 )
41+ {
42+ return this . writer . GetMemory ( sizeHint ) ;
43+ }
44+
45+ /// <inheritdoc/>
46+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
47+ public Span < byte > GetSpan ( int sizeHint = 0 )
48+ {
49+ return this . writer . GetSpan ( sizeHint ) ;
50+ }
51+ }
52+ }
Original file line number Diff line number Diff line change 1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+ // See the LICENSE file in the project root for more information.
4+
5+ using System ;
6+ using System . Buffers ;
7+ using System . Runtime . CompilerServices ;
8+ using Microsoft . Toolkit . HighPerformance . Buffers ;
9+
10+ namespace Microsoft . Toolkit . HighPerformance . Streams . Sources
11+ {
12+ /// <summary>
13+ /// An <see cref="IBufferWriter{T}"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
14+ /// </summary>
15+ internal readonly struct IBufferWriterOwner : IBufferWriter < byte >
16+ {
17+ /// <summary>
18+ /// The wrapped <see cref="ArrayPoolBufferWriter{T}"/> array.
19+ /// </summary>
20+ private readonly IBufferWriter < byte > writer ;
21+
22+ /// <summary>
23+ /// Initializes a new instance of the <see cref="IBufferWriterOwner"/> struct.
24+ /// </summary>
25+ /// <param name="writer">The wrapped <see cref="IBufferWriter{T}"/> instance.</param>
26+ public IBufferWriterOwner ( IBufferWriter < byte > writer )
27+ {
28+ this . writer = writer ;
29+ }
30+
31+ /// <inheritdoc/>
32+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
33+ public void Advance ( int count )
34+ {
35+ this . writer . Advance ( count ) ;
36+ }
37+
38+ /// <inheritdoc/>
39+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
40+ public Memory < byte > GetMemory ( int sizeHint = 0 )
41+ {
42+ return this . writer . GetMemory ( sizeHint ) ;
43+ }
44+
45+ /// <inheritdoc/>
46+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
47+ public Span < byte > GetSpan ( int sizeHint = 0 )
48+ {
49+ return this . writer . GetSpan ( sizeHint ) ;
50+ }
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments