Using.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  13. {
  14. if (resourceFactory == null)
  15. throw Error.ArgumentNull(nameof(resourceFactory));
  16. if (enumerableFactory == null)
  17. throw Error.ArgumentNull(nameof(enumerableFactory));
  18. return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);
  19. }
  20. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<Task<TResource>> resourceFactory, Func<TResource, Task<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  21. {
  22. if (resourceFactory == null)
  23. throw Error.ArgumentNull(nameof(resourceFactory));
  24. if (enumerableFactory == null)
  25. throw Error.ArgumentNull(nameof(enumerableFactory));
  26. return new UsingAsyncIteratorWithTask<TSource, TResource>(resourceFactory, enumerableFactory);
  27. }
  28. private sealed class UsingAsyncIterator<TSource, TResource> : AsyncIterator<TSource> where TResource : IDisposable
  29. {
  30. private readonly Func<TResource, IAsyncEnumerable<TSource>> _enumerableFactory;
  31. private readonly Func<TResource> _resourceFactory;
  32. private IAsyncEnumerator<TSource> _enumerator;
  33. private TResource _resource;
  34. public UsingAsyncIterator(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory)
  35. {
  36. Debug.Assert(resourceFactory != null);
  37. Debug.Assert(enumerableFactory != null);
  38. _resourceFactory = resourceFactory;
  39. _enumerableFactory = enumerableFactory;
  40. }
  41. public override AsyncIterator<TSource> Clone()
  42. {
  43. return new UsingAsyncIterator<TSource, TResource>(_resourceFactory, _enumerableFactory);
  44. }
  45. public override async ValueTask DisposeAsync()
  46. {
  47. if (_enumerator != null)
  48. {
  49. await _enumerator.DisposeAsync().ConfigureAwait(false);
  50. _enumerator = null;
  51. }
  52. if (_resource != null)
  53. {
  54. _resource.Dispose();
  55. _resource = default;
  56. }
  57. await base.DisposeAsync().ConfigureAwait(false);
  58. }
  59. protected override async ValueTask<bool> MoveNextCore()
  60. {
  61. // NB: Earlier behavior of this operator was more eager, causing the resource factory to be called upon calling
  62. // GetAsyncEnumerator. This is inconsistent with asynchronous "using" and with a C# 8.0 async iterator with
  63. // a using statement inside, so this logic got moved to MoveNextAsync instead.
  64. switch (_state)
  65. {
  66. case AsyncIteratorState.Allocated:
  67. _resource = _resourceFactory();
  68. _enumerator = _enumerableFactory(_resource).GetAsyncEnumerator(_cancellationToken);
  69. _state = AsyncIteratorState.Iterating;
  70. goto case AsyncIteratorState.Iterating;
  71. case AsyncIteratorState.Iterating:
  72. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  73. {
  74. _current = _enumerator.Current;
  75. return true;
  76. }
  77. await DisposeAsync().ConfigureAwait(false);
  78. break;
  79. }
  80. return false;
  81. }
  82. }
  83. private sealed class UsingAsyncIteratorWithTask<TSource, TResource> : AsyncIterator<TSource> where TResource : IDisposable
  84. {
  85. private readonly Func<TResource, Task<IAsyncEnumerable<TSource>>> _enumerableFactory;
  86. private readonly Func<Task<TResource>> _resourceFactory;
  87. private IAsyncEnumerable<TSource> _enumerable;
  88. private IAsyncEnumerator<TSource> _enumerator;
  89. private TResource _resource;
  90. public UsingAsyncIteratorWithTask(Func<Task<TResource>> resourceFactory, Func<TResource, Task<IAsyncEnumerable<TSource>>> enumerableFactory)
  91. {
  92. Debug.Assert(resourceFactory != null);
  93. Debug.Assert(enumerableFactory != null);
  94. _resourceFactory = resourceFactory;
  95. _enumerableFactory = enumerableFactory;
  96. }
  97. public override AsyncIterator<TSource> Clone()
  98. {
  99. return new UsingAsyncIteratorWithTask<TSource, TResource>(_resourceFactory, _enumerableFactory);
  100. }
  101. public override async ValueTask DisposeAsync()
  102. {
  103. if (_enumerator != null)
  104. {
  105. await _enumerator.DisposeAsync().ConfigureAwait(false);
  106. _enumerator = null;
  107. }
  108. if (_resource != null)
  109. {
  110. _resource.Dispose();
  111. _resource = default;
  112. }
  113. await base.DisposeAsync().ConfigureAwait(false);
  114. }
  115. protected override async ValueTask<bool> MoveNextCore()
  116. {
  117. switch (_state)
  118. {
  119. case AsyncIteratorState.Allocated:
  120. _resource = await _resourceFactory().ConfigureAwait(false);
  121. _enumerable = await _enumerableFactory(_resource).ConfigureAwait(false);
  122. _enumerator = _enumerable.GetAsyncEnumerator(_cancellationToken);
  123. _state = AsyncIteratorState.Iterating;
  124. goto case AsyncIteratorState.Iterating;
  125. case AsyncIteratorState.Iterating:
  126. while (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  127. {
  128. _current = _enumerator.Current;
  129. return true;
  130. }
  131. await DisposeAsync().ConfigureAwait(false);
  132. break;
  133. }
  134. return false;
  135. }
  136. }
  137. }
  138. }