AsyncETTaskMethodBuilder.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using System.Security;
  5. namespace ET
  6. {
  7. public struct ETAsyncTaskMethodBuilder
  8. {
  9. private ETTask tcs;
  10. // 1. Static Create method.
  11. [DebuggerHidden]
  12. public static ETAsyncTaskMethodBuilder Create()
  13. {
  14. ETAsyncTaskMethodBuilder builder = new ETAsyncTaskMethodBuilder() { tcs = ETTask.Create(true) };
  15. return builder;
  16. }
  17. // 2. TaskLike Task property.
  18. [DebuggerHidden]
  19. public ETTask Task => this.tcs;
  20. // 3. SetException
  21. [DebuggerHidden]
  22. public void SetException(Exception exception)
  23. {
  24. this.tcs.SetException(exception);
  25. }
  26. // 4. SetResult
  27. [DebuggerHidden]
  28. public void SetResult()
  29. {
  30. this.tcs.SetResult();
  31. }
  32. // 5. AwaitOnCompleted
  33. [DebuggerHidden]
  34. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
  35. {
  36. awaiter.OnCompleted(stateMachine.MoveNext);
  37. }
  38. // 6. AwaitUnsafeOnCompleted
  39. [DebuggerHidden]
  40. [SecuritySafeCritical]
  41. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
  42. {
  43. awaiter.OnCompleted(stateMachine.MoveNext);
  44. }
  45. // 7. Start
  46. [DebuggerHidden]
  47. public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
  48. {
  49. stateMachine.MoveNext();
  50. }
  51. // 8. SetStateMachine
  52. [DebuggerHidden]
  53. public void SetStateMachine(IAsyncStateMachine stateMachine)
  54. {
  55. }
  56. }
  57. public struct ETAsyncTaskMethodBuilder<T>
  58. {
  59. private ETTask<T> tcs;
  60. // 1. Static Create method.
  61. [DebuggerHidden]
  62. public static ETAsyncTaskMethodBuilder<T> Create()
  63. {
  64. ETAsyncTaskMethodBuilder<T> builder = new ETAsyncTaskMethodBuilder<T>() { tcs = ETTask<T>.Create(true) };
  65. return builder;
  66. }
  67. // 2. TaskLike Task property.
  68. [DebuggerHidden]
  69. public ETTask<T> Task => this.tcs;
  70. // 3. SetException
  71. [DebuggerHidden]
  72. public void SetException(Exception exception)
  73. {
  74. this.tcs.SetException(exception);
  75. }
  76. // 4. SetResult
  77. [DebuggerHidden]
  78. public void SetResult(T ret)
  79. {
  80. this.tcs.SetResult(ret);
  81. }
  82. // 5. AwaitOnCompleted
  83. [DebuggerHidden]
  84. public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
  85. {
  86. awaiter.OnCompleted(stateMachine.MoveNext);
  87. }
  88. // 6. AwaitUnsafeOnCompleted
  89. [DebuggerHidden]
  90. [SecuritySafeCritical]
  91. public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
  92. {
  93. awaiter.OnCompleted(stateMachine.MoveNext);
  94. }
  95. // 7. Start
  96. [DebuggerHidden]
  97. public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
  98. {
  99. stateMachine.MoveNext();
  100. }
  101. // 8. SetStateMachine
  102. [DebuggerHidden]
  103. public void SetStateMachine(IAsyncStateMachine stateMachine)
  104. {
  105. }
  106. }
  107. }