-
Notifications
You must be signed in to change notification settings - Fork 17
/
ComputationBuilders.fs
152 lines (126 loc) · 6.12 KB
/
ComputationBuilders.fs
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
// Optimized (Value)Task computation expressions for F#
// Author: Nino Floris - mail@ninofloris.com
// Copyright (c) 2019 Crowded B.V.
// Distributed under the MIT License (https://opensource.org/licenses/MIT).
namespace FSharp.Control.Tasks
open System
open System.ComponentModel
open Ply
open Ply.TplPrimitives
open System.Threading.Tasks
[<EditorBrowsable(EditorBrowsableState.Never)>]
module Builders =
type TaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) : Task<'u> = runAsTask f
type UnitTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = run f
if t.IsCompletedSuccessfully then Task.CompletedTask else t.AsTask() :> Task
type ValueTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = run f
type UnitValueTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = run f
if t.IsCompletedSuccessfully then ValueTask() else ValueTask(t.AsTask() :> Task)
// Backwards compat.
[<Obsolete("Please open FSharp.Control.Tasks instead of FSharp.Control.Tasks.Builders")>]
let task = TaskBuilder()
[<Obsolete("Please open FSharp.Control.Tasks instead of FSharp.Control.Tasks.Builders")>]
let unitTask = UnitTaskBuilder()
[<Obsolete("Please open FSharp.Control.Tasks instead of FSharp.Control.Tasks.Builders")>]
let vtask = ValueTaskBuilder()
[<Obsolete("Please open FSharp.Control.Tasks instead of FSharp.Control.Tasks.Builders")>]
let unitVtask = UnitValueTaskBuilder()
module Unsafe =
type UnsafePlyBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = runUnwrappedAsPly f
type UnsafeUnitTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = runUnwrapped f
if t.IsCompletedSuccessfully then Task.CompletedTask else t.AsTask() :> Task
type UnsafeValueTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = runUnwrapped f
type UnsafeUnitValueTaskBuilder() =
inherit AffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = runUnwrapped f
if t.IsCompletedSuccessfully then ValueTask() else ValueTask(t.AsTask() :> Task)
// Backwards compat.
[<Obsolete("Please open FSharp.Control.Tasks.Affine.Unsafe instead of FSharp.Control.Tasks.Builders.Unsafe")>]
let uply = UnsafePlyBuilder()
[<Obsolete("Please open FSharp.Control.Tasks.Affine.Unsafe instead of FSharp.Control.Tasks.Builders.Unsafe")>]
let uunitTask = UnsafeUnitTaskBuilder()
[<Obsolete("Please open FSharp.Control.Tasks.Affine.Unsafe instead of FSharp.Control.Tasks.Builders.Unsafe")>]
let uvtask = UnsafeValueTaskBuilder()
[<Obsolete("Please open FSharp.Control.Tasks.Affine.Unsafe instead of FSharp.Control.Tasks.Builders.Unsafe")>]
let uunitVtask = UnsafeUnitValueTaskBuilder()
module NonAffine =
type TaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) : Task<'u> = runAsTask f
type UnitTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = run f
if t.IsCompletedSuccessfully then Task.CompletedTask else t.AsTask() :> Task
type ValueTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = run f
type UnitValueTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = run f
if t.IsCompletedSuccessfully then ValueTask() else ValueTask(t.AsTask() :> Task)
module Unsafe =
type UnsafePlyBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = runUnwrappedAsPly f
type UnsafeUnitTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = runUnwrapped f
if t.IsCompletedSuccessfully then Task.CompletedTask else t.AsTask() :> Task
type UnsafeValueTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) = runUnwrapped f
type UnsafeUnitValueTaskBuilder() =
inherit NonAffineBuilder()
member inline __.Run(f : unit -> Ply<'u>) =
let t = runUnwrapped f
if t.IsCompletedSuccessfully then ValueTask() else ValueTask(t.AsTask() :> Task)
[<AutoOpen>]
/// Defines builders that are scheduler affine, respecting the SynchronizationContext or current TaskScheduler.
/// These match C# async await behavior, when building an application you normally want to use these builders.
module Affine =
open Builders
let task = TaskBuilder()
let unitTask = UnitTaskBuilder()
let vtask = ValueTaskBuilder()
let unitVtask = UnitValueTaskBuilder()
module Unsafe =
open Unsafe
let uply = UnsafePlyBuilder()
let uunitTask = UnsafeUnitTaskBuilder()
let uvtask = UnsafeValueTaskBuilder()
let uunitVtask = UnsafeUnitValueTaskBuilder()
/// Defines builders that are free of scheduler affinity, rejecting the SynchronizationContext or current TaskScheduler.
/// Also known as Task.ConfigureAwait(false), when building a library you want to use these builders.
module NonAffine =
open Builders.NonAffine
let task = TaskBuilder()
let unitTask = UnitTaskBuilder()
let vtask = ValueTaskBuilder()
let unitVtask = UnitValueTaskBuilder()
module Unsafe =
open Unsafe
let uply = UnsafePlyBuilder()
let uunitTask = UnsafeUnitTaskBuilder()
let uvtask = UnsafeValueTaskBuilder()
let uunitVtask = UnsafeUnitValueTaskBuilder()