From ceabac70d8e069b7a5193aed9a927787405497cd Mon Sep 17 00:00:00 2001 From: akiraveliara Date: Sat, 17 Aug 2024 14:47:47 +0200 Subject: [PATCH] make them disposable --- src/Bundles/ValueCollections/ValueAppendList.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Bundles/ValueCollections/ValueAppendList.cs b/src/Bundles/ValueCollections/ValueAppendList.cs index 6899892..4f633e3 100644 --- a/src/Bundles/ValueCollections/ValueAppendList.cs +++ b/src/Bundles/ValueCollections/ValueAppendList.cs @@ -2,18 +2,20 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +using System; using System.Buffers; namespace Bundles.ValueCollections; /// -/// A value-type list type that can only be modified by appending, never by removing. +/// A value-type list type that can only be modified by appending, never by removing. Use after disposal is undefined behaviour. /// /// The element type controlled by this list. -public partial record struct ValueAppendList +public partial record struct ValueAppendList : IDisposable { private T[] buffer; private int index; + private bool disposed; /// /// Gets a readonly reference to the element at the specified index. @@ -51,6 +53,16 @@ public ref readonly T Add(T value) public readonly Enumerator GetEnumerator() => new(this); + public void Dispose() + { + if (!this.disposed && typeof(T).IsPrimitive) + { + ArrayPool.Shared.Return(this.buffer); + } + + this.disposed = true; + } + private void ResizeBuffer() { int newLength = this.buffer.Length * 2;