Contains.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace Cysharp.Threading.Tasks.Linq
  6. {
  7. public static partial class UniTaskAsyncEnumerable
  8. {
  9. public static UniTask<Boolean> ContainsAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken = default)
  10. {
  11. return ContainsAsync(source, value, EqualityComparer<TSource>.Default, cancellationToken);
  12. }
  13. public static UniTask<Boolean> ContainsAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  17. return Contains.ContainsAsync(source, value, comparer, cancellationToken);
  18. }
  19. }
  20. internal static class Contains
  21. {
  22. internal static async UniTask<bool> ContainsAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
  23. {
  24. var e = source.GetAsyncEnumerator(cancellationToken);
  25. try
  26. {
  27. while (await e.MoveNextAsync())
  28. {
  29. if (comparer.Equals(value, e.Current))
  30. {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. finally
  37. {
  38. if (e != null)
  39. {
  40. await e.DisposeAsync();
  41. }
  42. }
  43. }
  44. }
  45. }