Removed callvirt-s in BufferWriterStream and AsStream cast

This commit is contained in:
Sergio Pedri
2020-10-06 13:41:57 +02:00
parent 8b5ed6edca
commit 368111012d
6 changed files with 149 additions and 10 deletions
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Buffers;
using Microsoft.Toolkit.HighPerformance.Streams;
using Microsoft.Toolkit.HighPerformance.Streams.Sources;
namespace Microsoft.Toolkit.HighPerformance.Extensions
{
/// <summary>
/// Helpers for working with the <see cref="ArrayPoolBufferWriter{T}"/> type.
/// </summary>
public static class ArrayPoolBufferWriterExtensions
{
/// <summary>
/// Returns a <see cref="Stream"/> that can be used to write to a target an <see cref="ArrayPoolBufferWriter{T}"/> of <see cref="byte"/> instance.
/// </summary>
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance.</param>
/// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
/// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Stream AsStream(this ArrayPoolBufferWriter<byte> writer)
{
return new IBufferWriterStream<ArrayBufferWriterOwner>(new ArrayBufferWriterOwner(writer));
}
}
}
@@ -9,6 +9,7 @@ using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.Toolkit.HighPerformance.Streams;
using Microsoft.Toolkit.HighPerformance.Streams.Sources;
namespace Microsoft.Toolkit.HighPerformance.Extensions
{
@@ -18,16 +19,16 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions
public static class IBufferWriterExtensions
{
/// <summary>
/// Returns a <see cref="Stream"/> that can be used to write to a target <see cref="IBufferWriter{T}"/> of <see cref="byte"/> instance.
/// Returns a <see cref="Stream"/> that can be used to write to a target an <see cref="IBufferWriter{T}"/> of <see cref="byte"/> instance.
/// </summary>
/// <param name="writer">The target <see cref="Memory{T}"/> of <see cref="byte"/> instance.</param>
/// <param name="writer">The target <see cref="IBufferWriter{T}"/> instance.</param>
/// <returns>A <see cref="Stream"/> wrapping <paramref name="writer"/> and writing data to its underlying buffer.</returns>
/// <remarks>The returned <see cref="Stream"/> can only be written to and does not support seeking.</remarks>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Stream AsStream(this IBufferWriter<byte> writer)
{
return new IBufferWriterStream(writer);
return new IBufferWriterStream<IBufferWriterOwner>(new IBufferWriterOwner(writer));
}
/// <summary>
@@ -11,8 +11,8 @@ using System.Threading.Tasks;
namespace Microsoft.Toolkit.HighPerformance.Streams
{
/// <inheritdoc cref="IBufferWriterStream"/>
internal sealed partial class IBufferWriterStream
/// <inheritdoc cref="IBufferWriterStream{TWriter}"/>
internal sealed partial class IBufferWriterStream<TWriter>
{
/// <inheritdoc/>
public override void CopyTo(Stream destination, int bufferSize)
@@ -14,12 +14,14 @@ namespace Microsoft.Toolkit.HighPerformance.Streams
/// <summary>
/// A <see cref="Stream"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
/// </summary>
internal sealed partial class IBufferWriterStream : Stream
/// <typeparam name="TWriter">The type of buffer writer to use.</typeparam>
internal sealed partial class IBufferWriterStream<TWriter> : Stream
where TWriter : struct, IBufferWriter<byte>
{
/// <summary>
/// The target <see cref="IBufferWriter{T}"/> instance to use.
/// The target <typeparamref name="TWriter"/> instance to use.
/// </summary>
private readonly IBufferWriter<byte> bufferWriter;
private readonly TWriter bufferWriter;
/// <summary>
/// Indicates whether or not the current instance has been disposed
@@ -27,10 +29,10 @@ namespace Microsoft.Toolkit.HighPerformance.Streams
private bool disposed;
/// <summary>
/// Initializes a new instance of the <see cref="IBufferWriterStream"/> class.
/// Initializes a new instance of the <see cref="IBufferWriterStream{TWriter}"/> class.
/// </summary>
/// <param name="bufferWriter">The target <see cref="IBufferWriter{T}"/> instance to use.</param>
public IBufferWriterStream(IBufferWriter<byte> bufferWriter)
public IBufferWriterStream(TWriter bufferWriter)
{
this.bufferWriter = bufferWriter;
}
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Buffers;
namespace Microsoft.Toolkit.HighPerformance.Streams.Sources
{
/// <summary>
/// An <see cref="IBufferWriter{T}"/> implementation wrapping an <see cref="ArrayPoolBufferWriter{T}"/> instance.
/// </summary>
internal readonly struct ArrayBufferWriterOwner : IBufferWriter<byte>
{
/// <summary>
/// The wrapped <see cref="ArrayPoolBufferWriter{T}"/> array.
/// </summary>
private readonly ArrayPoolBufferWriter<byte> writer;
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBufferWriterOwner"/> struct.
/// </summary>
/// <param name="writer">The wrapped <see cref="ArrayPoolBufferWriter{T}"/> instance.</param>
public ArrayBufferWriterOwner(ArrayPoolBufferWriter<byte> writer)
{
this.writer = writer;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Advance(int count)
{
this.writer.Advance(count);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Memory<byte> GetMemory(int sizeHint = 0)
{
return this.writer.GetMemory(sizeHint);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> GetSpan(int sizeHint = 0)
{
return this.writer.GetSpan(sizeHint);
}
}
}
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using Microsoft.Toolkit.HighPerformance.Buffers;
namespace Microsoft.Toolkit.HighPerformance.Streams.Sources
{
/// <summary>
/// An <see cref="IBufferWriter{T}"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
/// </summary>
internal readonly struct IBufferWriterOwner : IBufferWriter<byte>
{
/// <summary>
/// The wrapped <see cref="ArrayPoolBufferWriter{T}"/> array.
/// </summary>
private readonly IBufferWriter<byte> writer;
/// <summary>
/// Initializes a new instance of the <see cref="IBufferWriterOwner"/> struct.
/// </summary>
/// <param name="writer">The wrapped <see cref="IBufferWriter{T}"/> instance.</param>
public IBufferWriterOwner(IBufferWriter<byte> writer)
{
this.writer = writer;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Advance(int count)
{
this.writer.Advance(count);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Memory<byte> GetMemory(int sizeHint = 0)
{
return this.writer.GetMemory(sizeHint);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> GetSpan(int sizeHint = 0)
{
return this.writer.GetSpan(sizeHint);
}
}
}