-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathtasks.fs
More file actions
718 lines (621 loc) · 31.1 KB
/
tasks.fs
File metadata and controls
718 lines (621 loc) · 31.1 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// Task builder for F# that compiles to allocation-free paths for synchronous code.
//
// Originally written in 2016 by Robert Peele (humbobst@gmail.com)
// New operator-based overload resolution for F# 4.0 compatibility by Gustavo Leon in 2018.
// Revised for insertion into FSharp.Core by Microsoft, 2019.
//
// Original notice:
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights
// to this software to the public domain worldwide. This software is distributed without any warranty.
//
// Updates:
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Control
open System
open System.Runtime.CompilerServices
open System.Threading
open System.Threading.Tasks
open Microsoft.FSharp.Core
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
open Microsoft.FSharp.Collections
/// The extra data stored in ResumableStateMachine for tasks
[<Struct; NoComparison; NoEquality>]
type TaskStateMachineData<'T> =
[<DefaultValue(false)>]
val mutable Result: 'T
[<DefaultValue(false)>]
val mutable MethodBuilder: AsyncTaskMethodBuilder<'T>
and TaskStateMachine<'TOverall> = ResumableStateMachine<TaskStateMachineData<'TOverall>>
and TaskResumptionFunc<'TOverall> = ResumptionFunc<TaskStateMachineData<'TOverall>>
and TaskResumptionDynamicInfo<'TOverall> = ResumptionDynamicInfo<TaskStateMachineData<'TOverall>>
and TaskCode<'TOverall, 'T> = ResumableCode<TaskStateMachineData<'TOverall>, 'T>
type TaskBuilderBase() =
member inline _.Delay(generator: unit -> TaskCode<'TOverall, 'T>) : TaskCode<'TOverall, 'T> =
TaskCode<'TOverall, 'T>(fun sm -> (generator ()).Invoke(&sm))
/// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
[<DefaultValue>]
member inline _.Zero() : TaskCode<'TOverall, unit> =
ResumableCode.Zero()
member inline _.Return(value: 'T) : TaskCode<'T, 'T> =
TaskCode<'T, _>(fun sm ->
sm.Data.Result <- value
true)
/// Chains together a step with its following step.
/// Note that this requires that the first step has no result.
/// This prevents constructs like `task { return 1; return 2; }`.
member inline _.Combine
(task1: TaskCode<'TOverall, unit>, task2: TaskCode<'TOverall, 'T>)
: TaskCode<'TOverall, 'T> =
ResumableCode.Combine(task1, task2)
/// Builds a step that executes the body while the condition predicate is true.
member inline _.While
([<InlineIfLambda>] condition: unit -> bool, body: TaskCode<'TOverall, unit>)
: TaskCode<'TOverall, unit> =
ResumableCode.While(condition, body)
/// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
/// to retrieve the step, and in the continuation of the step (if any).
member inline _.TryWith
(body: TaskCode<'TOverall, 'T>, catch: exn -> TaskCode<'TOverall, 'T>)
: TaskCode<'TOverall, 'T> =
ResumableCode.TryWith(body, catch)
/// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
/// to retrieve the step, and in the continuation of the step (if any).
member inline _.TryFinally
(body: TaskCode<'TOverall, 'T>, [<InlineIfLambda>] compensation: unit -> unit)
: TaskCode<'TOverall, 'T> =
ResumableCode.TryFinally(
body,
ResumableCode<_, _>(fun _sm ->
compensation ()
true)
)
member inline _.For(sequence: seq<'T>, body: 'T -> TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
ResumableCode.For(sequence, body)
#if NETSTANDARD2_1
member inline internal this.TryFinallyAsync
(body: TaskCode<'TOverall, 'T>, compensation: unit -> ValueTask)
: TaskCode<'TOverall, 'T> =
ResumableCode.TryFinallyAsync(
body,
ResumableCode<_, _>(fun sm ->
if __useResumableCode then
let mutable __stack_condition_fin = true
let __stack_vtask = compensation ()
if not __stack_vtask.IsCompleted then
let mutable awaiter = __stack_vtask.GetAwaiter()
let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
__stack_condition_fin <- __stack_yield_fin
if not __stack_condition_fin then
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
__stack_condition_fin
else
let vtask = compensation ()
let mutable awaiter = vtask.GetAwaiter()
let cont =
TaskResumptionFunc<'TOverall>(fun sm ->
awaiter.GetResult() |> ignore
true)
// shortcut to continue immediately
if awaiter.IsCompleted then
true
else
sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
sm.ResumptionDynamicInfo.ResumptionFunc <- cont
false)
)
member inline this.Using<'Resource, 'TOverall, 'T when 'Resource :> IAsyncDisposable | null>
(resource: 'Resource, body: 'Resource -> TaskCode<'TOverall, 'T>)
: TaskCode<'TOverall, 'T> =
this.TryFinallyAsync(
(fun sm -> (body resource).Invoke(&sm)),
(fun () ->
if not (isNull (box resource)) then
resource.DisposeAsync()
else
ValueTask())
)
#endif
type TaskBuilder() =
inherit TaskBuilderBase()
// This is the dynamic implementation - this is not used
// for statically compiled tasks. An executor (resumptionFuncExecutor) is
// registered with the state machine, plus the initial resumption.
// The executor stays constant throughout the execution, it wraps each step
// of the execution in a try/with. The resumption is changed at each step
// to represent the continuation of the computation.
static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> =
let mutable sm = TaskStateMachine<'T>()
let initialResumptionFunc = TaskResumptionFunc<'T>(fun sm -> code.Invoke(&sm))
let resumptionInfo =
{ new TaskResumptionDynamicInfo<'T>(initialResumptionFunc) with
member info.MoveNext(sm) =
let mutable savedExn = null
try
sm.ResumptionDynamicInfo.ResumptionData <- null
let step = info.ResumptionFunc.Invoke(&sm)
if step then
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
else
let mutable awaiter =
sm.ResumptionDynamicInfo.ResumptionData :?> ICriticalNotifyCompletion
assert not (isNull awaiter)
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
with exn ->
savedExn <- exn
// Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
match savedExn with
| null -> ()
| exn -> sm.Data.MethodBuilder.SetException exn
member _.SetStateMachine(sm, state) =
sm.Data.MethodBuilder.SetStateMachine(state)
}
sm.ResumptionDynamicInfo <- resumptionInfo
sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
sm.Data.MethodBuilder.Start(&sm)
sm.Data.MethodBuilder.Task
member inline _.Run(code: TaskCode<'T, 'T>) : Task<'T> =
if __useResumableCode then
__stateMachine<TaskStateMachineData<'T>, Task<'T>>
(MoveNextMethodImpl<_>(fun sm ->
//-- RESUMABLE CODE START
__resumeAt sm.ResumptionPoint
let mutable __stack_exn: Exception = null
try
let __stack_code_fin = code.Invoke(&sm)
if __stack_code_fin then
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
with exn ->
__stack_exn <- exn
// Run SetException outside the stack unwind, see https://github.com/dotnet/roslyn/issues/26567
match __stack_exn with
| null -> ()
| exn -> sm.Data.MethodBuilder.SetException exn
//-- RESUMABLE CODE END
))
(SetStateMachineMethodImpl<_>(fun sm state -> sm.Data.MethodBuilder.SetStateMachine(state)))
(AfterCode<_, _>(fun sm ->
sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
sm.Data.MethodBuilder.Start(&sm)
sm.Data.MethodBuilder.Task))
else
TaskBuilder.RunDynamic(code)
type BackgroundTaskBuilder() =
inherit TaskBuilderBase()
static member RunDynamic(code: TaskCode<'T, 'T>) : Task<'T> =
// backgroundTask { .. } escapes to a background thread where necessary
// See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/
if
isNull SynchronizationContext.Current
&& obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default)
then
TaskBuilder.RunDynamic(code)
else
Task.Run<'T>(fun () -> TaskBuilder.RunDynamic(code))
//// Same as TaskBuilder.Run except the start is inside Task.Run if necessary
member inline _.Run(code: TaskCode<'T, 'T>) : Task<'T> =
if __useResumableCode then
__stateMachine<TaskStateMachineData<'T>, Task<'T>>
(MoveNextMethodImpl<_>(fun sm ->
//-- RESUMABLE CODE START
__resumeAt sm.ResumptionPoint
try
let __stack_code_fin = code.Invoke(&sm)
if __stack_code_fin then
sm.Data.MethodBuilder.SetResult(sm.Data.Result)
with exn ->
sm.Data.MethodBuilder.SetException exn
//-- RESUMABLE CODE END
))
(SetStateMachineMethodImpl<_>(fun sm state -> sm.Data.MethodBuilder.SetStateMachine(state)))
(AfterCode<_, Task<'T>>(fun sm ->
// backgroundTask { .. } escapes to a background thread where necessary
// See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/
if
isNull SynchronizationContext.Current
&& obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default)
then
sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
sm.Data.MethodBuilder.Start(&sm)
sm.Data.MethodBuilder.Task
else
let sm = sm // copy contents of state machine so we can capture it
Task.Run<'T>(fun () ->
let mutable sm = sm // host local mutable copy of contents of state machine on this thread pool thread
sm.Data.MethodBuilder <- AsyncTaskMethodBuilder<'T>.Create()
sm.Data.MethodBuilder.Start(&sm)
sm.Data.MethodBuilder.Task)))
else
BackgroundTaskBuilder.RunDynamic(code)
module TaskBuilder =
let task = TaskBuilder()
let backgroundTask = BackgroundTaskBuilder()
namespace Microsoft.FSharp.Control.TaskBuilderExtensions
open Microsoft.FSharp.Control
open System
open System.Runtime.CompilerServices
open System.Threading.Tasks
open Microsoft.FSharp.Core
open Microsoft.FSharp.Core.CompilerServices
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
module LowPriority =
// Low priority extensions
type TaskBuilderBase with
[<NoEagerConstraintApplication>]
static member inline BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall
when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
and ^Awaiter :> ICriticalNotifyCompletion
and ^Awaiter: (member get_IsCompleted: unit -> bool)
and ^Awaiter: (member GetResult: unit -> 'TResult1)>
(sm: byref<_>, task: ^TaskLike, continuation: 'TResult1 -> TaskCode<'TOverall, 'TResult2>)
: bool =
let mutable awaiter = (^TaskLike: (member GetAwaiter: unit -> ^Awaiter) task)
let cont =
TaskResumptionFunc<'TOverall>(fun sm ->
let result = (^Awaiter: (member GetResult: unit -> 'TResult1) awaiter)
(continuation result).Invoke(&sm))
// shortcut to continue immediately
if (^Awaiter: (member get_IsCompleted: unit -> bool) awaiter) then
cont.Invoke(&sm)
else
sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
sm.ResumptionDynamicInfo.ResumptionFunc <- cont
false
[<NoEagerConstraintApplication>]
member inline _.Bind< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall
when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
and ^Awaiter :> ICriticalNotifyCompletion
and ^Awaiter: (member get_IsCompleted: unit -> bool)
and ^Awaiter: (member GetResult: unit -> 'TResult1)>
(task: ^TaskLike, continuation: 'TResult1 -> TaskCode<'TOverall, 'TResult2>)
: TaskCode<'TOverall, 'TResult2> =
TaskCode<'TOverall, _>(fun sm ->
if __useResumableCode then
//-- RESUMABLE CODE START
// Get an awaiter from the awaitable
let mutable awaiter = (^TaskLike: (member GetAwaiter: unit -> ^Awaiter) task)
let mutable __stack_fin = true
if not (^Awaiter: (member get_IsCompleted: unit -> bool) awaiter) then
// This will yield with __stack_yield_fin = false
// This will resume with __stack_yield_fin = true
let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
__stack_fin <- __stack_yield_fin
if __stack_fin then
let result = (^Awaiter: (member GetResult: unit -> 'TResult1) awaiter)
(continuation result).Invoke(&sm)
else
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
false
else
TaskBuilderBase.BindDynamic< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall>(
&sm,
task,
continuation
)
//-- RESUMABLE CODE END
)
[<NoEagerConstraintApplication>]
member inline this.ReturnFrom< ^TaskLike, ^Awaiter, 'T
when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
and ^Awaiter :> ICriticalNotifyCompletion
and ^Awaiter: (member get_IsCompleted: unit -> bool)
and ^Awaiter: (member GetResult: unit -> 'T)>
(task: ^TaskLike)
: TaskCode<'T, 'T> =
this.Bind(task, this.Return)
member inline _.Using<'Resource, 'TOverall, 'T when 'Resource :> IDisposable | null>
(resource: 'Resource, body: 'Resource -> TaskCode<'TOverall, 'T>)
=
ResumableCode.Using(resource, body)
type TaskBuilder with
member inline this.MergeSources< ^TaskLike1, ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter1, ^Awaiter2
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> ^TResult1)
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> ^TResult2)>
(task1: ^TaskLike1, task2: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
type BackgroundTaskBuilder with
member inline this.MergeSources< ^TaskLike1, ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter1, ^Awaiter2
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> ^TResult1)
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> ^TResult2)>
(task1: ^TaskLike1, task2: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
module HighPriority =
// High priority extensions
type TaskBuilderBase with
static member BindDynamic
(sm: byref<_>, task: Task<'TResult1>, continuation: 'TResult1 -> TaskCode<'TOverall, 'TResult2>)
: bool =
let mutable awaiter = task.GetAwaiter()
let cont =
TaskResumptionFunc<'TOverall>(fun sm ->
let result = awaiter.GetResult()
(continuation result).Invoke(&sm))
// shortcut to continue immediately
if awaiter.IsCompleted then
cont.Invoke(&sm)
else
sm.ResumptionDynamicInfo.ResumptionData <- (awaiter :> ICriticalNotifyCompletion)
sm.ResumptionDynamicInfo.ResumptionFunc <- cont
false
member inline _.Bind
(task: Task<'TResult1>, continuation: 'TResult1 -> TaskCode<'TOverall, 'TResult2>)
: TaskCode<'TOverall, 'TResult2> =
TaskCode<'TOverall, _>(fun sm ->
if __useResumableCode then
//-- RESUMABLE CODE START
// Get an awaiter from the task
let mutable awaiter = task.GetAwaiter()
let mutable __stack_fin = true
if not awaiter.IsCompleted then
// This will yield with __stack_yield_fin = false
// This will resume with __stack_yield_fin = true
let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm)
__stack_fin <- __stack_yield_fin
if __stack_fin then
let result = awaiter.GetResult()
(continuation result).Invoke(&sm)
else
sm.Data.MethodBuilder.AwaitUnsafeOnCompleted(&awaiter, &sm)
false
else
TaskBuilderBase.BindDynamic(&sm, task, continuation)
//-- RESUMABLE CODE END
)
member inline this.ReturnFrom(task: Task<'T>) : TaskCode<'T, 'T> =
this.Bind(task, this.Return)
type TaskBuilder with
// This overload is required for type inference in tasks cases
member inline this.MergeSources
(task1: Task< ^TResult1 >, task2: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
type BackgroundTaskBuilder with
// This overload is required for type inference in tasks cases
member inline this.MergeSources
(task1: Task< ^TResult1 >, task2: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
module MediumPriority =
open LowPriority
open HighPriority
// Medium priority extensions
type TaskBuilderBase with
member inline this.Bind
(computation: Async<'TResult1>, continuation: 'TResult1 -> TaskCode<'TOverall, 'TResult2>)
: TaskCode<'TOverall, 'TResult2> =
this.Bind(Async.StartImmediateAsTask computation, continuation)
member inline this.ReturnFrom(computation: Async<'T>) : TaskCode<'T, 'T> =
this.ReturnFrom(Async.StartImmediateAsTask computation)
type TaskBuilder with
// This overload is required for type inference in tasks cases
member inline this.MergeSources< ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter2
when ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> 'TResult2)>
(task1: Task< ^TResult1 >, task2: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in tasks cases
member inline this.MergeSources< ^TaskLike1, ^TResult1, ^TResult2, ^Awaiter1
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> 'TResult1)>
(task1: ^TaskLike1, task2: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async cases
member inline this.MergeSources
(computation1: Async< ^TResult1 >, computation2: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation1,
fun (result1: ^TResult1) ->
this.Bind(computation2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in task + async cases
member inline this.MergeSources
(task: Task< ^TResult1 >, computation: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task,
fun (result1: ^TResult1) ->
this.Bind(computation, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async + task case
member inline this.MergeSources
(computation: Async< ^TResult1 >, task: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation,
fun (result1: ^TResult1) ->
this.Bind(task, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
type BackgroundTaskBuilder with
// This overload is required for type inference in tasks cases
member inline this.MergeSources< ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter2
when ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> 'TResult2)>
(task1: Task< ^TResult1 >, task2: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in tasks cases
member inline this.MergeSources< ^TaskLike1, ^TResult1, ^TResult2, ^Awaiter1
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> 'TResult1)>
(task1: ^TaskLike1, task2: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task1,
fun (result1: ^TResult1) ->
this.Bind(task2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async cases
member inline this.MergeSources
(computation1: Async< ^TResult1 >, computation2: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation1,
fun (result1: ^TResult1) ->
this.Bind(computation2, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in task + async cases
member inline this.MergeSources
(task: Task< ^TResult1 >, computation: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task,
fun (result1: ^TResult1) ->
this.Bind(computation, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async + task case
member inline this.MergeSources
(computation: Async< ^TResult1 >, task: Task< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation,
fun (result1: ^TResult1) ->
this.Bind(task, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
module LowPlusPriority =
open LowPriority
open MediumPriority
type TaskBuilder with
// This overload is required for type inference in async cases
member inline this.MergeSources< ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter2
when ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> 'TResult2)>
(computation: Async< ^TResult1 >, task: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation,
fun (result1: ^TResult1) ->
this.Bind(task, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async cases
member inline this.MergeSources< ^TaskLike1, ^TResult1, ^TResult2, ^Awaiter1
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> 'TResult1)>
(task: ^TaskLike1, computation: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task,
fun (result1: ^TResult1) ->
this.Bind(computation, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
type BackgroundTaskBuilder with
// This overload is required for type inference in async cases
member inline this.MergeSources< ^TaskLike2, ^TResult1, ^TResult2, ^Awaiter2
when ^TaskLike2: (member GetAwaiter: unit -> ^Awaiter2)
and ^Awaiter2 :> ICriticalNotifyCompletion
and ^Awaiter2: (member get_IsCompleted: unit -> bool)
and ^Awaiter2: (member GetResult: unit -> 'TResult2)>
(computation: Async< ^TResult1 >, task: ^TaskLike2)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
computation,
fun (result1: ^TResult1) ->
this.Bind(task, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)
// This overload is required for type inference in async cases
member inline this.MergeSources< ^TaskLike1, ^TResult1, ^TResult2, ^Awaiter1
when ^TaskLike1: (member GetAwaiter: unit -> ^Awaiter1)
and ^Awaiter1 :> ICriticalNotifyCompletion
and ^Awaiter1: (member get_IsCompleted: unit -> bool)
and ^Awaiter1: (member GetResult: unit -> 'TResult1)>
(task: ^TaskLike1, computation: Async< ^TResult2 >)
: Task<struct (^TResult1 * ^TResult2)> =
this.Run(
this.Bind(
task,
fun (result1: ^TResult1) ->
this.Bind(computation, fun (result2: ^TResult2) -> this.Return struct (result1, result2))
)
)