Just tried this and seems to not allocate in Unity. Something more generic like IReadOnlyCollection can't be used as it allocates.
public static RefLinqEnumerable<T, HashSetEnumerator<T>> ToRefLinq<T>(this HashSet<T> c) => new(new(c));
public struct HashSetEnumerator<T> : IRefEnumerable<T>
{
private T curr;
private HashSet<T>.Enumerator ie;
public HashSetEnumerator(HashSet<T> list)
{
ie = list.GetEnumerator();
curr = default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
bool end = ie.MoveNext();
curr = ie.Current;
if(!end)
ie.Dispose();
return end;
}
public T Current => curr;
}
No idea if that Dispose is correct.
Just tried this and seems to not allocate in Unity. Something more generic like IReadOnlyCollection can't be used as it allocates.
No idea if that Dispose is correct.