Select.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<TResult> Select<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. Error.ThrowArgumentNullException(selector, nameof(selector));
  12. return new Cysharp.Threading.Tasks.Linq.Select<TSource, TResult>(source, selector);
  13. }
  14. public static IUniTaskAsyncEnumerable<TResult> Select<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, TResult> selector)
  15. {
  16. Error.ThrowArgumentNullException(source, nameof(source));
  17. Error.ThrowArgumentNullException(selector, nameof(selector));
  18. return new Cysharp.Threading.Tasks.Linq.SelectInt<TSource, TResult>(source, selector);
  19. }
  20. public static IUniTaskAsyncEnumerable<TResult> SelectAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector)
  21. {
  22. Error.ThrowArgumentNullException(source, nameof(source));
  23. Error.ThrowArgumentNullException(selector, nameof(selector));
  24. return new Cysharp.Threading.Tasks.Linq.SelectAwait<TSource, TResult>(source, selector);
  25. }
  26. public static IUniTaskAsyncEnumerable<TResult> SelectAwait<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask<TResult>> selector)
  27. {
  28. Error.ThrowArgumentNullException(source, nameof(source));
  29. Error.ThrowArgumentNullException(selector, nameof(selector));
  30. return new Cysharp.Threading.Tasks.Linq.SelectIntAwait<TSource, TResult>(source, selector);
  31. }
  32. public static IUniTaskAsyncEnumerable<TResult> SelectAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector)
  33. {
  34. Error.ThrowArgumentNullException(source, nameof(source));
  35. Error.ThrowArgumentNullException(selector, nameof(selector));
  36. return new Cysharp.Threading.Tasks.Linq.SelectAwaitWithCancellation<TSource, TResult>(source, selector);
  37. }
  38. public static IUniTaskAsyncEnumerable<TResult> SelectAwaitWithCancellation<TSource, TResult>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask<TResult>> selector)
  39. {
  40. Error.ThrowArgumentNullException(source, nameof(source));
  41. Error.ThrowArgumentNullException(selector, nameof(selector));
  42. return new Cysharp.Threading.Tasks.Linq.SelectIntAwaitWithCancellation<TSource, TResult>(source, selector);
  43. }
  44. }
  45. internal sealed class Select<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  46. {
  47. readonly IUniTaskAsyncEnumerable<TSource> source;
  48. readonly Func<TSource, TResult> selector;
  49. public Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector)
  50. {
  51. this.source = source;
  52. this.selector = selector;
  53. }
  54. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  55. {
  56. return new _Select(source, selector, cancellationToken);
  57. }
  58. sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  59. {
  60. readonly IUniTaskAsyncEnumerable<TSource> source;
  61. readonly Func<TSource, TResult> selector;
  62. readonly CancellationToken cancellationToken;
  63. int state = -1;
  64. IUniTaskAsyncEnumerator<TSource> enumerator;
  65. UniTask<bool>.Awaiter awaiter;
  66. Action moveNextAction;
  67. public _Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  68. {
  69. this.source = source;
  70. this.selector = selector;
  71. this.cancellationToken = cancellationToken;
  72. this.moveNextAction = MoveNext;
  73. TaskTracker.TrackActiveTask(this, 3);
  74. }
  75. public TResult Current { get; private set; }
  76. public UniTask<bool> MoveNextAsync()
  77. {
  78. if (state == -2) return default;
  79. completionSource.Reset();
  80. MoveNext();
  81. return new UniTask<bool>(this, completionSource.Version);
  82. }
  83. void MoveNext()
  84. {
  85. try
  86. {
  87. switch (state)
  88. {
  89. case -1: // init
  90. enumerator = source.GetAsyncEnumerator(cancellationToken);
  91. goto case 0;
  92. case 0:
  93. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  94. if (awaiter.IsCompleted)
  95. {
  96. goto case 1;
  97. }
  98. else
  99. {
  100. state = 1;
  101. awaiter.UnsafeOnCompleted(moveNextAction);
  102. return;
  103. }
  104. case 1:
  105. if (awaiter.GetResult())
  106. {
  107. Current = selector(enumerator.Current);
  108. goto CONTINUE;
  109. }
  110. else
  111. {
  112. goto DONE;
  113. }
  114. default:
  115. goto DONE;
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. state = -2;
  121. completionSource.TrySetException(ex);
  122. return;
  123. }
  124. DONE:
  125. state = -2;
  126. completionSource.TrySetResult(false);
  127. return;
  128. CONTINUE:
  129. state = 0;
  130. completionSource.TrySetResult(true);
  131. return;
  132. }
  133. public UniTask DisposeAsync()
  134. {
  135. TaskTracker.RemoveTracking(this);
  136. return enumerator.DisposeAsync();
  137. }
  138. }
  139. }
  140. internal sealed class SelectInt<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  141. {
  142. readonly IUniTaskAsyncEnumerable<TSource> source;
  143. readonly Func<TSource, int, TResult> selector;
  144. public SelectInt(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector)
  145. {
  146. this.source = source;
  147. this.selector = selector;
  148. }
  149. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  150. {
  151. return new _Select(source, selector, cancellationToken);
  152. }
  153. sealed class _Select : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  154. {
  155. readonly IUniTaskAsyncEnumerable<TSource> source;
  156. readonly Func<TSource, int, TResult> selector;
  157. readonly CancellationToken cancellationToken;
  158. int state = -1;
  159. IUniTaskAsyncEnumerator<TSource> enumerator;
  160. UniTask<bool>.Awaiter awaiter;
  161. Action moveNextAction;
  162. int index;
  163. public _Select(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector, CancellationToken cancellationToken)
  164. {
  165. this.source = source;
  166. this.selector = selector;
  167. this.cancellationToken = cancellationToken;
  168. this.moveNextAction = MoveNext;
  169. TaskTracker.TrackActiveTask(this, 3);
  170. }
  171. public TResult Current { get; private set; }
  172. public UniTask<bool> MoveNextAsync()
  173. {
  174. if (state == -2) return default;
  175. completionSource.Reset();
  176. MoveNext();
  177. return new UniTask<bool>(this, completionSource.Version);
  178. }
  179. void MoveNext()
  180. {
  181. try
  182. {
  183. switch (state)
  184. {
  185. case -1: // init
  186. enumerator = source.GetAsyncEnumerator(cancellationToken);
  187. goto case 0;
  188. case 0:
  189. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  190. if (awaiter.IsCompleted)
  191. {
  192. goto case 1;
  193. }
  194. else
  195. {
  196. state = 1;
  197. awaiter.UnsafeOnCompleted(moveNextAction);
  198. return;
  199. }
  200. case 1:
  201. if (awaiter.GetResult())
  202. {
  203. Current = selector(enumerator.Current, checked(index++));
  204. goto CONTINUE;
  205. }
  206. else
  207. {
  208. goto DONE;
  209. }
  210. default:
  211. goto DONE;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. state = -2;
  217. completionSource.TrySetException(ex);
  218. return;
  219. }
  220. DONE:
  221. state = -2;
  222. completionSource.TrySetResult(false);
  223. return;
  224. CONTINUE:
  225. state = 0;
  226. completionSource.TrySetResult(true);
  227. return;
  228. }
  229. public UniTask DisposeAsync()
  230. {
  231. TaskTracker.RemoveTracking(this);
  232. return enumerator.DisposeAsync();
  233. }
  234. }
  235. }
  236. internal sealed class SelectAwait<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  237. {
  238. readonly IUniTaskAsyncEnumerable<TSource> source;
  239. readonly Func<TSource, UniTask<TResult>> selector;
  240. public SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector)
  241. {
  242. this.source = source;
  243. this.selector = selector;
  244. }
  245. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  246. {
  247. return new _SelectAwait(source, selector, cancellationToken);
  248. }
  249. sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  250. {
  251. readonly IUniTaskAsyncEnumerable<TSource> source;
  252. readonly Func<TSource, UniTask<TResult>> selector;
  253. readonly CancellationToken cancellationToken;
  254. int state = -1;
  255. IUniTaskAsyncEnumerator<TSource> enumerator;
  256. UniTask<bool>.Awaiter awaiter;
  257. UniTask<TResult>.Awaiter awaiter2;
  258. Action moveNextAction;
  259. public _SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<TResult>> selector, CancellationToken cancellationToken)
  260. {
  261. this.source = source;
  262. this.selector = selector;
  263. this.cancellationToken = cancellationToken;
  264. this.moveNextAction = MoveNext;
  265. TaskTracker.TrackActiveTask(this, 3);
  266. }
  267. public TResult Current { get; private set; }
  268. public UniTask<bool> MoveNextAsync()
  269. {
  270. if (state == -2) return default;
  271. completionSource.Reset();
  272. MoveNext();
  273. return new UniTask<bool>(this, completionSource.Version);
  274. }
  275. void MoveNext()
  276. {
  277. try
  278. {
  279. switch (state)
  280. {
  281. case -1: // init
  282. enumerator = source.GetAsyncEnumerator(cancellationToken);
  283. goto case 0;
  284. case 0:
  285. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  286. if (awaiter.IsCompleted)
  287. {
  288. goto case 1;
  289. }
  290. else
  291. {
  292. state = 1;
  293. awaiter.UnsafeOnCompleted(moveNextAction);
  294. return;
  295. }
  296. case 1:
  297. if (awaiter.GetResult())
  298. {
  299. awaiter2 = selector(enumerator.Current).GetAwaiter();
  300. if (awaiter2.IsCompleted)
  301. {
  302. goto case 2;
  303. }
  304. else
  305. {
  306. state = 2;
  307. awaiter2.UnsafeOnCompleted(moveNextAction);
  308. return;
  309. }
  310. }
  311. else
  312. {
  313. goto DONE;
  314. }
  315. case 2:
  316. Current = awaiter2.GetResult();
  317. goto CONTINUE;
  318. default:
  319. goto DONE;
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. state = -2;
  325. completionSource.TrySetException(ex);
  326. return;
  327. }
  328. DONE:
  329. state = -2;
  330. completionSource.TrySetResult(false);
  331. return;
  332. CONTINUE:
  333. state = 0;
  334. completionSource.TrySetResult(true);
  335. return;
  336. }
  337. public UniTask DisposeAsync()
  338. {
  339. TaskTracker.RemoveTracking(this);
  340. return enumerator.DisposeAsync();
  341. }
  342. }
  343. }
  344. internal sealed class SelectIntAwait<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  345. {
  346. readonly IUniTaskAsyncEnumerable<TSource> source;
  347. readonly Func<TSource, int, UniTask<TResult>> selector;
  348. public SelectIntAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<TResult>> selector)
  349. {
  350. this.source = source;
  351. this.selector = selector;
  352. }
  353. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  354. {
  355. return new _SelectAwait(source, selector, cancellationToken);
  356. }
  357. sealed class _SelectAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  358. {
  359. readonly IUniTaskAsyncEnumerable<TSource> source;
  360. readonly Func<TSource, int, UniTask<TResult>> selector;
  361. readonly CancellationToken cancellationToken;
  362. int state = -1;
  363. IUniTaskAsyncEnumerator<TSource> enumerator;
  364. UniTask<bool>.Awaiter awaiter;
  365. UniTask<TResult>.Awaiter awaiter2;
  366. Action moveNextAction;
  367. int index;
  368. public _SelectAwait(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, UniTask<TResult>> selector, CancellationToken cancellationToken)
  369. {
  370. this.source = source;
  371. this.selector = selector;
  372. this.cancellationToken = cancellationToken;
  373. this.moveNextAction = MoveNext;
  374. TaskTracker.TrackActiveTask(this, 3);
  375. }
  376. public TResult Current { get; private set; }
  377. public UniTask<bool> MoveNextAsync()
  378. {
  379. if (state == -2) return default;
  380. completionSource.Reset();
  381. MoveNext();
  382. return new UniTask<bool>(this, completionSource.Version);
  383. }
  384. void MoveNext()
  385. {
  386. try
  387. {
  388. switch (state)
  389. {
  390. case -1: // init
  391. enumerator = source.GetAsyncEnumerator(cancellationToken);
  392. goto case 0;
  393. case 0:
  394. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  395. if (awaiter.IsCompleted)
  396. {
  397. goto case 1;
  398. }
  399. else
  400. {
  401. state = 1;
  402. awaiter.UnsafeOnCompleted(moveNextAction);
  403. return;
  404. }
  405. case 1:
  406. if (awaiter.GetResult())
  407. {
  408. awaiter2 = selector(enumerator.Current, checked(index++)).GetAwaiter();
  409. if (awaiter2.IsCompleted)
  410. {
  411. goto case 2;
  412. }
  413. else
  414. {
  415. state = 2;
  416. awaiter2.UnsafeOnCompleted(moveNextAction);
  417. return;
  418. }
  419. }
  420. else
  421. {
  422. goto DONE;
  423. }
  424. case 2:
  425. Current = awaiter2.GetResult();
  426. goto CONTINUE;
  427. default:
  428. goto DONE;
  429. }
  430. }
  431. catch (Exception ex)
  432. {
  433. state = -2;
  434. completionSource.TrySetException(ex);
  435. return;
  436. }
  437. DONE:
  438. state = -2;
  439. completionSource.TrySetResult(false);
  440. return;
  441. CONTINUE:
  442. state = 0;
  443. completionSource.TrySetResult(true);
  444. return;
  445. }
  446. public UniTask DisposeAsync()
  447. {
  448. TaskTracker.RemoveTracking(this);
  449. return enumerator.DisposeAsync();
  450. }
  451. }
  452. }
  453. internal sealed class SelectAwaitWithCancellation<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  454. {
  455. readonly IUniTaskAsyncEnumerable<TSource> source;
  456. readonly Func<TSource, CancellationToken, UniTask<TResult>> selector;
  457. public SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector)
  458. {
  459. this.source = source;
  460. this.selector = selector;
  461. }
  462. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  463. {
  464. return new _SelectAwaitWithCancellation(source, selector, cancellationToken);
  465. }
  466. sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  467. {
  468. readonly IUniTaskAsyncEnumerable<TSource> source;
  469. readonly Func<TSource, CancellationToken, UniTask<TResult>> selector;
  470. readonly CancellationToken cancellationToken;
  471. int state = -1;
  472. IUniTaskAsyncEnumerator<TSource> enumerator;
  473. UniTask<bool>.Awaiter awaiter;
  474. UniTask<TResult>.Awaiter awaiter2;
  475. Action moveNextAction;
  476. public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
  477. {
  478. this.source = source;
  479. this.selector = selector;
  480. this.cancellationToken = cancellationToken;
  481. this.moveNextAction = MoveNext;
  482. TaskTracker.TrackActiveTask(this, 3);
  483. }
  484. public TResult Current { get; private set; }
  485. public UniTask<bool> MoveNextAsync()
  486. {
  487. if (state == -2) return default;
  488. completionSource.Reset();
  489. MoveNext();
  490. return new UniTask<bool>(this, completionSource.Version);
  491. }
  492. void MoveNext()
  493. {
  494. try
  495. {
  496. switch (state)
  497. {
  498. case -1: // init
  499. enumerator = source.GetAsyncEnumerator(cancellationToken);
  500. goto case 0;
  501. case 0:
  502. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  503. if (awaiter.IsCompleted)
  504. {
  505. goto case 1;
  506. }
  507. else
  508. {
  509. state = 1;
  510. awaiter.UnsafeOnCompleted(moveNextAction);
  511. return;
  512. }
  513. case 1:
  514. if (awaiter.GetResult())
  515. {
  516. awaiter2 = selector(enumerator.Current, cancellationToken).GetAwaiter();
  517. if (awaiter2.IsCompleted)
  518. {
  519. goto case 2;
  520. }
  521. else
  522. {
  523. state = 2;
  524. awaiter2.UnsafeOnCompleted(moveNextAction);
  525. return;
  526. }
  527. }
  528. else
  529. {
  530. goto DONE;
  531. }
  532. case 2:
  533. Current = awaiter2.GetResult();
  534. goto CONTINUE;
  535. default:
  536. goto DONE;
  537. }
  538. }
  539. catch (Exception ex)
  540. {
  541. state = -2;
  542. completionSource.TrySetException(ex);
  543. return;
  544. }
  545. DONE:
  546. state = -2;
  547. completionSource.TrySetResult(false);
  548. return;
  549. CONTINUE:
  550. state = 0;
  551. completionSource.TrySetResult(true);
  552. return;
  553. }
  554. public UniTask DisposeAsync()
  555. {
  556. TaskTracker.RemoveTracking(this);
  557. return enumerator.DisposeAsync();
  558. }
  559. }
  560. }
  561. internal sealed class SelectIntAwaitWithCancellation<TSource, TResult> : IUniTaskAsyncEnumerable<TResult>
  562. {
  563. readonly IUniTaskAsyncEnumerable<TSource> source;
  564. readonly Func<TSource, int, CancellationToken, UniTask<TResult>> selector;
  565. public SelectIntAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<TResult>> selector)
  566. {
  567. this.source = source;
  568. this.selector = selector;
  569. }
  570. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  571. {
  572. return new _SelectAwaitWithCancellation(source, selector, cancellationToken);
  573. }
  574. sealed class _SelectAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  575. {
  576. readonly IUniTaskAsyncEnumerable<TSource> source;
  577. readonly Func<TSource, int, CancellationToken, UniTask<TResult>> selector;
  578. readonly CancellationToken cancellationToken;
  579. int state = -1;
  580. IUniTaskAsyncEnumerator<TSource> enumerator;
  581. UniTask<bool>.Awaiter awaiter;
  582. UniTask<TResult>.Awaiter awaiter2;
  583. Action moveNextAction;
  584. int index;
  585. public _SelectAwaitWithCancellation(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, UniTask<TResult>> selector, CancellationToken cancellationToken)
  586. {
  587. this.source = source;
  588. this.selector = selector;
  589. this.cancellationToken = cancellationToken;
  590. this.moveNextAction = MoveNext;
  591. TaskTracker.TrackActiveTask(this, 3);
  592. }
  593. public TResult Current { get; private set; }
  594. public UniTask<bool> MoveNextAsync()
  595. {
  596. if (state == -2) return default;
  597. completionSource.Reset();
  598. MoveNext();
  599. return new UniTask<bool>(this, completionSource.Version);
  600. }
  601. void MoveNext()
  602. {
  603. try
  604. {
  605. switch (state)
  606. {
  607. case -1: // init
  608. enumerator = source.GetAsyncEnumerator(cancellationToken);
  609. goto case 0;
  610. case 0:
  611. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  612. if (awaiter.IsCompleted)
  613. {
  614. goto case 1;
  615. }
  616. else
  617. {
  618. state = 1;
  619. awaiter.UnsafeOnCompleted(moveNextAction);
  620. return;
  621. }
  622. case 1:
  623. if (awaiter.GetResult())
  624. {
  625. awaiter2 = selector(enumerator.Current, checked(index++), cancellationToken).GetAwaiter();
  626. if (awaiter2.IsCompleted)
  627. {
  628. goto case 2;
  629. }
  630. else
  631. {
  632. state = 2;
  633. awaiter2.UnsafeOnCompleted(moveNextAction);
  634. return;
  635. }
  636. }
  637. else
  638. {
  639. goto DONE;
  640. }
  641. case 2:
  642. Current = awaiter2.GetResult();
  643. goto CONTINUE;
  644. default:
  645. goto DONE;
  646. }
  647. }
  648. catch (Exception ex)
  649. {
  650. state = -2;
  651. completionSource.TrySetException(ex);
  652. return;
  653. }
  654. DONE:
  655. state = -2;
  656. completionSource.TrySetResult(false);
  657. return;
  658. CONTINUE:
  659. state = 0;
  660. completionSource.TrySetResult(true);
  661. return;
  662. }
  663. public UniTask DisposeAsync()
  664. {
  665. TaskTracker.RemoveTracking(this);
  666. return enumerator.DisposeAsync();
  667. }
  668. }
  669. }
  670. }