UniTask/Assets/UniRx.Async/CompilerServices/AsyncUniTaskVoidMethodBuild...

107 lines
2.9 KiB
C#
Raw Normal View History

2019-05-19 23:14:47 +08:00
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Security;
namespace UniRx.Async.CompilerServices
{
public struct AsyncUniTaskVoidMethodBuilder
{
2020-04-18 04:07:59 +08:00
IMoveNextRunner runner;
2019-05-19 23:14:47 +08:00
// 1. Static Create method.
[DebuggerHidden]
2020-04-18 04:07:59 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2019-05-19 23:14:47 +08:00
public static AsyncUniTaskVoidMethodBuilder Create()
{
2020-04-18 04:07:59 +08:00
return default;
2019-05-19 23:14:47 +08:00
}
// 2. TaskLike Task property(void)
2020-04-18 04:07:59 +08:00
public UniTaskVoid Task
{
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return default;
}
}
2019-05-19 23:14:47 +08:00
// 3. SetException
[DebuggerHidden]
public void SetException(Exception exception)
{
2020-04-18 04:07:59 +08:00
// runner is finished, return first.
if (runner != null)
{
runner.Return();
runner = null;
}
2019-05-19 23:14:47 +08:00
UniTaskScheduler.PublishUnobservedTaskException(exception);
}
// 4. SetResult
[DebuggerHidden]
public void SetResult()
{
2020-04-18 04:07:59 +08:00
// runner is finished, return.
if (runner != null)
{
runner.Return();
runner = null;
}
2019-05-19 23:14:47 +08:00
}
// 5. AwaitOnCompleted
[DebuggerHidden]
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine
{
2020-04-18 04:07:59 +08:00
if (runner == null)
2019-05-19 23:14:47 +08:00
{
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
2019-05-19 23:14:47 +08:00
}
2020-04-18 04:07:59 +08:00
awaiter.OnCompleted(runner.CallMoveNext);
2019-05-19 23:14:47 +08:00
}
// 6. AwaitUnsafeOnCompleted
[DebuggerHidden]
[SecuritySafeCritical]
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine
{
2020-04-18 04:07:59 +08:00
if (runner == null)
2019-05-19 23:14:47 +08:00
{
runner = MoveNextRunner<TStateMachine>.Create(ref stateMachine);
2019-05-19 23:14:47 +08:00
}
2020-04-18 04:07:59 +08:00
awaiter.OnCompleted(runner.CallMoveNext);
2019-05-19 23:14:47 +08:00
}
// 7. Start
[DebuggerHidden]
public void Start<TStateMachine>(ref TStateMachine stateMachine)
where TStateMachine : IAsyncStateMachine
{
stateMachine.MoveNext();
}
// 8. SetStateMachine
[DebuggerHidden]
public void SetStateMachine(IAsyncStateMachine stateMachine)
{
2020-04-18 04:07:59 +08:00
// don't use boxed stateMachine.
2019-05-19 23:14:47 +08:00
}
}
}
#endif