using System; using System.Buffers; namespace PrefsGUI.Sync { /// /// ArrayPoolを使ってArraySegmentをプールする構造体 /// public struct ArraySegmentPooled : IDisposable { public static ArraySegmentPooled Get(int size) { var array = ArrayPool.Shared.Rent(size); var arraySegment = new ArraySegment(array, 0, size); return new ArraySegmentPooled() { ArraySegment = arraySegment }; } public static implicit operator ArraySegment (ArraySegmentPooled arraySegmentPooled) => arraySegmentPooled.ArraySegment; public ArraySegment ArraySegment { get; private set; } public void Dispose() { if (ArraySegment.Array != null) { ArrayPool.Shared.Return(ArraySegment.Array); } } } }