ToEnumerable.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerable
  9. {
  10. // REVIEW: This type of blocking is an anti-pattern. We may want to move it to System.Interactive.Async
  11. // and remove it from System.Linq.Async API surface.
  12. /// <summary>
  13. /// Converts an async-enumerable sequence to an enumerable sequence.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  16. /// <param name="source">An async-enumerable sequence to convert to an enumerable sequence.</param>
  17. /// <returns>The enumerable sequence containing the elements in the async-enumerable sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. [Obsolete("IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and it does not implement this method because 'sync over async' of this kind is an anti-pattern. Please use a different strategy.")]
  20. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. return Core(source);
  25. static IEnumerable<TSource> Core(IAsyncEnumerable<TSource> source)
  26. {
  27. var e = source.GetAsyncEnumerator(default);
  28. try
  29. {
  30. while (true)
  31. {
  32. if (!Wait(e.MoveNextAsync()))
  33. break;
  34. yield return e.Current;
  35. }
  36. }
  37. finally
  38. {
  39. Wait(e.DisposeAsync());
  40. }
  41. }
  42. }
  43. // NB: ValueTask and ValueTask<T> do not have to support blocking on a call to GetResult when backed by
  44. // an IValueTaskSource or IValueTaskSource<T> implementation. Convert to a Task or Task<T> to do so
  45. // in case the task hasn't completed yet.
  46. private static void Wait(ValueTask task)
  47. {
  48. var awaiter = task.GetAwaiter();
  49. if (!awaiter.IsCompleted)
  50. {
  51. task.AsTask().GetAwaiter().GetResult();
  52. return;
  53. }
  54. awaiter.GetResult();
  55. }
  56. private static T Wait<T>(ValueTask<T> task)
  57. {
  58. var awaiter = task.GetAwaiter();
  59. if (!awaiter.IsCompleted)
  60. {
  61. return task.AsTask().GetAwaiter().GetResult();
  62. }
  63. return awaiter.GetResult();
  64. }
  65. }
  66. }