AsyncEnumerable.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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> Return<TValue>(TValue value)
  21. {
  22. return new[] { value }.ToAsyncEnumerable();
  23. }
  24. public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  25. {
  26. if (exception == null)
  27. throw new ArgumentNullException(nameof(exception));
  28. return CreateEnumerable(
  29. () => CreateEnumerator<TValue>(
  30. ct =>
  31. {
  32. var tcs = new TaskCompletionSource<bool>();
  33. tcs.TrySetException(exception);
  34. return tcs.Task;
  35. },
  36. current: null,
  37. dispose: null)
  38. );
  39. }
  40. }
  41. }