Skip to content

Commit

Permalink
make them disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
akiraveliara committed Aug 17, 2024
1 parent 55acbe7 commit ceabac7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Bundles/ValueCollections/ValueAppendList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The element type controlled by this list.</typeparam>
public partial record struct ValueAppendList<T>
public partial record struct ValueAppendList<T> : IDisposable
{
private T[] buffer;
private int index;
private bool disposed;

/// <summary>
/// Gets a readonly reference to the element at the specified index.
Expand Down Expand Up @@ -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<T>.Shared.Return(this.buffer);
}

this.disposed = true;
}

private void ResizeBuffer()
{
int newLength = this.buffer.Length * 2;
Expand Down

0 comments on commit ceabac7

Please sign in to comment.