Throw.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Returns an async-enumerable sequence that terminates with an exception.
  13. /// </summary>
  14. /// <typeparam name="TValue">The type used for the <see cref="IAsyncEnumerable{T}"/> type parameter of the resulting sequence.</typeparam>
  15. /// <param name="exception">Exception object used for the sequence's termination.</param>
  16. /// <returns>The async-enumerable sequence that terminates exceptionally with the specified exception object.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  18. public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  19. {
  20. if (exception == null)
  21. throw Error.ArgumentNull(nameof(exception));
  22. #if NO_TASK_FROMEXCEPTION
  23. var tcs = new TaskCompletionSource<bool>();
  24. tcs.TrySetException(exception);
  25. var moveNextThrows = new ValueTask<bool>(tcs.Task);
  26. #else
  27. var moveNextThrows = new ValueTask<bool>(Task.FromException<bool>(exception));
  28. #endif
  29. return new ThrowEnumerable<TValue>(moveNextThrows);
  30. }
  31. private sealed class ThrowEnumerable<TValue> : IAsyncEnumerable<TValue>
  32. {
  33. private readonly ValueTask<bool> _moveNextThrows;
  34. public ThrowEnumerable(ValueTask<bool> moveNextThrows)
  35. {
  36. _moveNextThrows = moveNextThrows;
  37. }
  38. public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)
  39. {
  40. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  41. return new ThrowEnumerator(_moveNextThrows);
  42. }
  43. private sealed class ThrowEnumerator : IAsyncEnumerator<TValue>
  44. {
  45. private ValueTask<bool> _moveNextThrows;
  46. public ThrowEnumerator(ValueTask<bool> moveNextThrows)
  47. {
  48. _moveNextThrows = moveNextThrows;
  49. }
  50. public TValue Current => default!;
  51. public ValueTask DisposeAsync()
  52. {
  53. _moveNextThrows = new ValueTask<bool>(false);
  54. return default;
  55. }
  56. public ValueTask<bool> MoveNextAsync()
  57. {
  58. var result = _moveNextThrows;
  59. _moveNextThrows = new ValueTask<bool>(false);
  60. return result;
  61. }
  62. }
  63. }
  64. }
  65. }