Contains.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken = default) =>
  12. source is ICollection<TSource> collection ? Task.FromResult(collection.Contains(value)) :
  13. ContainsAsync(source, value, comparer: null, cancellationToken);
  14. public static Task<bool> ContainsAsync<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken = default)
  15. {
  16. if (source == null)
  17. throw Error.ArgumentNull(nameof(source));
  18. //
  19. // See https://github.com/dotnet/corefx/pull/25097 for the optimization here.
  20. //
  21. if (comparer == null)
  22. {
  23. return Core(source, value, cancellationToken);
  24. async Task<bool> Core<TSource>(IAsyncEnumerable<TSource> _source, TSource _value, IEqualityComparer<TSource> _comparer, CancellationToken _cancellationToken)
  25. {
  26. #if CSHARP8 && AETOR_HAS_CT // CS0656 Missing compiler required member 'System.Collections.Generic.IAsyncEnumerable`1.GetAsyncEnumerator'
  27. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  28. {
  29. if (EqualityComparer<TSource>.Default.Equals(item, _value))
  30. {
  31. return true;
  32. }
  33. }
  34. #else
  35. var e = _source.GetAsyncEnumerator(_cancellationToken);
  36. try
  37. {
  38. while (await e.MoveNextAsync().ConfigureAwait(false))
  39. {
  40. if (EqualityComparer<TSource>.Default.Equals(e.Current, _value))
  41. {
  42. return true;
  43. }
  44. }
  45. }
  46. finally
  47. {
  48. await e.DisposeAsync().ConfigureAwait(false);
  49. }
  50. #endif
  51. return false;
  52. }
  53. }
  54. else
  55. {
  56. return Core(source, value, comparer, cancellationToken);
  57. async Task<bool> Core<TSource>(IAsyncEnumerable<TSource> _source, TSource _value, IEqualityComparer<TSource> _comparer, CancellationToken _cancellationToken)
  58. {
  59. #if CSHARP8 && AETOR_HAS_CT // CS0656 Missing compiler required member 'System.Collections.Generic.IAsyncEnumerable`1.GetAsyncEnumerator'
  60. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  61. {
  62. if (_comparer.Equals(item, _value))
  63. {
  64. return true;
  65. }
  66. }
  67. #else
  68. var e = _source.GetAsyncEnumerator(_cancellationToken);
  69. try
  70. {
  71. while (await e.MoveNextAsync().ConfigureAwait(false))
  72. {
  73. if (_comparer.Equals(e.Current, _value))
  74. {
  75. return true;
  76. }
  77. }
  78. }
  79. finally
  80. {
  81. await e.DisposeAsync().ConfigureAwait(false);
  82. }
  83. #endif
  84. return false;
  85. }
  86. }
  87. }
  88. }
  89. }