DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.0.0-rc.2" />

 BufferConsumer<T>

Represents implementation of IReadOnlySpanConsumer<T> in the form of the writer to IBufferWriter<T>.
using System; using System.Buffers; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; namespace DotNext.Buffers { [StructLayout(LayoutKind.Auto)] [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] public readonly struct BufferConsumer<T> : IReadOnlySpanConsumer<T>, ISupplier<ReadOnlyMemory<T>, CancellationToken, ValueTask>, IEquatable<BufferConsumer<T>> { [System.Runtime.CompilerServices.Nullable(1)] private readonly IBufferWriter<T> output; public bool IsEmpty => output == null; [System.Runtime.CompilerServices.NullableContext(1)] public BufferConsumer(IBufferWriter<T> output) { if (output == null) throw new ArgumentNullException("output"); this.output = output; } void IReadOnlySpanConsumer<T>.Invoke([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlySpan<T> span) { BuffersExtensions.Write<T>(output, span); } ValueTask ISupplier<ReadOnlyMemory<T>, CancellationToken, ValueTask>.Invoke([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlyMemory<T> input, CancellationToken token) { if (!token.IsCancellationRequested) { ValueTask result = default(ValueTask); try { BuffersExtensions.Write<T>(output, input.Span); return result; } catch (Exception exception) { return ValueTask.FromException(exception); } } return ValueTask.FromCanceled(token); } public bool Equals([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] BufferConsumer<T> other) { return output == other.output; } public override bool Equals([NotNullWhen(true)] object other) { if (other is BufferConsumer<T>) { BufferConsumer<T> other2 = (BufferConsumer<T>)other; return Equals(other2); } return false; } public override int GetHashCode() { return RuntimeHelpers.GetHashCode(output); } public override string ToString() { return output?.ToString(); } public static bool operator ==([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] BufferConsumer<T> x, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] BufferConsumer<T> y) { return x.Equals(y); } public static bool operator !=([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] BufferConsumer<T> x, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] BufferConsumer<T> y) { return !x.Equals(y); } } }