Skip to content

Commit adc9699

Browse files
gustywallymathieu
authored andcommitted
+ (Value)Task.recover
1 parent bd78b35 commit adc9699

2 files changed

Lines changed: 42 additions & 29 deletions

File tree

src/FSharpPlus/Extensions/Task.fs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@ module Task =
3939
/// <returns>A Task that is completed successfully with the specified value.</returns>
4040
let result (value: 'T) : Task<'T> = Task.FromResult value
4141

42-
43-
/// <summary>Creates a Task that's completed unsuccessfully with the specified exception.</summary>
44-
/// <param name="exn">The exception to be raised.</param>
45-
/// <returns>A Task that is completed unsuccessfully with the specified exception.</returns>
46-
let raise<'T> (exn: exn) : Task<'T> =
47-
#if NET5_0_OR_GREATER
48-
Task.FromException<'T> exn
49-
#else
50-
let tcs = TaskCompletionSource<'T> tcsOptions
51-
tcs.SetException exn
52-
tcs.Task
53-
#endif
54-
5542
/// <summary>Creates a Task that's completed unsuccessfully with the specified exceptions.</summary>
5643
/// <param name="aex">The AggregateException to be raised.</param>
5744
/// <returns>A Task that is completed unsuccessfully with the specified exceptions.</returns>
@@ -483,14 +470,30 @@ module Task =
483470
/// <param name="source">The source task.</param>
484471
/// <returns>A successful resulting task.</returns>
485472
/// <remarks>The result is always a successful task, unless the mapping function itself throws an exception.</remarks>
473+
#if !NET45
474+
let inline recover ([<InlineIfLambda>]mapper: exn -> 'T) (source: Task<'T>) : Task<'T> =
475+
#else
486476
let inline recover (mapper: exn -> 'T) (source: Task<'T>) : Task<'T> =
477+
#endif
487478
#if !NET45
488479
let source = nullArgCheck (nameof source) source
489480
#else
490481
raiseIfNull "source" source
491482
#endif
492483

493-
tryWith (fun () -> source) (mapper >> result)
484+
tryWith (fun () -> source) (mapper >> Task.FromResult)
485+
486+
/// <summary>Creates a Task that's completed unsuccessfully with the specified exception.</summary>
487+
/// <param name="exn">The exception to be raised.</param>
488+
/// <returns>A Task that is completed unsuccessfully with the specified exception.</returns>
489+
let raise<'T> (exn: exn) : Task<'T> =
490+
#if NET5_0_OR_GREATER
491+
Task.FromException<'T> exn
492+
#else
493+
let tcs = TaskCompletionSource<'T> tcsOptions
494+
tcs.SetException exn
495+
tcs.Task
496+
#endif
494497

495498
/// <summary>Maps the exception of a faulted task to another exception.</summary>
496499
/// <param name="mapper">Mapping function from exception to exception.</param>
@@ -525,8 +528,6 @@ module Task =
525528
match source with
526529
| Ok x -> result x
527530
| Error exn -> raise exn
528-
529-
530531
/// Workaround to fix signatures without breaking binary compatibility.
531532
[<AutoOpen>]
532533
module Task_v2 =

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ module ValueTask =
4242
tcs.SetResult value
4343
ValueTask<'T> tcs.Task
4444
#endif
45-
46-
/// <summary>Creates a ValueTask that's completed unsuccessfully with the specified exception.</summary>
47-
/// <param name="exn">The exception to be raised.</param>
48-
/// <returns>A ValueTask that is completed unsuccessfully with the specified exception.</returns>
49-
let raise<'T> (exn: exn) : ValueTask<'T> =
50-
#if NET5_0_OR_GREATER
51-
ValueTask.FromException<'T> exn
52-
#else
53-
let tcs = TaskCompletionSource<'T> tcsOptions
54-
tcs.SetException exn
55-
ValueTask<'T> tcs.Task
56-
#endif
5745

5846
/// <summary>Creates a Task that's completed unsuccessfully with the specified exceptions.</summary>
5947
/// <param name="aex">The AggregateException to be raised.</param>
@@ -391,13 +379,37 @@ module ValueTask =
391379
/// <returns>A successful resulting task.</returns>
392380
/// <remarks>The result is always a successful task, unless the mapping function itself throws an exception.</remarks>
393381
let inline recover ([<InlineIfLambda>]mapper: exn -> 'T) (source: ValueTask<'T>) : ValueTask<'T> =
394-
tryWith (mapper >> result) (fun () -> source)
382+
#if NET5_0_OR_GREATER
383+
tryWith (mapper >> ValueTask.FromResult) (fun () -> source)
384+
#else
385+
let fromResult r =
386+
let tcs = TaskCompletionSource<'T> tcsOptions
387+
tcs.SetResult r
388+
ValueTask<'T> tcs.Task
389+
tryWith (mapper >> fromResult) (fun () -> source)
390+
#endif
391+
392+
/// <summary>Creates a ValueTask that's completed unsuccessfully with the specified exception.</summary>
393+
/// <param name="exn">The exception to be raised.</param>
394+
/// <returns>A ValueTask that is completed unsuccessfully with the specified exception.</returns>
395+
let raise<'T> (exn: exn) : ValueTask<'T> =
396+
#if NET5_0_OR_GREATER
397+
ValueTask.FromException<'T> exn
398+
#else
399+
let tcs = TaskCompletionSource<'T> tcsOptions
400+
tcs.SetException exn
401+
ValueTask<'T> tcs.Task
402+
#endif
395403

396404
/// <summary>Maps the exception of a faulted task to another exception.</summary>
397405
/// <param name="mapper">Mapping function from exception to exception.</param>
398406
/// <param name="source">The source task.</param>
399407
/// <returns>The resulting task.</returns>
408+
#if !NET45
400409
let inline mapError ([<InlineIfLambda>]mapper: exn -> exn) (source: ValueTask<'T>) : ValueTask<'T> =
410+
#else
411+
let inline mapError (mapper: exn -> exn) (source: ValueTask<'T>) : ValueTask<'T> =
412+
#endif
401413
if source.IsCompleted then
402414
match source with
403415
| Faulted exn -> FromExceptions (AggregateException (mapper exn))

0 commit comments

Comments
 (0)