Zip.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second)
  9. {
  10. Error.ThrowArgumentNullException(first, nameof(first));
  11. Error.ThrowArgumentNullException(second, nameof(second));
  12. return Zip(first, second, (x, y) => (x, y));
  13. }
  14. public static IUniTaskAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  15. {
  16. Error.ThrowArgumentNullException(first, nameof(first));
  17. Error.ThrowArgumentNullException(second, nameof(second));
  18. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  19. return new Zip<TFirst, TSecond, TResult>(first, second, resultSelector);
  20. }
  21. public static IUniTaskAsyncEnumerable<TResult> ZipAwait<TFirst, TSecond, TResult>(this IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, UniTask<TResult>> selector)
  22. {
  23. Error.ThrowArgumentNullException(first, nameof(first));
  24. Error.ThrowArgumentNullException(second, nameof(second));
  25. Error.ThrowArgumentNullException(selector, nameof(selector));
  26. return new ZipAwait<TFirst, TSecond, TResult>(first, second, selector);
  27. }
  28. public static IUniTaskAsyncEnumerable<TResult> ZipAwaitWithCancellation<TFirst, TSecond, TResult>(this IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, UniTask<TResult>> selector)
  29. {
  30. Error.ThrowArgumentNullException(first, nameof(first));
  31. Error.ThrowArgumentNullException(second, nameof(second));
  32. Error.ThrowArgumentNullException(selector, nameof(selector));
  33. return new ZipAwaitWithCancellation<TFirst, TSecond, TResult>(first, second, selector);
  34. }
  35. }
  36. internal sealed class Zip<TFirst, TSecond, TResult> : IUniTaskAsyncEnumerable<TResult>
  37. {
  38. readonly IUniTaskAsyncEnumerable<TFirst> first;
  39. readonly IUniTaskAsyncEnumerable<TSecond> second;
  40. readonly Func<TFirst, TSecond, TResult> resultSelector;
  41. public Zip(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  42. {
  43. this.first = first;
  44. this.second = second;
  45. this.resultSelector = resultSelector;
  46. }
  47. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  48. {
  49. return new _Zip(first, second, resultSelector, cancellationToken);
  50. }
  51. sealed class _Zip : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  52. {
  53. static readonly Action<object> firstMoveNextCoreDelegate = FirstMoveNextCore;
  54. static readonly Action<object> secondMoveNextCoreDelegate = SecondMoveNextCore;
  55. readonly IUniTaskAsyncEnumerable<TFirst> first;
  56. readonly IUniTaskAsyncEnumerable<TSecond> second;
  57. readonly Func<TFirst, TSecond, TResult> resultSelector;
  58. CancellationToken cancellationToken;
  59. IUniTaskAsyncEnumerator<TFirst> firstEnumerator;
  60. IUniTaskAsyncEnumerator<TSecond> secondEnumerator;
  61. UniTask<bool>.Awaiter firstAwaiter;
  62. UniTask<bool>.Awaiter secondAwaiter;
  63. public _Zip(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector, CancellationToken cancellationToken)
  64. {
  65. this.first = first;
  66. this.second = second;
  67. this.resultSelector = resultSelector;
  68. this.cancellationToken = cancellationToken;
  69. TaskTracker.TrackActiveTask(this, 3);
  70. }
  71. public TResult Current { get; private set; }
  72. public UniTask<bool> MoveNextAsync()
  73. {
  74. completionSource.Reset();
  75. if (firstEnumerator == null)
  76. {
  77. firstEnumerator = first.GetAsyncEnumerator(cancellationToken);
  78. secondEnumerator = second.GetAsyncEnumerator(cancellationToken);
  79. }
  80. firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter();
  81. if (firstAwaiter.IsCompleted)
  82. {
  83. FirstMoveNextCore(this);
  84. }
  85. else
  86. {
  87. firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this);
  88. }
  89. return new UniTask<bool>(this, completionSource.Version);
  90. }
  91. static void FirstMoveNextCore(object state)
  92. {
  93. var self = (_Zip)state;
  94. if (self.TryGetResult(self.firstAwaiter, out var result))
  95. {
  96. if (result)
  97. {
  98. try
  99. {
  100. self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
  101. }
  102. catch (Exception ex)
  103. {
  104. self.completionSource.TrySetException(ex);
  105. return;
  106. }
  107. if (self.secondAwaiter.IsCompleted)
  108. {
  109. SecondMoveNextCore(self);
  110. }
  111. else
  112. {
  113. self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self);
  114. }
  115. }
  116. else
  117. {
  118. self.completionSource.TrySetResult(false);
  119. }
  120. }
  121. }
  122. static void SecondMoveNextCore(object state)
  123. {
  124. var self = (_Zip)state;
  125. if (self.TryGetResult(self.secondAwaiter, out var result))
  126. {
  127. if (result)
  128. {
  129. try
  130. {
  131. self.Current = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current);
  132. }
  133. catch (Exception ex)
  134. {
  135. self.completionSource.TrySetException(ex);
  136. }
  137. if (self.cancellationToken.IsCancellationRequested)
  138. {
  139. self.completionSource.TrySetCanceled(self.cancellationToken);
  140. }
  141. else
  142. {
  143. self.completionSource.TrySetResult(true);
  144. }
  145. }
  146. else
  147. {
  148. self.completionSource.TrySetResult(false);
  149. }
  150. }
  151. }
  152. public async UniTask DisposeAsync()
  153. {
  154. TaskTracker.RemoveTracking(this);
  155. if (firstEnumerator != null)
  156. {
  157. await firstEnumerator.DisposeAsync();
  158. }
  159. if (secondEnumerator != null)
  160. {
  161. await secondEnumerator.DisposeAsync();
  162. }
  163. }
  164. }
  165. }
  166. internal sealed class ZipAwait<TFirst, TSecond, TResult> : IUniTaskAsyncEnumerable<TResult>
  167. {
  168. readonly IUniTaskAsyncEnumerable<TFirst> first;
  169. readonly IUniTaskAsyncEnumerable<TSecond> second;
  170. readonly Func<TFirst, TSecond, UniTask<TResult>> resultSelector;
  171. public ZipAwait(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, UniTask<TResult>> resultSelector)
  172. {
  173. this.first = first;
  174. this.second = second;
  175. this.resultSelector = resultSelector;
  176. }
  177. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  178. {
  179. return new _ZipAwait(first, second, resultSelector, cancellationToken);
  180. }
  181. sealed class _ZipAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  182. {
  183. static readonly Action<object> firstMoveNextCoreDelegate = FirstMoveNextCore;
  184. static readonly Action<object> secondMoveNextCoreDelegate = SecondMoveNextCore;
  185. static readonly Action<object> resultAwaitCoreDelegate = ResultAwaitCore;
  186. readonly IUniTaskAsyncEnumerable<TFirst> first;
  187. readonly IUniTaskAsyncEnumerable<TSecond> second;
  188. readonly Func<TFirst, TSecond, UniTask<TResult>> resultSelector;
  189. CancellationToken cancellationToken;
  190. IUniTaskAsyncEnumerator<TFirst> firstEnumerator;
  191. IUniTaskAsyncEnumerator<TSecond> secondEnumerator;
  192. UniTask<bool>.Awaiter firstAwaiter;
  193. UniTask<bool>.Awaiter secondAwaiter;
  194. UniTask<TResult>.Awaiter resultAwaiter;
  195. public _ZipAwait(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
  196. {
  197. this.first = first;
  198. this.second = second;
  199. this.resultSelector = resultSelector;
  200. this.cancellationToken = cancellationToken;
  201. TaskTracker.TrackActiveTask(this, 3);
  202. }
  203. public TResult Current { get; private set; }
  204. public UniTask<bool> MoveNextAsync()
  205. {
  206. completionSource.Reset();
  207. if (firstEnumerator == null)
  208. {
  209. firstEnumerator = first.GetAsyncEnumerator(cancellationToken);
  210. secondEnumerator = second.GetAsyncEnumerator(cancellationToken);
  211. }
  212. firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter();
  213. if (firstAwaiter.IsCompleted)
  214. {
  215. FirstMoveNextCore(this);
  216. }
  217. else
  218. {
  219. firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this);
  220. }
  221. return new UniTask<bool>(this, completionSource.Version);
  222. }
  223. static void FirstMoveNextCore(object state)
  224. {
  225. var self = (_ZipAwait)state;
  226. if (self.TryGetResult(self.firstAwaiter, out var result))
  227. {
  228. if (result)
  229. {
  230. try
  231. {
  232. self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
  233. }
  234. catch (Exception ex)
  235. {
  236. self.completionSource.TrySetException(ex);
  237. return;
  238. }
  239. if (self.secondAwaiter.IsCompleted)
  240. {
  241. SecondMoveNextCore(self);
  242. }
  243. else
  244. {
  245. self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self);
  246. }
  247. }
  248. else
  249. {
  250. self.completionSource.TrySetResult(false);
  251. }
  252. }
  253. }
  254. static void SecondMoveNextCore(object state)
  255. {
  256. var self = (_ZipAwait)state;
  257. if (self.TryGetResult(self.secondAwaiter, out var result))
  258. {
  259. if (result)
  260. {
  261. try
  262. {
  263. self.resultAwaiter = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current).GetAwaiter();
  264. if (self.resultAwaiter.IsCompleted)
  265. {
  266. ResultAwaitCore(self);
  267. }
  268. else
  269. {
  270. self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self);
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. self.completionSource.TrySetException(ex);
  276. }
  277. }
  278. else
  279. {
  280. self.completionSource.TrySetResult(false);
  281. }
  282. }
  283. }
  284. static void ResultAwaitCore(object state)
  285. {
  286. var self = (_ZipAwait)state;
  287. if (self.TryGetResult(self.resultAwaiter, out var result))
  288. {
  289. self.Current = result;
  290. if (self.cancellationToken.IsCancellationRequested)
  291. {
  292. self.completionSource.TrySetCanceled(self.cancellationToken);
  293. }
  294. else
  295. {
  296. self.completionSource.TrySetResult(true);
  297. }
  298. }
  299. }
  300. public async UniTask DisposeAsync()
  301. {
  302. TaskTracker.RemoveTracking(this);
  303. if (firstEnumerator != null)
  304. {
  305. await firstEnumerator.DisposeAsync();
  306. }
  307. if (secondEnumerator != null)
  308. {
  309. await secondEnumerator.DisposeAsync();
  310. }
  311. }
  312. }
  313. }
  314. internal sealed class ZipAwaitWithCancellation<TFirst, TSecond, TResult> : IUniTaskAsyncEnumerable<TResult>
  315. {
  316. readonly IUniTaskAsyncEnumerable<TFirst> first;
  317. readonly IUniTaskAsyncEnumerable<TSecond> second;
  318. readonly Func<TFirst, TSecond, CancellationToken, UniTask<TResult>> resultSelector;
  319. public ZipAwaitWithCancellation(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, UniTask<TResult>> resultSelector)
  320. {
  321. this.first = first;
  322. this.second = second;
  323. this.resultSelector = resultSelector;
  324. }
  325. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  326. {
  327. return new _ZipAwaitWithCancellation(first, second, resultSelector, cancellationToken);
  328. }
  329. sealed class _ZipAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  330. {
  331. static readonly Action<object> firstMoveNextCoreDelegate = FirstMoveNextCore;
  332. static readonly Action<object> secondMoveNextCoreDelegate = SecondMoveNextCore;
  333. static readonly Action<object> resultAwaitCoreDelegate = ResultAwaitCore;
  334. readonly IUniTaskAsyncEnumerable<TFirst> first;
  335. readonly IUniTaskAsyncEnumerable<TSecond> second;
  336. readonly Func<TFirst, TSecond, CancellationToken, UniTask<TResult>> resultSelector;
  337. CancellationToken cancellationToken;
  338. IUniTaskAsyncEnumerator<TFirst> firstEnumerator;
  339. IUniTaskAsyncEnumerator<TSecond> secondEnumerator;
  340. UniTask<bool>.Awaiter firstAwaiter;
  341. UniTask<bool>.Awaiter secondAwaiter;
  342. UniTask<TResult>.Awaiter resultAwaiter;
  343. public _ZipAwaitWithCancellation(IUniTaskAsyncEnumerable<TFirst> first, IUniTaskAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, UniTask<TResult>> resultSelector, CancellationToken cancellationToken)
  344. {
  345. this.first = first;
  346. this.second = second;
  347. this.resultSelector = resultSelector;
  348. this.cancellationToken = cancellationToken;
  349. TaskTracker.TrackActiveTask(this, 3);
  350. }
  351. public TResult Current { get; private set; }
  352. public UniTask<bool> MoveNextAsync()
  353. {
  354. completionSource.Reset();
  355. if (firstEnumerator == null)
  356. {
  357. firstEnumerator = first.GetAsyncEnumerator(cancellationToken);
  358. secondEnumerator = second.GetAsyncEnumerator(cancellationToken);
  359. }
  360. firstAwaiter = firstEnumerator.MoveNextAsync().GetAwaiter();
  361. if (firstAwaiter.IsCompleted)
  362. {
  363. FirstMoveNextCore(this);
  364. }
  365. else
  366. {
  367. firstAwaiter.SourceOnCompleted(firstMoveNextCoreDelegate, this);
  368. }
  369. return new UniTask<bool>(this, completionSource.Version);
  370. }
  371. static void FirstMoveNextCore(object state)
  372. {
  373. var self = (_ZipAwaitWithCancellation)state;
  374. if (self.TryGetResult(self.firstAwaiter, out var result))
  375. {
  376. if (result)
  377. {
  378. try
  379. {
  380. self.secondAwaiter = self.secondEnumerator.MoveNextAsync().GetAwaiter();
  381. }
  382. catch (Exception ex)
  383. {
  384. self.completionSource.TrySetException(ex);
  385. return;
  386. }
  387. if (self.secondAwaiter.IsCompleted)
  388. {
  389. SecondMoveNextCore(self);
  390. }
  391. else
  392. {
  393. self.secondAwaiter.SourceOnCompleted(secondMoveNextCoreDelegate, self);
  394. }
  395. }
  396. else
  397. {
  398. self.completionSource.TrySetResult(false);
  399. }
  400. }
  401. }
  402. static void SecondMoveNextCore(object state)
  403. {
  404. var self = (_ZipAwaitWithCancellation)state;
  405. if (self.TryGetResult(self.secondAwaiter, out var result))
  406. {
  407. if (result)
  408. {
  409. try
  410. {
  411. self.resultAwaiter = self.resultSelector(self.firstEnumerator.Current, self.secondEnumerator.Current, self.cancellationToken).GetAwaiter();
  412. if (self.resultAwaiter.IsCompleted)
  413. {
  414. ResultAwaitCore(self);
  415. }
  416. else
  417. {
  418. self.resultAwaiter.SourceOnCompleted(resultAwaitCoreDelegate, self);
  419. }
  420. }
  421. catch (Exception ex)
  422. {
  423. self.completionSource.TrySetException(ex);
  424. }
  425. }
  426. else
  427. {
  428. self.completionSource.TrySetResult(false);
  429. }
  430. }
  431. }
  432. static void ResultAwaitCore(object state)
  433. {
  434. var self = (_ZipAwaitWithCancellation)state;
  435. if (self.TryGetResult(self.resultAwaiter, out var result))
  436. {
  437. self.Current = result;
  438. if (self.cancellationToken.IsCancellationRequested)
  439. {
  440. self.completionSource.TrySetCanceled(self.cancellationToken);
  441. }
  442. else
  443. {
  444. self.completionSource.TrySetResult(true);
  445. }
  446. }
  447. }
  448. public async UniTask DisposeAsync()
  449. {
  450. TaskTracker.RemoveTracking(this);
  451. if (firstEnumerator != null)
  452. {
  453. await firstEnumerator.DisposeAsync();
  454. }
  455. if (secondEnumerator != null)
  456. {
  457. await secondEnumerator.DisposeAsync();
  458. }
  459. }
  460. }
  461. }
  462. }