ToEnumerable.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.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. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. return Core();
  17. IEnumerable<TSource> Core()
  18. {
  19. var e = source.GetAsyncEnumerator(default);
  20. try
  21. {
  22. while (true)
  23. {
  24. if (!Wait(e.MoveNextAsync()))
  25. break;
  26. yield return e.Current;
  27. }
  28. }
  29. finally
  30. {
  31. Wait(e.DisposeAsync());
  32. }
  33. }
  34. }
  35. // NB: ValueTask and ValueTask<T> do not have to support blocking on a call to GetResult when backed by
  36. // an IValueTaskSource or IValueTaskSource<T> implementation. Convert to a Task or Task<T> to do so
  37. // in case the task hasn't completed yet.
  38. private static void Wait(ValueTask task)
  39. {
  40. var awaiter = task.GetAwaiter();
  41. if (!awaiter.IsCompleted)
  42. {
  43. task.AsTask().GetAwaiter().GetResult();
  44. return;
  45. }
  46. awaiter.GetResult();
  47. }
  48. private static T Wait<T>(ValueTask<T> task)
  49. {
  50. var awaiter = task.GetAwaiter();
  51. if (!awaiter.IsCompleted)
  52. {
  53. return task.AsTask().GetAwaiter().GetResult();
  54. }
  55. return awaiter.GetResult();
  56. }
  57. }
  58. }