Skip to content

Instantly share code, notes, and snippets.

@vasilkosturski
Created January 5, 2021 14:20
Show Gist options
  • Save vasilkosturski/f7ba875b152f8e17891fedd44802a18e to your computer and use it in GitHub Desktop.
Save vasilkosturski/f7ba875b152f8e17891fedd44802a18e to your computer and use it in GitHub Desktop.
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