AsyncEnumerable.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. return source.Select(x => x);
  15. }
  16. public static IAsyncEnumerable<TValue> Empty<TValue>()
  17. {
  18. return CreateEnumerable(() => CreateEnumerator<TValue>(ct => TaskExt.False, current: null, dispose: null));
  19. }
  20. public static IAsyncEnumerable<TValue> Never<TValue>()
  21. {
  22. return CreateEnumerable(() => CreateEnumerator<TValue>(tcs => tcs.Task, current: null, dispose: null));
  23. }
  24. public static IAsyncEnumerable<TValue> Return<TValue>(TValue value)
  25. {
  26. return new[] { value }.ToAsyncEnumerable();
  27. }
  28. public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  29. {
  30. if (exception == null)
  31. throw new ArgumentNullException(nameof(exception));
  32. return CreateEnumerable(
  33. () => CreateEnumerator<TValue>(
  34. ct =>
  35. {
  36. var tcs = new TaskCompletionSource<bool>();
  37. tcs.TrySetException(exception);
  38. return tcs.Task;
  39. },
  40. current: null,
  41. dispose: null)
  42. );
  43. }
  44. }
  45. }