-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultHandle.cs
214 lines (188 loc) · 7.51 KB
/
ResultHandle.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma warning disable S5034, S5034
using System.Runtime.ExceptionServices;
namespace Icicle;
/// <summary>
/// Represents a handle to an action after a <see cref="TaskScope.Run"/> from a delegate added via
/// <see cref="TaskScope.Add"/>
/// </summary>
public sealed class ResultHandle : BaseHandle
{
private readonly Func<CancellationToken, ValueTask> _taskFactory;
private ValueTask? _task;
internal ResultHandle(RunToken runToken, Func<CancellationToken, ValueTask> taskFactory)
: base(runToken)
{
_taskFactory = taskFactory;
}
/// <summary>
/// Returns the current state of the <see cref="ResultHandle"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <returns>current state</returns>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle"/>
/// </exception>
public ResultHandleState GetState(RunToken token)
{
ThrowOnInvalidToken(token);
return _task switch
{
{ IsCompletedSuccessfully: true } => ResultHandleState.Succeeded,
{ IsFaulted: true } => ResultHandleState.Faulted,
_ => ResultHandleState.Terminated
};
}
/// <summary>
/// If the <see cref="ResultHandle"/> has a status of <see cref="ResultHandleState.Faulted"/> it throws the <see cref="Exception"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle"/>
/// </exception>
/// <exception cref="Exception">if the <see cref="ResultHandle"/> has faulted</exception>
public void ThrowIfFaulted(RunToken token)
{
ThrowOnInvalidToken(token);
if (_task is { IsFaulted: true } ft)
{
ExceptionDispatchInfo.Capture(ft.AsTask().Exception!.TryUnwrap()).Throw();
}
}
/// <summary>
/// Casts the <see cref="ResultHandle"/> as a <see cref="ValueTask"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle"/>
/// </exception>
public ValueTask AsValueTask(RunToken token)
{
ThrowOnInvalidToken(token);
return _task!.Value;
}
/// <inheritdoc />
internal override ValueTask Run(CancellationToken token)
{
_task = _taskFactory(token);
return _task.Value;
}
}
/// <summary>
/// Represents a handle to a result produced after a <see cref="TaskScope.Run"/> from a delegate added via
/// <see cref="TaskScope.Add{T}"/>
/// </summary>
/// <typeparam name="T">some result</typeparam>
public sealed class ResultHandle<T> : BaseHandle
{
private readonly Func<CancellationToken, ValueTask<T>> _taskFactory;
private ValueTask<T>? _task;
internal ResultHandle(RunToken runToken, Func<CancellationToken, ValueTask<T>> taskFactory)
: base(runToken)
{
_taskFactory = taskFactory;
}
/// <summary>
/// Returns the current state of the <see cref="ResultHandle{T}"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <returns>current state</returns>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle{T}"/>
/// </exception>
public ResultHandleState GetState(RunToken token)
{
ThrowOnInvalidToken(token);
return _task switch
{
{ IsCompletedSuccessfully: true } => ResultHandleState.Succeeded,
{ IsFaulted: true } => ResultHandleState.Faulted,
_ => ResultHandleState.Terminated
};
}
/// <summary>
/// Returns the <see cref="ResultHandle{T}"/> value
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <returns>T or throws</returns>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle{T}"/>
/// </exception>
/// <exception cref="Exception">if the <see cref="ResultHandle{T}"/> has faulted</exception>
/// <exception cref="TaskCanceledException">if the <see cref="ResultHandle{T}"/> was <see cref="ResultHandleState.Terminated"/></exception>
public T Value(RunToken token)
{
ThrowOnInvalidToken(token);
switch (_task)
{
case { IsCompletedSuccessfully: true } st:
return st.Result;
case { IsFaulted: true } ft:
ExceptionDispatchInfo.Capture(ft.AsTask().Exception!.TryUnwrap()).Throw();
return default!; // unreachable
default:
throw new TaskCanceledException();
}
}
/// <summary>
/// Returns the <see cref="ResultHandle{T}"/> value or default
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <returns>T or default</returns>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle{T}"/>
/// </exception>
/// <exception cref="Exception">if the <see cref="ResultHandle{T}"/> has faulted</exception>
public T? ValueOrDefault(RunToken token)
{
ThrowOnInvalidToken(token);
ThrowIfFaulted(token);
if (_task is { IsCompletedSuccessfully: true } st)
{
return st.Result;
}
return default;
}
/// <summary>
/// If the <see cref="ResultHandle{T}"/> has a status of <see cref="ResultHandleState.Faulted"/> it throws the <see cref="Exception"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle{T}"/>
/// </exception>
/// <exception cref="Exception">if the <see cref="ResultHandle{T}"/> has faulted</exception>
public void ThrowIfFaulted(RunToken token)
{
ThrowOnInvalidToken(token);
if (_task is { IsFaulted: true } ft)
{
ExceptionDispatchInfo.Capture(ft.AsTask().Exception!.TryUnwrap()).Throw();
}
}
/// <summary>
/// Casts the <see cref="ResultHandle{T}"/> as a <see cref="ValueTask{T}"/>
/// </summary>
/// <param name="token">returned proof from a call to <see cref="TaskScope.Run"/></param>
/// <exception cref="InvalidRunTokenException">
/// if provided token is not from the same <see cref="TaskScope"/> that
/// created this <see cref="ResultHandle{T}"/>
/// </exception>
public ValueTask<T> AsValueTask(RunToken token)
{
ThrowOnInvalidToken(token);
return _task!.Value;
}
#pragma warning disable AsyncFixer01
internal override async ValueTask Run(CancellationToken token)
{
_task = _taskFactory(token);
await _task.Value;
}
#pragma warning restore AsyncFixer01
}