ETTask.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. namespace ETModel
  6. {
  7. /// <summary>
  8. /// Lightweight unity specified task-like object.
  9. /// </summary>
  10. [AsyncMethodBuilder(typeof (AsyncETTaskMethodBuilder))]
  11. public partial struct ETTask: IEquatable<ETTask>
  12. {
  13. private readonly IAwaiter awaiter;
  14. [DebuggerHidden]
  15. public ETTask(IAwaiter awaiter)
  16. {
  17. this.awaiter = awaiter;
  18. }
  19. [DebuggerHidden]
  20. public AwaiterStatus Status => awaiter?.Status ?? AwaiterStatus.Succeeded;
  21. [DebuggerHidden]
  22. public bool IsCompleted => awaiter?.IsCompleted ?? true;
  23. [DebuggerHidden]
  24. public void GetResult()
  25. {
  26. if (awaiter != null)
  27. {
  28. awaiter.GetResult();
  29. }
  30. }
  31. [DebuggerHidden]
  32. public Awaiter GetAwaiter()
  33. {
  34. return new Awaiter(this);
  35. }
  36. public bool Equals(ETTask other)
  37. {
  38. if (this.awaiter == null && other.awaiter == null)
  39. {
  40. return true;
  41. }
  42. if (this.awaiter != null && other.awaiter != null)
  43. {
  44. return this.awaiter == other.awaiter;
  45. }
  46. return false;
  47. }
  48. public override int GetHashCode()
  49. {
  50. if (this.awaiter == null)
  51. {
  52. return 0;
  53. }
  54. return this.awaiter.GetHashCode();
  55. }
  56. public override string ToString()
  57. {
  58. return this.awaiter == null? "()"
  59. : this.awaiter.Status == AwaiterStatus.Succeeded? "()"
  60. : "(" + this.awaiter.Status + ")";
  61. }
  62. public struct Awaiter: IAwaiter
  63. {
  64. private readonly ETTask task;
  65. [DebuggerHidden]
  66. public Awaiter(ETTask task)
  67. {
  68. this.task = task;
  69. }
  70. [DebuggerHidden]
  71. public bool IsCompleted => task.IsCompleted;
  72. [DebuggerHidden]
  73. public AwaiterStatus Status => task.Status;
  74. [DebuggerHidden]
  75. public void GetResult()
  76. {
  77. task.GetResult();
  78. }
  79. [DebuggerHidden]
  80. public void OnCompleted(Action continuation)
  81. {
  82. if (task.awaiter != null)
  83. {
  84. task.awaiter.OnCompleted(continuation);
  85. }
  86. else
  87. {
  88. continuation();
  89. }
  90. }
  91. [DebuggerHidden]
  92. public void UnsafeOnCompleted(Action continuation)
  93. {
  94. if (task.awaiter != null)
  95. {
  96. task.awaiter.UnsafeOnCompleted(continuation);
  97. }
  98. else
  99. {
  100. continuation();
  101. }
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Lightweight unity specified task-like object.
  107. /// </summary>
  108. [AsyncMethodBuilder(typeof (ETAsyncTaskMethodBuilder<>))]
  109. public struct ETTask<T>: IEquatable<ETTask<T>>
  110. {
  111. private readonly T result;
  112. private readonly IAwaiter<T> awaiter;
  113. [DebuggerHidden]
  114. public ETTask(T result)
  115. {
  116. this.result = result;
  117. this.awaiter = null;
  118. }
  119. [DebuggerHidden]
  120. public ETTask(IAwaiter<T> awaiter)
  121. {
  122. this.result = default;
  123. this.awaiter = awaiter;
  124. }
  125. [DebuggerHidden]
  126. public AwaiterStatus Status => awaiter?.Status ?? AwaiterStatus.Succeeded;
  127. [DebuggerHidden]
  128. public bool IsCompleted => awaiter?.IsCompleted ?? true;
  129. [DebuggerHidden]
  130. public T Result
  131. {
  132. get
  133. {
  134. if (awaiter == null)
  135. {
  136. return result;
  137. }
  138. return this.awaiter.GetResult();
  139. }
  140. }
  141. [DebuggerHidden]
  142. public Awaiter GetAwaiter()
  143. {
  144. return new Awaiter(this);
  145. }
  146. public bool Equals(ETTask<T> other)
  147. {
  148. if (this.awaiter == null && other.awaiter == null)
  149. {
  150. return EqualityComparer<T>.Default.Equals(this.result, other.result);
  151. }
  152. if (this.awaiter != null && other.awaiter != null)
  153. {
  154. return this.awaiter == other.awaiter;
  155. }
  156. return false;
  157. }
  158. public override int GetHashCode()
  159. {
  160. if (this.awaiter == null)
  161. {
  162. if (result == null)
  163. {
  164. return 0;
  165. }
  166. return result.GetHashCode();
  167. }
  168. return this.awaiter.GetHashCode();
  169. }
  170. public override string ToString()
  171. {
  172. return this.awaiter == null? result.ToString()
  173. : this.awaiter.Status == AwaiterStatus.Succeeded? this.awaiter.GetResult().ToString()
  174. : "(" + this.awaiter.Status + ")";
  175. }
  176. public static implicit operator ETTask(ETTask<T> task)
  177. {
  178. if (task.awaiter != null)
  179. {
  180. return new ETTask(task.awaiter);
  181. }
  182. return new ETTask();
  183. }
  184. public struct Awaiter: IAwaiter<T>
  185. {
  186. private readonly ETTask<T> task;
  187. [DebuggerHidden]
  188. public Awaiter(ETTask<T> task)
  189. {
  190. this.task = task;
  191. }
  192. [DebuggerHidden]
  193. public bool IsCompleted => task.IsCompleted;
  194. [DebuggerHidden]
  195. public AwaiterStatus Status => task.Status;
  196. [DebuggerHidden]
  197. void IAwaiter.GetResult()
  198. {
  199. GetResult();
  200. }
  201. [DebuggerHidden]
  202. public T GetResult()
  203. {
  204. return task.Result;
  205. }
  206. [DebuggerHidden]
  207. public void OnCompleted(Action continuation)
  208. {
  209. if (task.awaiter != null)
  210. {
  211. task.awaiter.OnCompleted(continuation);
  212. }
  213. else
  214. {
  215. continuation();
  216. }
  217. }
  218. [DebuggerHidden]
  219. public void UnsafeOnCompleted(Action continuation)
  220. {
  221. if (task.awaiter != null)
  222. {
  223. task.awaiter.UnsafeOnCompleted(continuation);
  224. }
  225. else
  226. {
  227. continuation();
  228. }
  229. }
  230. }
  231. }
  232. }