Skip to content

Commit 38d3b8b

Browse files
committed
Harmonize internal active patterns for Tasks
1 parent 558b8ea commit 38d3b8b

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/FSharpPlus/Extensions/Task.fs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ module Task =
1111
open System.Threading.Tasks
1212
open FSharpPlus.Internals.Errors
1313

14-
let private (|Canceled|Faulted|Completed|) (t: Task<'a>) =
15-
if t.IsCanceled then Canceled
16-
else if t.IsFaulted then Faulted (Unchecked.nonNull t.Exception)
17-
else Completed t.Result
14+
/// Active pattern to match the state of a completed Task
15+
let inline private (|Succeeded|Canceled|Faulted|) (t: Task<'a>) =
16+
if t.IsCompletedSuccessfully then Succeeded t.Result
17+
elif t.IsFaulted then Faulted (Unchecked.nonNull (t.Exception))
18+
elif t.IsCanceled then Canceled
19+
else invalidOp "Internal error: The task is not yet completed."
1820

1921
/// <summary>Creates a task workflow from 'source' another, mapping its result with 'f'.</summary>
2022
let map (f: 'T -> 'U) (source: Task<'T>) : Task<'U> =
@@ -38,7 +40,7 @@ module Task =
3840
let k = function
3941
| Canceled -> tcs.SetCanceled ()
4042
| Faulted e -> tcs.SetException e.InnerExceptions
41-
| Completed r ->
43+
| Succeeded r ->
4244
try tcs.SetResult (f r)
4345
with e -> tcs.SetException e
4446
source.ContinueWith k |> ignore
@@ -70,15 +72,15 @@ module Task =
7072
let k = function
7173
| Canceled -> tcs.SetCanceled ()
7274
| Faulted e -> tcs.SetException e.InnerExceptions
73-
| Completed r ->
75+
| Succeeded r ->
7476
try tcs.SetResult (f x.Result r)
7577
with e -> tcs.SetException e
7678
y.ContinueWith k |> ignore
7779
| _, TaskStatus.RanToCompletion ->
7880
let k = function
7981
| Canceled -> tcs.SetCanceled ()
8082
| Faulted e -> tcs.SetException e.InnerExceptions
81-
| Completed r ->
83+
| Succeeded r ->
8284
try tcs.SetResult (f r y.Result)
8385
with e -> tcs.SetException e
8486
x.ContinueWith k |> ignore
@@ -87,12 +89,12 @@ module Task =
8789
function
8890
| Canceled -> tcs.SetCanceled ()
8991
| Faulted e -> tcs.SetException e.InnerExceptions
90-
| Completed r ->
92+
| Succeeded r ->
9193
y.ContinueWith (
9294
function
9395
| Canceled -> tcs.SetCanceled ()
9496
| Faulted e -> tcs.SetException e.InnerExceptions
95-
| Completed r' ->
97+
| Succeeded r' ->
9698
try tcs.SetResult (f r r')
9799
with e -> tcs.SetException e
98100
) |> ignore) |> ignore
@@ -129,17 +131,17 @@ module Task =
129131
function
130132
| Canceled -> tcs.SetCanceled ()
131133
| Faulted e -> tcs.SetException e.InnerExceptions
132-
| Completed r ->
134+
| Succeeded r ->
133135
y.ContinueWith (
134136
function
135137
| Canceled -> tcs.SetCanceled ()
136138
| Faulted e -> tcs.SetException e.InnerExceptions
137-
| Completed r' ->
139+
| Succeeded r' ->
138140
z.ContinueWith (
139141
function
140142
| Canceled -> tcs.SetCanceled ()
141143
| Faulted e -> tcs.SetException e.InnerExceptions
142-
| Completed r'' ->
144+
| Succeeded r'' ->
143145
try tcs.SetResult (f r r' r'')
144146
with e -> tcs.SetException e
145147
) |> ignore) |> ignore) |> ignore
@@ -183,7 +185,7 @@ module Task =
183185
match t with
184186
| Canceled -> cancelled <- true
185187
| Faulted e -> failures[i] <- e.InnerExceptions
186-
| Completed r -> v.Value <- r
188+
| Succeeded r -> v.Value <- r
187189
trySet ()
188190

189191
if task1.IsCompleted && task2.IsCompleted then
@@ -235,7 +237,7 @@ module Task =
235237
match t with
236238
| Canceled -> cancelled <- true
237239
| Faulted e -> failures[i] <- e.InnerExceptions
238-
| Completed r -> v.Value <- r
240+
| Succeeded r -> v.Value <- r
239241
trySet ()
240242

241243
if task1.IsCompleted && task2.IsCompleted && task3.IsCompleted then
@@ -273,15 +275,15 @@ module Task =
273275
let k = function
274276
| Canceled -> tcs.SetCanceled ()
275277
| Faulted e -> tcs.SetException e.InnerExceptions
276-
| Completed r ->
278+
| Succeeded r ->
277279
try tcs.SetResult (f.Result r)
278280
with e -> tcs.SetException e
279281
x.ContinueWith k |> ignore
280282
| _, TaskStatus.RanToCompletion ->
281283
let k = function
282284
| Canceled -> tcs.SetCanceled ()
283285
| Faulted e -> tcs.SetException e.InnerExceptions
284-
| Completed r ->
286+
| Succeeded r ->
285287
try tcs.SetResult (r x.Result)
286288
with e -> tcs.SetException e
287289
f.ContinueWith k |> ignore
@@ -290,12 +292,12 @@ module Task =
290292
function
291293
| Canceled -> tcs.SetCanceled ()
292294
| Faulted e -> tcs.SetException e.InnerExceptions
293-
| Completed r ->
295+
| Succeeded r ->
294296
x.ContinueWith (
295297
function
296298
| Canceled -> tcs.SetCanceled ()
297299
| Faulted e -> tcs.SetException e.InnerExceptions
298-
| Completed r' ->
300+
| Succeeded r' ->
299301
try tcs.SetResult (r r')
300302
with e -> tcs.SetException e
301303
) |> ignore) |> ignore
@@ -319,24 +321,24 @@ module Task =
319321
let k = function
320322
| Canceled -> tcs.SetCanceled ()
321323
| Faulted e -> tcs.SetException e.InnerExceptions
322-
| Completed r -> tcs.SetResult (x.Result, r)
324+
| Succeeded r -> tcs.SetResult (x.Result, r)
323325
y.ContinueWith k |> ignore
324326
| _, TaskStatus.RanToCompletion ->
325327
let k = function
326328
| Canceled -> tcs.SetCanceled ()
327329
| Faulted e -> tcs.SetException e.InnerExceptions
328-
| Completed r -> tcs.SetResult (r, y.Result)
330+
| Succeeded r -> tcs.SetResult (r, y.Result)
329331
x.ContinueWith k |> ignore
330332
| _, _ ->
331333
x.ContinueWith (
332334
function
333335
| Canceled -> tcs.SetCanceled ()
334336
| Faulted e -> tcs.SetException e.InnerExceptions
335-
| Completed r ->
337+
| Succeeded r ->
336338
y.ContinueWith (function
337339
| Canceled -> tcs.SetCanceled ()
338340
| Faulted e -> tcs.SetException e.InnerExceptions
339-
| Completed r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
341+
| Succeeded r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
340342
tcs.Task
341343

342344
/// <summary>Creates a task workflow from two workflows 'task1' and 'task2', tupling its results.</summary>

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ module ValueTask =
1111
open System.Threading.Tasks
1212
open FSharpPlus.Internals.Errors
1313

14+
/// Active pattern to match the state of a completed ValueTask
1415
let inline (|Succeeded|Canceled|Faulted|) (t: ValueTask<'T>) =
1516
if t.IsCompletedSuccessfully then Succeeded t.Result
17+
elif t.IsFaulted then Faulted (Unchecked.nonNull (t.AsTask().Exception))
1618
elif t.IsCanceled then Canceled
17-
else Faulted (t.AsTask().Exception |> Unchecked.nonNull)
19+
else invalidOp "Internal error: The task is not yet completed."
1820

1921
let inline continueTask (tcs: TaskCompletionSource<'Result>) (x: ValueTask<'t>) (k: 't -> unit) =
2022
let f = function

0 commit comments

Comments
 (0)