/// <summary> /// Writes a <see cref="ReadOnlySequence{T}"/> to a <see cref="IBufferWriterAsync{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="writer">The writer to write to</param> /// <param name="value">The <see cref="ReadOnlySequence{T}"/> to read from</param> /// <param name="token">A cancellation token to abort the operation</param> /// <returns>A task representing the progress fo the write operation</returns> public static async ValueTask WriteAsync <T>(this IBufferWriterAsync <T> writer, ReadOnlySequence <T> value, CancellationToken token = default) { foreach (var Segment in value) { await writer.WriteAsync(Segment.Span); } }
/// <summary> /// Writes a <see cref="ReadOnlySpan{T}"/> to a <see cref="IBufferWriterAsync{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="writer">The writer to write to</param> /// <param name="value">The <see cref="ReadOnlySpan{T}"/> to read from</param> /// <param name="token">A cancellation token to abort the operation</param> /// <returns>A task representing the progress fo the write operation</returns> public static ValueTask WriteAsync <T>(this IBufferWriterAsync <T> writer, ReadOnlySpan <T> value, CancellationToken token = default) { var Buffer = writer.GetMemory(value.Length); value.CopyTo(Buffer.Span); return(writer.AdvanceAsync(value.Length, token)); }
/// <summary> /// Reads a <see cref="IBufferReader{T}"/> to completion, writing to a <see cref="IBufferWriterAsync{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="reader">The reader to read from</param> /// <param name="writer">The writer to write to</param> /// <param name="token">A cancellation token to abort the operation</param> /// <returns>A task representing the progress fo the write operation</returns> public static async ValueTask CopyToAsync <T>(this IBufferReader <T> reader, IBufferWriterAsync <T> writer, CancellationToken token = default) { while (reader.CanRead) { var InBuffer = reader.GetMemory(0); if (InBuffer.IsEmpty) { break; } InBuffer.Span.CopyTo(writer.GetSpan(InBuffer.Length)); await writer.AdvanceAsync(InBuffer.Length, token); reader.Advance(InBuffer.Length); } }
/// <summary> /// Writes a <see cref="ReadOnlyMemory{T}"/> to a <see cref="IBufferWriterAsync{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="writer">The writer to write to</param> /// <param name="value">The <see cref="ReadOnlyMemory{T}"/> to read from</param> /// <param name="token">A cancellation token to abort the operation</param> /// <returns>A task representing the progress fo the write operation</returns> public static ValueTask WriteAsync <T>(this IBufferWriterAsync <T> writer, ReadOnlyMemory <T> value, CancellationToken token = default) => writer.WriteAsync(value.Span, token);