ETTask.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 (ETAsyncTaskMethodBuilder))]
  11. public partial struct ETTask: IEquatable<ETTask>
  12. {
  13. private static readonly ETTask<AsyncUnit> DefaultAsyncUnitTask = new ETTask<AsyncUnit>(AsyncUnit.Default);
  14. private readonly IAwaiter awaiter;
  15. [DebuggerHidden]
  16. public ETTask(IAwaiter awaiter)
  17. {
  18. this.awaiter = awaiter;
  19. }
  20. [DebuggerHidden]
  21. public ETTask(Func<ETTask> factory)
  22. {
  23. this.awaiter = new LazyPromise(factory);
  24. }
  25. [DebuggerHidden]
  26. public AwaiterStatus Status => awaiter?.Status ?? AwaiterStatus.Succeeded;
  27. [DebuggerHidden]
  28. public bool IsCompleted => awaiter?.IsCompleted ?? true;
  29. [DebuggerHidden]
  30. public void GetResult()
  31. {
  32. if (awaiter != null)
  33. {
  34. awaiter.GetResult();
  35. }
  36. }
  37. [DebuggerHidden]
  38. public Awaiter GetAwaiter()
  39. {
  40. return new Awaiter(this);
  41. }
  42. /// <summary>
  43. /// returns (bool IsCanceled) instead of throws OperationCanceledException.
  44. /// </summary>
  45. public ETTask<bool> SuppressCancellationThrow()
  46. {
  47. AwaiterStatus status = Status;
  48. switch (status)
  49. {
  50. case AwaiterStatus.Succeeded:
  51. return CompletedTasks.False;
  52. case AwaiterStatus.Canceled:
  53. return CompletedTasks.True;
  54. default:
  55. return new ETTask<bool>(new IsCanceledAwaiter(this.awaiter));
  56. }
  57. }
  58. public bool Equals(ETTask other)
  59. {
  60. if (this.awaiter == null && other.awaiter == null)
  61. {
  62. return true;
  63. }
  64. if (this.awaiter != null && other.awaiter != null)
  65. {
  66. return this.awaiter == other.awaiter;
  67. }
  68. return false;
  69. }
  70. public override int GetHashCode()
  71. {
  72. if (this.awaiter == null)
  73. {
  74. return 0;
  75. }
  76. return this.awaiter.GetHashCode();
  77. }
  78. public override string ToString()
  79. {
  80. return this.awaiter == null? "()"
  81. : this.awaiter.Status == AwaiterStatus.Succeeded? "()"
  82. : "(" + this.awaiter.Status + ")";
  83. }
  84. public static implicit operator ETTask<AsyncUnit>(ETTask task)
  85. {
  86. if (task.awaiter == null)
  87. {
  88. return DefaultAsyncUnitTask;
  89. }
  90. if (task.awaiter.IsCompleted)
  91. {
  92. return DefaultAsyncUnitTask;
  93. }
  94. // UniTask<T> -> UniTask is free but UniTask -> UniTask<T> requires wrapping cost.
  95. return new ETTask<AsyncUnit>(new AsyncUnitAwaiter(task.awaiter));
  96. }
  97. private class AsyncUnitAwaiter: IAwaiter<AsyncUnit>
  98. {
  99. private readonly IAwaiter awaiter;
  100. public AsyncUnitAwaiter(IAwaiter awaiter)
  101. {
  102. this.awaiter = awaiter;
  103. }
  104. public bool IsCompleted => awaiter.IsCompleted;
  105. public AwaiterStatus Status => awaiter.Status;
  106. public AsyncUnit GetResult()
  107. {
  108. awaiter.GetResult();
  109. return AsyncUnit.Default;
  110. }
  111. public void OnCompleted(Action continuation)
  112. {
  113. awaiter.OnCompleted(continuation);
  114. }
  115. public void UnsafeOnCompleted(Action continuation)
  116. {
  117. awaiter.UnsafeOnCompleted(continuation);
  118. }
  119. void IAwaiter.GetResult()
  120. {
  121. awaiter.GetResult();
  122. }
  123. }
  124. private class IsCanceledAwaiter: IAwaiter<bool>
  125. {
  126. private readonly IAwaiter awaiter;
  127. public IsCanceledAwaiter(IAwaiter awaiter)
  128. {
  129. this.awaiter = awaiter;
  130. }
  131. public bool IsCompleted => awaiter.IsCompleted;
  132. public AwaiterStatus Status => awaiter.Status;
  133. public bool GetResult()
  134. {
  135. if (awaiter.Status == AwaiterStatus.Canceled)
  136. {
  137. return true;
  138. }
  139. awaiter.GetResult();
  140. return false;
  141. }
  142. public void OnCompleted(Action continuation)
  143. {
  144. awaiter.OnCompleted(continuation);
  145. }
  146. public void UnsafeOnCompleted(Action continuation)
  147. {
  148. awaiter.UnsafeOnCompleted(continuation);
  149. }
  150. void IAwaiter.GetResult()
  151. {
  152. awaiter.GetResult();
  153. }
  154. }
  155. public struct Awaiter: IAwaiter
  156. {
  157. private readonly ETTask task;
  158. [DebuggerHidden]
  159. public Awaiter(ETTask task)
  160. {
  161. this.task = task;
  162. }
  163. [DebuggerHidden]
  164. public bool IsCompleted => task.IsCompleted;
  165. [DebuggerHidden]
  166. public AwaiterStatus Status => task.Status;
  167. [DebuggerHidden]
  168. public void GetResult()
  169. {
  170. task.GetResult();
  171. }
  172. [DebuggerHidden]
  173. public void OnCompleted(Action continuation)
  174. {
  175. if (task.awaiter != null)
  176. {
  177. task.awaiter.OnCompleted(continuation);
  178. }
  179. else
  180. {
  181. continuation();
  182. }
  183. }
  184. [DebuggerHidden]
  185. public void UnsafeOnCompleted(Action continuation)
  186. {
  187. if (task.awaiter != null)
  188. {
  189. task.awaiter.UnsafeOnCompleted(continuation);
  190. }
  191. else
  192. {
  193. continuation();
  194. }
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Lightweight unity specified task-like object.
  200. /// </summary>
  201. [AsyncMethodBuilder(typeof (AsyncUniTaskMethodBuilder<>))]
  202. public struct ETTask<T>: IEquatable<ETTask<T>>
  203. {
  204. private readonly T result;
  205. private readonly IAwaiter<T> awaiter;
  206. [DebuggerHidden]
  207. public ETTask(T result)
  208. {
  209. this.result = result;
  210. this.awaiter = null;
  211. }
  212. [DebuggerHidden]
  213. public ETTask(IAwaiter<T> awaiter)
  214. {
  215. this.result = default;
  216. this.awaiter = awaiter;
  217. }
  218. [DebuggerHidden]
  219. public ETTask(Func<ETTask<T>> factory)
  220. {
  221. this.result = default;
  222. this.awaiter = new LazyPromise<T>(factory);
  223. }
  224. [DebuggerHidden]
  225. public AwaiterStatus Status => awaiter?.Status ?? AwaiterStatus.Succeeded;
  226. [DebuggerHidden]
  227. public bool IsCompleted => awaiter?.IsCompleted ?? true;
  228. [DebuggerHidden]
  229. public T Result
  230. {
  231. get
  232. {
  233. if (awaiter == null)
  234. {
  235. return result;
  236. }
  237. return this.awaiter.GetResult();
  238. }
  239. }
  240. [DebuggerHidden]
  241. public Awaiter GetAwaiter()
  242. {
  243. return new Awaiter(this);
  244. }
  245. /// <summary>
  246. /// returns (bool IsCanceled, T Result) instead of throws OperationCanceledException.
  247. /// </summary>
  248. public ETTask<(bool IsCanceled, T Result)> SuppressCancellationThrow()
  249. {
  250. var status = Status;
  251. if (status == AwaiterStatus.Succeeded)
  252. {
  253. return new ETTask<(bool, T)>((false, Result));
  254. }
  255. if (status == AwaiterStatus.Canceled)
  256. {
  257. return new ETTask<(bool, T)>((true, default));
  258. }
  259. return new ETTask<(bool, T)>(new IsCanceledAwaiter(awaiter));
  260. }
  261. public bool Equals(ETTask<T> other)
  262. {
  263. if (this.awaiter == null && other.awaiter == null)
  264. {
  265. return EqualityComparer<T>.Default.Equals(this.result, other.result);
  266. }
  267. if (this.awaiter != null && other.awaiter != null)
  268. {
  269. return this.awaiter == other.awaiter;
  270. }
  271. return false;
  272. }
  273. public override int GetHashCode()
  274. {
  275. if (this.awaiter == null)
  276. {
  277. if (result == null)
  278. {
  279. return 0;
  280. }
  281. return result.GetHashCode();
  282. }
  283. return this.awaiter.GetHashCode();
  284. }
  285. public override string ToString()
  286. {
  287. return this.awaiter == null? result.ToString()
  288. : this.awaiter.Status == AwaiterStatus.Succeeded? this.awaiter.GetResult().ToString()
  289. : "(" + this.awaiter.Status + ")";
  290. }
  291. public static implicit operator ETTask(ETTask<T> task)
  292. {
  293. if (task.awaiter != null)
  294. {
  295. return new ETTask(task.awaiter);
  296. }
  297. return new ETTask();
  298. }
  299. private class IsCanceledAwaiter: IAwaiter<(bool, T)>
  300. {
  301. private readonly IAwaiter<T> awaiter;
  302. public IsCanceledAwaiter(IAwaiter<T> awaiter)
  303. {
  304. this.awaiter = awaiter;
  305. }
  306. public bool IsCompleted => awaiter.IsCompleted;
  307. public AwaiterStatus Status => awaiter.Status;
  308. public (bool, T) GetResult()
  309. {
  310. if (awaiter.Status == AwaiterStatus.Canceled)
  311. {
  312. return (true, default);
  313. }
  314. return (false, awaiter.GetResult());
  315. }
  316. public void OnCompleted(Action continuation)
  317. {
  318. awaiter.OnCompleted(continuation);
  319. }
  320. public void UnsafeOnCompleted(Action continuation)
  321. {
  322. awaiter.UnsafeOnCompleted(continuation);
  323. }
  324. void IAwaiter.GetResult()
  325. {
  326. awaiter.GetResult();
  327. }
  328. }
  329. public struct Awaiter: IAwaiter<T>
  330. {
  331. private readonly ETTask<T> task;
  332. [DebuggerHidden]
  333. public Awaiter(ETTask<T> task)
  334. {
  335. this.task = task;
  336. }
  337. [DebuggerHidden]
  338. public bool IsCompleted => task.IsCompleted;
  339. [DebuggerHidden]
  340. public AwaiterStatus Status => task.Status;
  341. [DebuggerHidden]
  342. void IAwaiter.GetResult()
  343. {
  344. GetResult();
  345. }
  346. [DebuggerHidden]
  347. public T GetResult()
  348. {
  349. return task.Result;
  350. }
  351. [DebuggerHidden]
  352. public void OnCompleted(Action continuation)
  353. {
  354. if (task.awaiter != null)
  355. {
  356. task.awaiter.OnCompleted(continuation);
  357. }
  358. else
  359. {
  360. continuation();
  361. }
  362. }
  363. [DebuggerHidden]
  364. public void UnsafeOnCompleted(Action continuation)
  365. {
  366. if (task.awaiter != null)
  367. {
  368. task.awaiter.UnsafeOnCompleted(continuation);
  369. }
  370. else
  371. {
  372. continuation();
  373. }
  374. }
  375. }
  376. }
  377. }