ETTask.cs 6.8 KB

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