DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="5.3.1" />

 TextConsumer

Represents implementation of IReadOnlySpanConsumer<T> in the form of the writer to TextWriter.
using DotNext.Buffers; using DotNext.Runtime.CompilerServices; using System; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DotNext.IO { [StructLayout(LayoutKind.Auto)] public readonly struct TextConsumer : IReadOnlySpanConsumer<char>, ISupplier<ReadOnlyMemory<char>, CancellationToken, ValueTask>, IFunctional<Func<ReadOnlyMemory<char>, CancellationToken, ValueTask>>, IFlushable, IEquatable<TextConsumer> { private readonly TextWriter output; public bool IsEmpty => output == null; [NullableContext(1)] public TextConsumer(TextWriter output) { if (output == null) throw new ArgumentNullException("output"); this.output = output; } void IReadOnlySpanConsumer<char>.Invoke(ReadOnlySpan<char> input) { output.Write(input); } ValueTask ISupplier<ReadOnlyMemory<char>, CancellationToken, ValueTask>.Invoke(ReadOnlyMemory<char> input, CancellationToken token) { return new ValueTask(output.WriteAsync(input, token)); } void IFlushable.Flush() { output.Flush(); } Task IFlushable.FlushAsync(CancellationToken token) { if (!token.IsCancellationRequested) return output.FlushAsync(token); return Task.FromCanceled(token); } [NullableContext(2)] public override string ToString() { return output?.ToString(); } public static implicit operator TextConsumer(TextWriter output) { return new TextConsumer(output); } [CompilerGenerated] private bool PrintMembers(StringBuilder builder) { builder.Append("IsEmpty = "); builder.Append(IsEmpty.ToString()); return true; } public static bool operator !=(TextConsumer left, TextConsumer right) { return !(left == right); } public static bool operator ==(TextConsumer left, TextConsumer right) { return left.Equals(right); } [CompilerGenerated] public override int GetHashCode() { return EqualityComparer<TextWriter>.Default.GetHashCode(output); } [CompilerGenerated] public override bool Equals(object obj) { if (obj is TextConsumer) return Equals((TextConsumer)obj); return false; } [CompilerGenerated] public bool Equals(TextConsumer other) { return EqualityComparer<TextWriter>.Default.Equals(output, other.output); } [NullableContext(1)] [CompilerGenerated] public void Deconstruct(out TextWriter output) { output = this.output; } } }