DotNext by .NET Foundation and Contributors

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

 DelegatingReadOnlySpanConsumer<T, TArg>

Represents implementation of IReadOnlySpanConsumer<T> that delegates invocation to the delegate of type ReadOnlySpanAction<T, U>.
using DotNext.Runtime.CompilerServices; using System; using System.Buffers; 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 DelegatingReadOnlySpanConsumer<T, TArg> : IReadOnlySpanConsumer<T>, ISupplier<ReadOnlyMemory<T>, CancellationToken, ValueTask>, IFunctional<Func<ReadOnlyMemory<T>, CancellationToken, ValueTask>> { private readonly ReadOnlySpanAction<T, TArg> action; private readonly TArg arg; public bool IsEmpty => action == null; [System.Runtime.CompilerServices.NullableContext(1)] public DelegatingReadOnlySpanConsumer(ReadOnlySpanAction<T, TArg> action, TArg arg) { if (action == null) throw new ArgumentNullException("action"); this.action = action; this.arg = arg; } void IReadOnlySpanConsumer<T>.Invoke(ReadOnlySpan<T> span) { action(span, arg); } ValueTask ISupplier<ReadOnlyMemory<T>, CancellationToken, ValueTask>.Invoke(ReadOnlyMemory<T> input, CancellationToken token) { if (!token.IsCancellationRequested) { ValueTask result = default(ValueTask); try { action(input.Span, arg); return result; } catch (Exception exception) { return ValueTask.FromException(exception); } } return ValueTask.FromCanceled(token); } } }