using System.Buffers; namespace TanksServer.Interactivity; internal sealed class BufferPool: MemoryPool { private readonly MemoryPool _actualPool = Shared; public override int MaxBufferSize => int.MaxValue; protected override void Dispose(bool disposing) {} public override IMemoryOwner Rent(int minBufferSize = -1) { ArgumentOutOfRangeException.ThrowIfLessThan(minBufferSize, 1); return new BufferPoolMemoryOwner(_actualPool.Rent(minBufferSize), minBufferSize); } private sealed class BufferPoolMemoryOwner(IMemoryOwner actualOwner, int wantedSize): IMemoryOwner { public Memory Memory { get; } = actualOwner.Memory[..wantedSize]; public void Dispose() => actualOwner.Dispose(); } }