StitchATon2/Infra/Buffers/ArrayOwner.cs

33 lines
740 B
C#
Raw Normal View History

2025-07-30 07:30:00 +07:00
using System.Buffers;
namespace StitchATon2.Infra.Buffers;
public class ArrayOwner<T> : IBuffer<T> where T : unmanaged
{
private readonly ArrayPool<T> _owner;
private readonly T[] _buffer;
public ArrayOwner(ArrayPool<T> owner, int length)
2025-07-30 07:30:00 +07:00
{
_owner = owner;
_buffer = owner.Rent(length);
Length = length;
2025-07-30 07:30:00 +07:00
}
~ArrayOwner() => Dispose();
public void Dispose()
{
_owner.Return(_buffer);
GC.SuppressFinalize(this);
}
public ref T this[int index] => ref _buffer[index];
public Span<T> Span => _buffer.AsSpan(0, Length);
public Memory<T> Memory => _buffer.AsMemory(0, Length);
2025-07-30 07:30:00 +07:00
public T[] Array => _buffer;
public int Length { get; }
2025-07-30 07:30:00 +07:00
}