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 9
9
using System . Runtime . CompilerServices ;
10
10
using System . Runtime . InteropServices ;
11
11
using Microsoft . Toolkit . HighPerformance . Streams ;
12
+ using Microsoft . Toolkit . HighPerformance . Streams . Sources ;
12
13
13
14
namespace Microsoft . Toolkit . HighPerformance . Extensions
14
15
{
@@ -18,16 +19,16 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
18
19
public static class IBufferWriterExtensions
19
20
{
20
21
/// <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.
22
23
/// </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>
24
25
/// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
25
26
/// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
26
27
[ Pure ]
27
28
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
28
29
public static Stream AsStream ( this IBufferWriter < byte > writer )
29
30
{
30
- return new IBufferWriterStream ( writer ) ;
31
+ return new IBufferWriterStream < IBufferWriterOwner > ( new IBufferWriterOwner ( writer ) ) ;
31
32
}
32
33
33
34
/// <summary>
Original file line number Diff line number Diff line change 11
11
12
12
namespace Microsoft . Toolkit . HighPerformance . Streams
13
13
{
14
- /// <inheritdoc cref="IBufferWriterStream"/>
15
- internal sealed partial class IBufferWriterStream
14
+ /// <inheritdoc cref="IBufferWriterStream{TWriter} "/>
15
+ internal sealed partial class IBufferWriterStream < TWriter >
16
16
{
17
17
/// <inheritdoc/>
18
18
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
14
14
/// <summary>
15
15
/// A <see cref="Stream"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
16
16
/// </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 >
18
20
{
19
21
/// <summary>
20
- /// The target <see cref="IBufferWriter{T} "/> instance to use.
22
+ /// The target <typeparamref name="TWriter "/> instance to use.
21
23
/// </summary>
22
- private readonly IBufferWriter < byte > bufferWriter ;
24
+ private readonly TWriter bufferWriter ;
23
25
24
26
/// <summary>
25
27
/// Indicates whether or not the current instance has been disposed
26
28
/// </summary>
27
29
private bool disposed ;
28
30
29
31
/// <summary>
30
- /// Initializes a new instance of the <see cref="IBufferWriterStream"/> class.
32
+ /// Initializes a new instance of the <see cref="IBufferWriterStream{TWriter} "/> class.
31
33
/// </summary>
32
34
/// <param name="bufferWriter">The target <see cref="IBufferWriter{T}"/> instance to use.</param>
33
- public IBufferWriterStream ( IBufferWriter < byte > bufferWriter )
35
+ public IBufferWriterStream ( TWriter bufferWriter )
34
36
{
35
37
this . bufferWriter = bufferWriter ;
36
38
}
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