ETTask.cs 7.7 KB

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