SingletonList<T>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace DotNext.Collections.Generic
{
[StructLayout(LayoutKind.Auto)]
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal readonly struct SingletonList<[System.Runtime.CompilerServices.Nullable(2)] T> : IReadOnlyList<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>
{
[System.Runtime.CompilerServices.Nullable(0)]
internal struct Enumerator : IEnumerator<T>, IEnumerator, IDisposable
{
private bool requested;
public T Current {
[IsReadOnly]
get;
}
[System.Runtime.CompilerServices.Nullable(2)]
object IEnumerator.Current {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return Current;
}
}
internal Enumerator(T element)
{
Current = element;
requested = false;
}
void IDisposable.Dispose()
{
this = default(Enumerator);
}
public bool MoveNext()
{
if (!requested)
return requested = true;
return false;
}
public void Reset()
{
requested = false;
}
}
private readonly T item;
T IReadOnlyList<T>.this[int index] {
get {
if (index != 0)
throw new IndexOutOfRangeException(ExceptionMessages.IndexShouldBeZero);
return item;
}
}
int IReadOnlyCollection<T>.Count {
get {
return 1;
}
}
internal SingletonList(T item)
{
this.item = item;
}
[System.Runtime.CompilerServices.NullableContext(0)]
public Enumerator GetEnumerator()
{
return new Enumerator(item);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}