DotNext by .NET Foundation and Contributors

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

 Chunk<T>

sealed class Chunk<T> : ReadOnlySequenceSegment<T>
using System; using System.Buffers; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace DotNext.Buffers { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] internal sealed class Chunk<[System.Runtime.CompilerServices.Nullable(2)] T> : ReadOnlySequenceSegment<T> { private Chunk([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlyMemory<T> segment) { base.Memory = segment; } private Chunk<T> Next([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlyMemory<T> segment) { long runningIndex = base.RunningIndex; Chunk<T> obj = new Chunk<T>(segment) { RunningIndex = runningIndex + base.Memory.Length }; Chunk<T> result = obj; base.Next = obj; return result; } internal static void AddChunk([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ReadOnlyMemory<T> segment, [AllowNull] ref Chunk<T> first, [AllowNull] ref Chunk<T> last) { if (first == null || last == null) { Chunk<T> obj = new Chunk<T>(segment) { RunningIndex = 0 }; Chunk<T> chunk = obj; last = obj; first = chunk; } else last = last.Next(segment); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] internal static ReadOnlySequence<T> CreateSequence(Chunk<T> head, Chunk<T> tail) { return new ReadOnlySequence<T>(head, 0, tail, tail.Memory.Length); } } }