Skip to content

Commit afea5d5

Browse files
gustywallymathieu
authored andcommitted
Harmonize internal active patterns for Tasks
1 parent 3bf4186 commit afea5d5

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

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> =
@@ -42,7 +44,7 @@ module Task =
4244
let k = function
4345
| Canceled -> tcs.SetCanceled ()
4446
| Faulted e -> tcs.SetException e.InnerExceptions
45-
| Completed r ->
47+
| Succeeded r ->
4648
try tcs.SetResult (f r)
4749
with e -> tcs.SetException e
4850
source.ContinueWith k |> ignore
@@ -79,15 +81,15 @@ module Task =
7981
let k = function
8082
| Canceled -> tcs.SetCanceled ()
8183
| Faulted e -> tcs.SetException e.InnerExceptions
82-
| Completed r ->
84+
| Succeeded r ->
8385
try tcs.SetResult (f x.Result r)
8486
with e -> tcs.SetException e
8587
y.ContinueWith k |> ignore
8688
| _, TaskStatus.RanToCompletion ->
8789
let k = function
8890
| Canceled -> tcs.SetCanceled ()
8991
| Faulted e -> tcs.SetException e.InnerExceptions
90-
| Completed r ->
92+
| Succeeded r ->
9193
try tcs.SetResult (f r y.Result)
9294
with e -> tcs.SetException e
9395
x.ContinueWith k |> ignore
@@ -96,12 +98,12 @@ module Task =
9698
function
9799
| Canceled -> tcs.SetCanceled ()
98100
| Faulted e -> tcs.SetException e.InnerExceptions
99-
| Completed r ->
101+
| Succeeded r ->
100102
y.ContinueWith (
101103
function
102104
| Canceled -> tcs.SetCanceled ()
103105
| Faulted e -> tcs.SetException e.InnerExceptions
104-
| Completed r' ->
106+
| Succeeded r' ->
105107
try tcs.SetResult (f r r')
106108
with e -> tcs.SetException e
107109
) |> ignore) |> ignore
@@ -144,17 +146,17 @@ module Task =
144146
function
145147
| Canceled -> tcs.SetCanceled ()
146148
| Faulted e -> tcs.SetException e.InnerExceptions
147-
| Completed r ->
149+
| Succeeded r ->
148150
y.ContinueWith (
149151
function
150152
| Canceled -> tcs.SetCanceled ()
151153
| Faulted e -> tcs.SetException e.InnerExceptions
152-
| Completed r' ->
154+
| Succeeded r' ->
153155
z.ContinueWith (
154156
function
155157
| Canceled -> tcs.SetCanceled ()
156158
| Faulted e -> tcs.SetException e.InnerExceptions
157-
| Completed r'' ->
159+
| Succeeded r'' ->
158160
try tcs.SetResult (f r r' r'')
159161
with e -> tcs.SetException e
160162
) |> ignore) |> ignore) |> ignore
@@ -203,7 +205,7 @@ module Task =
203205
match t with
204206
| Canceled -> cancelled <- true
205207
| Faulted e -> failures[i] <- e.InnerExceptions
206-
| Completed r -> v.Value <- r
208+
| Succeeded r -> v.Value <- r
207209
trySet ()
208210

209211
if task1.IsCompleted && task2.IsCompleted then
@@ -261,7 +263,7 @@ module Task =
261263
match t with
262264
| Canceled -> cancelled <- true
263265
| Faulted e -> failures[i] <- e.InnerExceptions
264-
| Completed r -> v.Value <- r
266+
| Succeeded r -> v.Value <- r
265267
trySet ()
266268

267269
if task1.IsCompleted && task2.IsCompleted && task3.IsCompleted then
@@ -304,15 +306,15 @@ module Task =
304306
let k = function
305307
| Canceled -> tcs.SetCanceled ()
306308
| Faulted e -> tcs.SetException e.InnerExceptions
307-
| Completed r ->
309+
| Succeeded r ->
308310
try tcs.SetResult (f.Result r)
309311
with e -> tcs.SetException e
310312
x.ContinueWith k |> ignore
311313
| _, TaskStatus.RanToCompletion ->
312314
let k = function
313315
| Canceled -> tcs.SetCanceled ()
314316
| Faulted e -> tcs.SetException e.InnerExceptions
315-
| Completed r ->
317+
| Succeeded r ->
316318
try tcs.SetResult (r x.Result)
317319
with e -> tcs.SetException e
318320
f.ContinueWith k |> ignore
@@ -321,12 +323,12 @@ module Task =
321323
function
322324
| Canceled -> tcs.SetCanceled ()
323325
| Faulted e -> tcs.SetException e.InnerExceptions
324-
| Completed r ->
326+
| Succeeded r ->
325327
x.ContinueWith (
326328
function
327329
| Canceled -> tcs.SetCanceled ()
328330
| Faulted e -> tcs.SetException e.InnerExceptions
329-
| Completed r' ->
331+
| Succeeded r' ->
330332
try tcs.SetResult (r r')
331333
with e -> tcs.SetException e
332334
) |> ignore) |> ignore
@@ -355,24 +357,24 @@ module Task =
355357
let k = function
356358
| Canceled -> tcs.SetCanceled ()
357359
| Faulted e -> tcs.SetException e.InnerExceptions
358-
| Completed r -> tcs.SetResult (x.Result, r)
360+
| Succeeded r -> tcs.SetResult (x.Result, r)
359361
y.ContinueWith k |> ignore
360362
| _, TaskStatus.RanToCompletion ->
361363
let k = function
362364
| Canceled -> tcs.SetCanceled ()
363365
| Faulted e -> tcs.SetException e.InnerExceptions
364-
| Completed r -> tcs.SetResult (r, y.Result)
366+
| Succeeded r -> tcs.SetResult (r, y.Result)
365367
x.ContinueWith k |> ignore
366368
| _, _ ->
367369
x.ContinueWith (
368370
function
369371
| Canceled -> tcs.SetCanceled ()
370372
| Faulted e -> tcs.SetException e.InnerExceptions
371-
| Completed r ->
373+
| Succeeded r ->
372374
y.ContinueWith (function
373375
| Canceled -> tcs.SetCanceled ()
374376
| Faulted e -> tcs.SetException e.InnerExceptions
375-
| Completed r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
377+
| Succeeded r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
376378
tcs.Task
377379

378380
/// <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)