Sum.tt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. <#
  8. var types = new[]
  9. {
  10. typeof(int),
  11. typeof(long),
  12. typeof(float),
  13. typeof(double),
  14. typeof(decimal),
  15. typeof(int?),
  16. typeof(long?),
  17. typeof(float?),
  18. typeof(double?),
  19. typeof(decimal?),
  20. };
  21. Func<Type, bool> IsNullable = x => x.IsGenericType;
  22. Func<Type, string> TypeName = x => IsNullable(x) ? x.GetGenericArguments()[0].Name + "?" : x.Name;
  23. Func<Type, string> WithSuffix = x => IsNullable(x) ? ".GetValueOrDefault()" : "";
  24. #>
  25. using System;
  26. using System.Threading;
  27. using Cysharp.Threading.Tasks.Internal;
  28. namespace Cysharp.Threading.Tasks.Linq
  29. {
  30. public static partial class UniTaskAsyncEnumerable
  31. {
  32. <# foreach(var t in types) { #>
  33. public static UniTask<<#= TypeName(t) #>> SumAsync(this IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken = default)
  34. {
  35. Error.ThrowArgumentNullException(source, nameof(source));
  36. return Sum.SumAsync(source, cancellationToken);
  37. }
  38. public static UniTask<<#= TypeName(t) #>> SumAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken = default)
  39. {
  40. Error.ThrowArgumentNullException(source, nameof(source));
  41. Error.ThrowArgumentNullException(source, nameof(selector));
  42. return Sum.SumAsync(source, selector, cancellationToken);
  43. }
  44. public static UniTask<<#= TypeName(t) #>> SumAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
  45. {
  46. Error.ThrowArgumentNullException(source, nameof(source));
  47. Error.ThrowArgumentNullException(source, nameof(selector));
  48. return Sum.SumAwaitAsync(source, selector, cancellationToken);
  49. }
  50. public static UniTask<<#= TypeName(t) #>> SumAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken = default)
  51. {
  52. Error.ThrowArgumentNullException(source, nameof(source));
  53. Error.ThrowArgumentNullException(source, nameof(selector));
  54. return Sum.SumAwaitWithCancellationAsync(source, selector, cancellationToken);
  55. }
  56. <# } #>
  57. }
  58. internal static class Sum
  59. {
  60. <# foreach(var t in types) { #>
  61. public static async UniTask<<#= TypeName(t) #>> SumAsync(IUniTaskAsyncEnumerable<<#= TypeName(t) #>> source, CancellationToken cancellationToken)
  62. {
  63. <#= TypeName(t) #> sum = default;
  64. var e = source.GetAsyncEnumerator(cancellationToken);
  65. try
  66. {
  67. while (await e.MoveNextAsync())
  68. {
  69. sum += e.Current<#= WithSuffix(t) #>;
  70. }
  71. }
  72. finally
  73. {
  74. if (e != null)
  75. {
  76. await e.DisposeAsync();
  77. }
  78. }
  79. return sum;
  80. }
  81. public static async UniTask<<#= TypeName(t) #>> SumAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, <#= TypeName(t) #>> selector, CancellationToken cancellationToken)
  82. {
  83. <#= TypeName(t) #> sum = default;
  84. var e = source.GetAsyncEnumerator(cancellationToken);
  85. try
  86. {
  87. while (await e.MoveNextAsync())
  88. {
  89. sum += selector(e.Current)<#= WithSuffix(t) #>;
  90. }
  91. }
  92. finally
  93. {
  94. if (e != null)
  95. {
  96. await e.DisposeAsync();
  97. }
  98. }
  99. return sum;
  100. }
  101. public static async UniTask<<#= TypeName(t) #>> SumAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
  102. {
  103. <#= TypeName(t) #> sum = default;
  104. var e = source.GetAsyncEnumerator(cancellationToken);
  105. try
  106. {
  107. while (await e.MoveNextAsync())
  108. {
  109. sum += (await selector(e.Current))<#= WithSuffix(t) #>;
  110. }
  111. }
  112. finally
  113. {
  114. if (e != null)
  115. {
  116. await e.DisposeAsync();
  117. }
  118. }
  119. return sum;
  120. }
  121. public static async UniTask<<#= TypeName(t) #>> SumAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<<#= TypeName(t) #>>> selector, CancellationToken cancellationToken)
  122. {
  123. <#= TypeName(t) #> sum = default;
  124. var e = source.GetAsyncEnumerator(cancellationToken);
  125. try
  126. {
  127. while (await e.MoveNextAsync())
  128. {
  129. sum += (await selector(e.Current, cancellationToken))<#= WithSuffix(t) #>;
  130. }
  131. }
  132. finally
  133. {
  134. if (e != null)
  135. {
  136. await e.DisposeAsync();
  137. }
  138. }
  139. return sum;
  140. }
  141. <# } #>
  142. }
  143. }