@@ -21,8 +21,8 @@ module ValueTask =
2121 let inline continueTask ( tcs : TaskCompletionSource < 'Result >) ( x : ValueTask < 't >) ( k : 't -> unit ) =
2222 let f = function
2323 | Succeeded r -> k r
24- | Canceled -> tcs.SetCanceled ()
25- | Faulted e -> tcs.SetException e.InnerExceptions
24+ | Faulted axn -> tcs.SetException axn.InnerExceptions
25+ | Canceled -> tcs.SetCanceled ()
2626 if x.IsCompleted then f x
2727 else x.ConfigureAwait( false ) .GetAwaiter() .UnsafeOnCompleted ( fun () -> f x)
2828
@@ -70,19 +70,10 @@ module ValueTask =
7070 /// <param name="source">The source ValueTask workflow.</param>
7171 /// <returns>The resulting ValueTask workflow.</returns>
7272 let map ( mapper : 'T -> 'U ) ( source : ValueTask < 'T >) : ValueTask < 'U > =
73- if source.IsCompleted then
74- match source with
75- | Succeeded r -> try result ( mapper r) with e -> raise e
76- | Faulted exn -> raise exn
77- | Canceled -> canceled
78- else
79- let tcs = TaskCompletionSource< 'U> ()
80- let k = function
81- | Succeeded r -> try tcs.SetResult ( mapper r) with e -> tcs.SetException e
82- | Faulted exn -> tcs.SetException exn.InnerExceptions
83- | Canceled -> tcs.SetCanceled ()
84- continueWith k source
85- ValueTask< 'U> tcs.Task
73+ task {
74+ let! r = source
75+ return mapper r
76+ } |> ValueTask< 'U>
8677
8778
8879 /// <summary>Creates a ValueTask workflow from two workflows 'x' and 'y', mapping its results with 'f'.</summary>
@@ -143,8 +134,8 @@ module ValueTask =
143134 let k ( v : ref < _ >) i t =
144135 match t with
145136 | Succeeded r -> v.Value <- r
137+ | Faulted aex -> failures[ i] <- aex.InnerExceptions
146138 | Canceled -> cancelled <- true
147- | Faulted e -> failures[ i] <- e.InnerExceptions
148139 trySet ()
149140
150141 if task1.IsCompleted && task2.IsCompleted then
@@ -191,8 +182,8 @@ module ValueTask =
191182 let k ( v : ref < _ >) i t =
192183 match t with
193184 | Succeeded r -> v.Value <- r
185+ | Faulted aex -> failures[ i] <- aex.InnerExceptions
194186 | Canceled -> cancelled <- true
195- | Faulted e -> failures[ i] <- e.InnerExceptions
196187 trySet ()
197188
198189 if task1.IsCompleted && task2.IsCompleted && task3.IsCompleted then
@@ -236,20 +227,18 @@ module ValueTask =
236227
237228 /// Flattens two nested ValueTask into one.
238229 let join ( source : ValueTask < ValueTask < 'T >>) : ValueTask < 'T > =
239- if source.IsCompleted then
240- match source with
241- | Succeeded inner -> inner
242- | Faulted aex -> raise aex
243- | Canceled -> canceled
244- else
245- let tcs = TaskCompletionSource< 'T> ()
246- continueTask tcs source ( fun inner ->
247- continueTask tcs inner tcs.SetResult)
248- ValueTask< 'T> tcs.Task
230+ task {
231+ let! inner = source
232+ return ! inner
233+ } |> ValueTask< 'T>
249234
250235
251236 /// <summary>Creates a ValueTask workflow from 'source' workflow, mapping and flattening its result with 'f'.</summary>
252- let bind ( f : 'T -> ValueTask < 'U >) ( source : ValueTask < 'T >) : ValueTask < 'U > = source |> Unchecked.nonNull |> map f |> join
237+ let bind ( f : 'T -> ValueTask < 'U >) ( source : ValueTask < 'T >) : ValueTask < 'U > =
238+ task {
239+ let! r = source
240+ return ! f r
241+ } |> ValueTask< 'U>
253242
254243 /// <summary>Creates a ValueTask that ignores the result of the source ValueTask.</summary>
255244 /// <remarks>It can be used to convert non-generic ValueTask to unit ValueTask.</remarks>
@@ -272,53 +261,17 @@ module ValueTask =
272261
273262 /// Used to de-sugar try .. with .. blocks in Computation Expressions.
274263 let inline tryWith ( [<InlineIfLambda>] compensation : exn -> ValueTask < 'T >) ( [<InlineIfLambda>] body : unit -> ValueTask < 'T >) : ValueTask < 'T > =
275- let unwrapException ( agg : AggregateException ) =
276- if agg.InnerExceptions.Count = 1 then agg.InnerExceptions.[ 0 ]
277- else agg :> Exception
278-
279- let mutable ran = false
280-
281- let compensation exn =
282- if not ran then ran <- true
283- try compensation exn
284- with e -> raise e
285-
286- let task =
287- try body ()
288- with
289- | :? AggregateException as aex -> compensation ( unwrapException aex)
290- | exn -> compensation exn
291- if ran then task
292- elif task.IsCompleted then
293- match task with
294- | Succeeded _ -> task
295- | Faulted aex -> compensation ( unwrapException aex)
296- | Canceled -> compensation ( TaskCanceledException ())
297- else
298- let tcs = TaskCompletionSource< 'T> ()
299- let f = function
300- | Succeeded r -> tcs.SetResult r
301- | Faulted aex -> continueTask tcs ( compensation ( unwrapException aex)) ( fun r -> try tcs.SetResult r with e -> tcs.SetException e)
302- | Canceled -> continueTask tcs ( compensation ( TaskCanceledException ())) ( fun r -> try tcs.SetResult r with e -> tcs.SetException e)
303- task.ConfigureAwait( false ) .GetAwaiter() .UnsafeOnCompleted ( fun () -> f task)
304- ValueTask< 'T> tcs.Task
264+ task {
265+ try return ! body ()
266+ with e -> return ! compensation e
267+ } |> ValueTask< 'T>
305268
306269 /// Used to de-sugar try .. finally .. blocks in Computation Expressions.
307270 let inline tryFinally ( [<InlineIfLambda>] compensation : unit -> unit ) ( [<InlineIfLambda>] body : unit -> ValueTask < 'T >) : ValueTask < 'T > =
308- let mutable ran = false
309- let compensation () =
310- if not ran then
311- ran <- true
312- compensation ()
313- try
314- let task = body ()
315- if task.IsCompleted then compensation (); task
316- else
317- let t = task.AsTask()
318- t.ContinueWith( fun ( _ : Task < 'T >) -> compensation (); t) .Unwrap () |> ValueTask< 'T>
319- with _ ->
320- compensation ()
321- reraise ()
271+ task {
272+ try return ! body ()
273+ finally compensation ()
274+ } |> ValueTask< 'T>
322275
323276 /// Used to de-sugar use .. blocks in Computation Expressions.
324277 let inline using ( disp : 'T when 'T :> IDisposable ) ( [<InlineIfLambda>] body : 'T -> ValueTask < 'U >) =
0 commit comments