Created
January 5, 2021 14:20
-
-
Save vasilkosturski/f7ba875b152f8e17891fedd44802a18e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct AsyncTaskMethodBuilder | |
{ | |
private IAsyncStateMachine _stateMachine; | |
private Action _moveNextRunner; | |
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) | |
where TAwaiter : ICriticalNotifyCompletion | |
where TStateMachine : IAsyncStateMachine | |
{ | |
if (_stateMachine == null) | |
{ | |
IAsyncStateMachine boxed = stateMachine; | |
boxed.SetStateMachine(boxed); | |
_moveNextRunner = () => boxed.MoveNext(); | |
} | |
awaiter.UnsafeOnCompleted(_moveNextRunner); | |
} | |
public void SetStateMachine(IAsyncStateMachine stateMachine) => _stateMachine = stateMachine; | |
} | |
public interface IAsyncStateMachine | |
{ | |
public void MoveNext(); | |
public void SetStateMachine(IAsyncStateMachine stateMachine); | |
} | |
public struct StateMachine : IAsyncStateMachine | |
{ | |
public AsyncTaskMethodBuilder _methodBuilder; | |
public void MoveNext() | |
{ | |
// Implementation | |
} | |
public void SetStateMachine(IAsyncStateMachine stateMachine) | |
{ | |
_methodBuilder.SetStateMachine(stateMachine); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment