From 60b222742d316621abf36dfca60b87d830c5006b Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sat, 11 Jan 2020 16:51:13 -0600 Subject: [PATCH] Enumerables can now be stored as structs if possible --- IPA.Loader/Utilities/EnumerableExtensions.cs | 22 +++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/IPA.Loader/Utilities/EnumerableExtensions.cs b/IPA.Loader/Utilities/EnumerableExtensions.cs index 8cb732cb..3605deac 100644 --- a/IPA.Loader/Utilities/EnumerableExtensions.cs +++ b/IPA.Loader/Utilities/EnumerableExtensions.cs @@ -33,17 +33,19 @@ namespace IPA.Utilities this.first = first; } - public IEnumerator GetEnumerator() => new PrependEnumerator(this); + public PrependEnumerator GetEnumerator() => new PrependEnumerator(this); - private sealed class PrependEnumerator : IEnumerator + public struct PrependEnumerator : IEnumerator { private readonly IEnumerator restEnum; private readonly PrependEnumerable enumerable; - private int state = 0; - public PrependEnumerator(PrependEnumerable enumerable) + private int state; + internal PrependEnumerator(PrependEnumerable enumerable) { this.enumerable = enumerable; restEnum = enumerable.rest.GetEnumerator(); + state = 0; + Current = default; } public T Current { get; private set; } @@ -82,6 +84,7 @@ namespace IPA.Utilities } } + IEnumerator IEnumerable.GetEnumerator() => new PrependEnumerator(this); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } @@ -106,17 +109,19 @@ namespace IPA.Utilities this.last = last; } - public IEnumerator GetEnumerator() => new AppendEnumerator(this); + public AppendEnumerator GetEnumerator() => new AppendEnumerator(this); - private sealed class AppendEnumerator : IEnumerator + public struct AppendEnumerator : IEnumerator { private readonly IEnumerator restEnum; private readonly AppendEnumerable enumerable; - private int state = 0; - public AppendEnumerator(AppendEnumerable enumerable) + private int state; + internal AppendEnumerator(AppendEnumerable enumerable) { this.enumerable = enumerable; restEnum = enumerable.rest.GetEnumerator(); + state = 0; + Current = default; } public T Current { get; private set; } @@ -155,6 +160,7 @@ namespace IPA.Utilities } } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); }