Skip to content

Commit 5b61ffc

Browse files
committed
Remove FromX methods from ValueTask
1 parent 68588db commit 5b61ffc

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

src/FSharpPlus/Control/Monad.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ type Return =
145145
static member Return (_: 'T Task , _: Return ) = fun x -> Task.FromResult x : 'T Task
146146
#endif
147147
#if NETSTANDARD2_1 && !FABLE_COMPILER
148-
static member Return (_: 'T ValueTask , _: Return ) = fun x -> ValueTask.FromResult x : 'T ValueTask
148+
static member Return (_: 'T ValueTask , _: Return ) = fun (x: 'T) -> ValueTask<'T> x : 'T ValueTask
149149
#endif
150150
static member Return (_: option<'a> , _: Return ) = fun x -> Some x : option<'a>
151151
static member Return (_ : voption<'a> , _: Return ) = fun x -> ValueSome x : voption<'a>

src/FSharpPlus/Extensions/ValueTask.fs

+5-26
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,14 @@ namespace FSharpPlus
55
/// Additional operations on ValueTask<'T>
66
[<RequireQualifiedAccess>]
77
module ValueTask =
8-
9-
open System.Threading
8+
109
open System.Threading.Tasks
1110

1211
let inline internal (|Succeeded|Canceled|Faulted|) (t: ValueTask<'T>) =
1312
if t.IsCompletedSuccessfully then Succeeded t.Result
1413
elif t.IsCanceled then Canceled
15-
else Faulted (t.AsTask().Exception.InnerExceptions)
16-
17-
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed successfully with the specified result.</summary>
18-
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
19-
/// <param name="result">The result to store into the completed task.</param>
20-
/// <returns>The successfully completed task.</returns>
21-
let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result
22-
23-
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed exceptionally with the specified exception.</summary>
24-
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
25-
/// <param name="exception">The exception with which to complete the task.</param>
26-
/// <returns>The faulted task.</returns>
27-
let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)
14+
else Faulted (t.AsTask().Exception.InnerExceptions)
2815

29-
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed due to cancellation with the specified token.</summary>
30-
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
31-
/// <param name="cancellationToken">The token with which to complete the task.</param>
32-
/// <returns>The canceled task.</returns>
33-
let FromCanceled<'TResult> (cancellationToken: CancellationToken) = ValueTask<'TResult> (Task.FromCanceled<'TResult> cancellationToken)
34-
35-
/// <summary>Creates a <see cref="ValueTask{TResult}"/> from a <see cref="Task{TResult}"/>.</summary>
36-
/// <param name="source">Task workflow.</param>
37-
let FromTask<'TResult> (source: Task<'TResult>) = ValueTask<'TResult> source
38-
3916
let inline internal continueTask (tcs: TaskCompletionSource<'Result>) (x: ValueTask<'t>) (k: 't -> unit) =
4017
let f = function
4118
| Succeeded r -> k r
@@ -47,6 +24,8 @@ module ValueTask =
4724
aw.OnCompleted (fun () -> f x)
4825

4926
/// <summary>Creates a ValueTask workflow from 'source' another, mapping its result with 'f'.</summary>
27+
/// <param name="f">The mapping function.</param>
28+
/// <param name="source">ValueTask workflow.</param>
5029
let map (f: 'T -> 'U) (source: ValueTask<'T>) : ValueTask<'U> =
5130
let tcs = TaskCompletionSource<'U> ()
5231
continueTask tcs source (fun x ->
@@ -133,6 +112,6 @@ module ValueTask =
133112

134113

135114
/// Raises an exception in the ValueTask
136-
let raise (e: exn) = FromException e
115+
let raise (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)
137116

138117
#endif

tests/FSharpPlus.Tests/ValueTask.fs

+25-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,35 @@ module ValueTask =
1212

1313
exception TestException of string
1414

15+
16+
module ValueTask =
17+
18+
// Following is not available in F#6
19+
20+
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed successfully with the specified result.</summary>
21+
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
22+
/// <param name="result">The result to store into the completed task.</param>
23+
/// <returns>The successfully completed task.</returns>
24+
let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result
25+
26+
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed exceptionally with the specified exception.</summary>
27+
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
28+
/// <param name="exception">The exception with which to complete the task.</param>
29+
/// <returns>The faulted task.</returns>
30+
let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)
31+
32+
/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed due to cancellation with the specified token.</summary>
33+
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
34+
/// <param name="cancellationToken">The token with which to complete the task.</param>
35+
/// <returns>The canceled task.</returns>
36+
let FromCanceled<'TResult> (cancellationToken: CancellationToken) = ValueTask<'TResult> (Task.FromCanceled<'TResult> cancellationToken)
37+
1538
module ValueTaskTests =
1639

1740
let createValueTask isFailed value =
18-
if not isFailed then ValueTask.FromResult value
41+
if not isFailed then ValueTask.FromResult<_> value
1942
else
20-
ValueTask.FromException (TestException (sprintf "Ouch, can't create: %A" value ))
43+
ValueTask.FromException<_> (TestException (sprintf "Ouch, can't create: %A" value ))
2144

2245
let (|AggregateException|_|) (x: exn) =
2346
match x with

0 commit comments

Comments
 (0)