Skip to content

Commit 9b2e5e4

Browse files
committed
+ TaskBuilder tests for ValueTask
1 parent 66d13e4 commit 9b2e5e4

File tree

2 files changed

+760
-2
lines changed

2 files changed

+760
-2
lines changed

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ module ValueTask =
2525
if x.IsCompleted then f x
2626
else x.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> f x)
2727

28+
29+
let inline continueWith_ (x: ValueTask) f =
30+
if x.IsCompleted then f x
31+
else x.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> f x)
32+
33+
2834
let inline continueWith (x: ValueTask<'t>) f =
2935
if x.IsCompleted then f x
3036
else x.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> f x)
@@ -247,12 +253,22 @@ module ValueTask =
247253

248254
/// <summary>Creates a ValueTask that ignores the result of the source ValueTask.</summary>
249255
/// <remarks>It can be used to convert non-generic ValueTask to unit ValueTask.</remarks>
250-
let ignore (source: ValueTask<'T>) =
256+
let ignore (source: ValueTask) : ValueTask<unit> =
251257
if source.IsCompletedSuccessfully then
252258
source.GetAwaiter().GetResult() |> ignore
253259
Unchecked.defaultof<_>
254260
else
255-
new ValueTask (source.AsTask ())
261+
let tcs = TaskCompletionSource<unit> ()
262+
if source.IsFaulted then tcs.SetException (source.AsTask().Exception |> Unchecked.nonNull).InnerExceptions
263+
elif source.IsCanceled then tcs.SetCanceled ()
264+
elif source.IsCompleted then k source
265+
else
266+
let k (t: ValueTask) : unit =
267+
if t.IsCanceled then tcs.SetCanceled ()
268+
elif t.IsFaulted then tcs.SetException (source.AsTask().Exception |> Unchecked.nonNull).InnerExceptions
269+
else tcs.SetResult ()
270+
source.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> k source)
271+
tcs.Task |> ValueTask<unit>
256272

257273
/// Used to de-sugar try .. with .. blocks in Computation Expressions.
258274
let rec tryWith (compensation: exn -> ValueTask<'T>) (body: unit -> ValueTask<'T>) : ValueTask<'T> =

0 commit comments

Comments
 (0)