1
0

Yielder.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Security;
  5. namespace System.Linq
  6. {
  7. internal sealed class Yielder<T> : IYielder<T>, IAwaitable, IAwaiter
  8. {
  9. private readonly Action<Yielder<T>> _create;
  10. private Action _continuation;
  11. private bool _hasValue;
  12. private bool _running;
  13. private bool _stopped;
  14. public Yielder(Action<Yielder<T>> create) => _create = create;
  15. public T Current { get; private set; }
  16. public IAwaiter GetAwaiter() => this;
  17. public bool IsCompleted => false;
  18. public void GetResult()
  19. {
  20. }
  21. [SecurityCritical]
  22. public void UnsafeOnCompleted(Action continuation)
  23. {
  24. _continuation = continuation;
  25. }
  26. public void OnCompleted(Action continuation)
  27. {
  28. _continuation = continuation;
  29. }
  30. public IAwaitable Return(T value)
  31. {
  32. _hasValue = true;
  33. Current = value;
  34. return this;
  35. }
  36. public IAwaitable Break()
  37. {
  38. _stopped = true;
  39. return this;
  40. }
  41. public Yielder<T> GetEnumerator() => this;
  42. public bool MoveNext()
  43. {
  44. if (!_running)
  45. {
  46. _running = true;
  47. _create(this);
  48. }
  49. else
  50. {
  51. _hasValue = false;
  52. _continuation();
  53. }
  54. return !_stopped && _hasValue;
  55. }
  56. public void Reset() => throw new NotSupportedException();
  57. }
  58. }