IUniTaskSource.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. namespace Cysharp.Threading.Tasks
  5. {
  6. public enum UniTaskStatus
  7. {
  8. /// <summary>The operation has not yet completed.</summary>
  9. Pending = 0,
  10. /// <summary>The operation completed successfully.</summary>
  11. Succeeded = 1,
  12. /// <summary>The operation completed with an error.</summary>
  13. Faulted = 2,
  14. /// <summary>The operation completed due to cancellation.</summary>
  15. Canceled = 3
  16. }
  17. // similar as IValueTaskSource
  18. public interface IUniTaskSource
  19. #if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
  20. : System.Threading.Tasks.Sources.IValueTaskSource
  21. #pragma warning disable CS0108
  22. #endif
  23. {
  24. UniTaskStatus GetStatus(short token);
  25. void OnCompleted(Action<object> continuation, object state, short token);
  26. void GetResult(short token);
  27. UniTaskStatus UnsafeGetStatus(); // only for debug use.
  28. #if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
  29. #pragma warning restore CS0108
  30. System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource.GetStatus(short token)
  31. {
  32. return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token);
  33. }
  34. void System.Threading.Tasks.Sources.IValueTaskSource.GetResult(short token)
  35. {
  36. ((IUniTaskSource)this).GetResult(token);
  37. }
  38. void System.Threading.Tasks.Sources.IValueTaskSource.OnCompleted(Action<object> continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags)
  39. {
  40. // ignore flags, always none.
  41. ((IUniTaskSource)this).OnCompleted(continuation, state, token);
  42. }
  43. #endif
  44. }
  45. public interface IUniTaskSource<out T> : IUniTaskSource
  46. #if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
  47. , System.Threading.Tasks.Sources.IValueTaskSource<T>
  48. #endif
  49. {
  50. new T GetResult(short token);
  51. #if !UNITY_2018_3_OR_NEWER && !NETSTANDARD2_0
  52. new public UniTaskStatus GetStatus(short token)
  53. {
  54. return ((IUniTaskSource)this).GetStatus(token);
  55. }
  56. new public void OnCompleted(Action<object> continuation, object state, short token)
  57. {
  58. ((IUniTaskSource)this).OnCompleted(continuation, state, token);
  59. }
  60. System.Threading.Tasks.Sources.ValueTaskSourceStatus System.Threading.Tasks.Sources.IValueTaskSource<T>.GetStatus(short token)
  61. {
  62. return (System.Threading.Tasks.Sources.ValueTaskSourceStatus)(int)((IUniTaskSource)this).GetStatus(token);
  63. }
  64. T System.Threading.Tasks.Sources.IValueTaskSource<T>.GetResult(short token)
  65. {
  66. return ((IUniTaskSource<T>)this).GetResult(token);
  67. }
  68. void System.Threading.Tasks.Sources.IValueTaskSource<T>.OnCompleted(Action<object> continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags)
  69. {
  70. // ignore flags, always none.
  71. ((IUniTaskSource)this).OnCompleted(continuation, state, token);
  72. }
  73. #endif
  74. }
  75. public static class UniTaskStatusExtensions
  76. {
  77. /// <summary>status != Pending.</summary>
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static bool IsCompleted(this UniTaskStatus status)
  80. {
  81. return status != UniTaskStatus.Pending;
  82. }
  83. /// <summary>status == Succeeded.</summary>
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. public static bool IsCompletedSuccessfully(this UniTaskStatus status)
  86. {
  87. return status == UniTaskStatus.Succeeded;
  88. }
  89. /// <summary>status == Canceled.</summary>
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static bool IsCanceled(this UniTaskStatus status)
  92. {
  93. return status == UniTaskStatus.Canceled;
  94. }
  95. /// <summary>status == Faulted.</summary>
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public static bool IsFaulted(this UniTaskStatus status)
  98. {
  99. return status == UniTaskStatus.Faulted;
  100. }
  101. }
  102. }