-
Notifications
You must be signed in to change notification settings - Fork 2
/
ListTaskLike.cs
106 lines (87 loc) · 3.65 KB
/
ListTaskLike.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace NullableTaskLike
{
[Tasklike(typeof(ListTaskMethodBuilder<>))]
public class ListTaskLike<TResult> : List<TResult>
{
public bool IsCompleted => false;
public bool IsCompletedSuccessfully => false;
public bool IsFaulted => false;
public bool IsCanceled => false;
public ListTaskAwaiter<TResult> GetAwaiter() => new ListTaskAwaiter<TResult>(this);
public TResult Result => this.FirstOrDefault();
}
public static class ListTaskLike
{
public static Yielder<T> Yield<T>(T value) => new Yielder<T>(value);
public static Breaker<T> Break<T>() => new Breaker<T>();
}
public struct ListTaskAwaiter<TResult> : ICriticalNotifyCompletion
{
internal readonly ListTaskLike<TResult> _list;
internal ListTaskAwaiter(ListTaskLike<TResult> list) { _list = list; }
public bool IsCompleted => _list.IsCompleted;
public TResult GetResult() => _list.Result;
public void OnCompleted(Action continuation) => continuation();
public void UnsafeOnCompleted(Action continuation) => continuation();
}
public class Yielder<TResult> : ICriticalNotifyCompletion
{
internal TResult Value;
internal Yielder(TResult value) { Value = value; }
public bool IsCompleted => false;
public Yielder<TResult> GetAwaiter() => this;
public void OnCompleted(Action continuation) => continuation();
public void UnsafeOnCompleted(Action continuation) => continuation();
public TResult GetResult() => Value;
}
public class Breaker<T> : ICriticalNotifyCompletion
{
internal Breaker() {}
public bool IsCompleted => false;
public Breaker<T> GetAwaiter() => this;
public void OnCompleted(Action continuation) { }
public void UnsafeOnCompleted(Action continuation) { }
public T GetResult() => default(T);
}
class ListTaskMethodBuilder<TResult>
{
internal ListTaskLike<TResult> _list = new ListTaskLike<TResult>();
internal bool _broken;
public void SetResult(TResult result)
{
if (!_broken)
_list.Add(result);
}
public ListTaskLike<TResult> Task => _list;
public static ListTaskMethodBuilder<TResult> Create() => new ListTaskMethodBuilder<TResult>();
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine => stateMachine.MoveNext();
public void SetStateMachine(IAsyncStateMachine stateMachine) { }
public void SetException(Exception exception) { }
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
{
}
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
{
if (awaiter is Yielder<TResult>)
{
_list.Add(((Yielder<TResult>)(object)awaiter).Value);
stateMachine.MoveNext();
}
if (awaiter is ListTaskAwaiter<TResult>)
{
var list = ((ListTaskAwaiter<TResult>) (object) awaiter)._list;
_list.AddRange(list);
}
if (awaiter is Breaker<TResult>)
{
_broken = true;
}
}
}
}