Never.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TValue> Never<TValue>()
  12. {
  13. return NeverAsyncEnumerable<TValue>.Instance;
  14. }
  15. private sealed class NeverAsyncEnumerable<TValue> : IAsyncEnumerable<TValue>
  16. {
  17. internal static readonly NeverAsyncEnumerable<TValue> Instance = new NeverAsyncEnumerable<TValue>();
  18. public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)
  19. {
  20. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  21. return new NeverAsyncEnumerator(cancellationToken);
  22. }
  23. private sealed class NeverAsyncEnumerator : IAsyncEnumerator<TValue>
  24. {
  25. private readonly CancellationToken _token;
  26. private CancellationTokenRegistration _registration;
  27. private bool _once;
  28. private TaskCompletionSource<bool> _task;
  29. public NeverAsyncEnumerator(CancellationToken token)
  30. {
  31. _token = token;
  32. }
  33. public TValue Current => throw new InvalidOperationException();
  34. public ValueTask DisposeAsync()
  35. {
  36. _registration.Dispose();
  37. _task = null;
  38. return default;
  39. }
  40. public ValueTask<bool> MoveNextAsync()
  41. {
  42. if (_once)
  43. {
  44. return new ValueTask<bool>(false);
  45. }
  46. _once = true;
  47. _task = new TaskCompletionSource<bool>();
  48. _registration = _token.Register(state => ((NeverAsyncEnumerator)state)._task.SetCanceled(), this);
  49. return new ValueTask<bool>(_task.Task);
  50. }
  51. }
  52. }
  53. }
  54. }