-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use
TryGetSingle
instead of collection enumerable to lists. (#…
…1738) Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
- Loading branch information
1 parent
b320e4e
commit 03d7bbc
Showing
3 changed files
with
94 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Refit; | ||
|
||
internal static class EnumerableExtensions | ||
{ | ||
internal static EnumerablePeek TryGetSingle<T>(this IEnumerable<T> enumerable, out T? value) | ||
{ | ||
value = default(T); | ||
using var enumerator = enumerable.GetEnumerator(); | ||
var hasFirst = enumerator.MoveNext(); | ||
if (!hasFirst) | ||
return EnumerablePeek.Empty; | ||
|
||
value = enumerator.Current; | ||
if (!enumerator.MoveNext()) | ||
return EnumerablePeek.Single; | ||
|
||
value = default(T); | ||
return EnumerablePeek.Many; | ||
} | ||
} | ||
|
||
internal enum EnumerablePeek | ||
{ | ||
Empty, | ||
Single, | ||
Many | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters