1
0

AsyncEnumerable.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 Error.ArgumentNull(nameof(source));
  14. return source.Select(x => x);
  15. }
  16. private static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  17. {
  18. if (exception == null)
  19. throw Error.ArgumentNull(nameof(exception));
  20. #if NO_TASK_FROMEXCEPTION
  21. var tcs = new TaskCompletionSource<bool>();
  22. tcs.TrySetException(exception);
  23. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  24. #else
  25. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  26. #endif
  27. return CreateEnumerable(
  28. _ => CreateEnumerator<TValue>(
  29. () => moveNextThrows,
  30. current: null,
  31. dispose: null)
  32. );
  33. }
  34. }
  35. }