You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
|
|
namespace Connected.Expressions.Collections;
|
|
|
|
|
|
|
|
|
|
internal class EnumerateOnce<T> : IEnumerable<T>, IEnumerable
|
|
|
|
|
{
|
|
|
|
|
private IEnumerable<T>? _enumerable;
|
|
|
|
|
|
|
|
|
|
public EnumerateOnce(IEnumerable<T> enumerable)
|
|
|
|
|
{
|
|
|
|
|
_enumerable = enumerable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
var en = Interlocked.Exchange(ref _enumerable, null);
|
|
|
|
|
|
|
|
|
|
if (en is not null)
|
|
|
|
|
return en.GetEnumerator();
|
|
|
|
|
|
|
|
|
|
throw new Exception("Enumerated more than once.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|