From ad56a34478b6eedbad08dcdc1c99924a544f7534 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Fri, 2 Dec 2022 07:43:13 +0100 Subject: [PATCH] Remove FromX methods from ValueTask --- src/FSharpPlus/Control/Monad.fs | 2 +- src/FSharpPlus/Extensions/ValueTask.fs | 31 +++++--------------------- tests/FSharpPlus.Tests/ValueTask.fs | 28 +++++++++++++++++++++-- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/FSharpPlus/Control/Monad.fs b/src/FSharpPlus/Control/Monad.fs index 3e8f0e5d0..89635d765 100644 --- a/src/FSharpPlus/Control/Monad.fs +++ b/src/FSharpPlus/Control/Monad.fs @@ -145,7 +145,7 @@ type Return = static member Return (_: 'T Task , _: Return ) = fun x -> Task.FromResult x : 'T Task #endif #if NETSTANDARD2_1 && !FABLE_COMPILER - static member Return (_: 'T ValueTask , _: Return ) = fun x -> ValueTask.FromResult x : 'T ValueTask + static member Return (_: 'T ValueTask , _: Return ) = fun (x: 'T) -> ValueTask<'T> x : 'T ValueTask #endif static member Return (_: option<'a> , _: Return ) = fun x -> Some x : option<'a> static member Return (_ : voption<'a> , _: Return ) = fun x -> ValueSome x : voption<'a> diff --git a/src/FSharpPlus/Extensions/ValueTask.fs b/src/FSharpPlus/Extensions/ValueTask.fs index 26ead785a..48030d193 100644 --- a/src/FSharpPlus/Extensions/ValueTask.fs +++ b/src/FSharpPlus/Extensions/ValueTask.fs @@ -5,37 +5,14 @@ namespace FSharpPlus /// Additional operations on ValueTask<'T> [] module ValueTask = - - open System.Threading + open System.Threading.Tasks let inline internal (|Succeeded|Canceled|Faulted|) (t: ValueTask<'T>) = if t.IsCompletedSuccessfully then Succeeded t.Result elif t.IsCanceled then Canceled - else Faulted (t.AsTask().Exception.InnerExceptions) - - /// Creates a that's completed successfully with the specified result. - /// The type of the result returned by the task. - /// The result to store into the completed task. - /// The successfully completed task. - let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result - - /// Creates a that's completed exceptionally with the specified exception. - /// The type of the result returned by the task. - /// The exception with which to complete the task. - /// The faulted task. - let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``) + else Faulted (t.AsTask().Exception.InnerExceptions) - /// Creates a that's completed due to cancellation with the specified token. - /// The type of the result returned by the task. - /// The token with which to complete the task. - /// The canceled task. - let FromCanceled<'TResult> (cancellationToken: CancellationToken) = ValueTask<'TResult> (Task.FromCanceled<'TResult> cancellationToken) - - /// Creates a from a . - /// Task workflow. - let FromTask<'TResult> (source: Task<'TResult>) = ValueTask<'TResult> source - let inline internal continueTask (tcs: TaskCompletionSource<'Result>) (x: ValueTask<'t>) (k: 't -> unit) = let f = function | Succeeded r -> k r @@ -47,6 +24,8 @@ module ValueTask = aw.OnCompleted (fun () -> f x) /// Creates a ValueTask workflow from 'source' another, mapping its result with 'f'. + /// The mapping function. + /// ValueTask workflow. let map (f: 'T -> 'U) (source: ValueTask<'T>) : ValueTask<'U> = let tcs = TaskCompletionSource<'U> () continueTask tcs source (fun x -> @@ -133,6 +112,6 @@ module ValueTask = /// Raises an exception in the ValueTask - let raise (e: exn) = FromException e + let raise (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``) #endif \ No newline at end of file diff --git a/tests/FSharpPlus.Tests/ValueTask.fs b/tests/FSharpPlus.Tests/ValueTask.fs index 0593b605a..af8d8b4dc 100644 --- a/tests/FSharpPlus.Tests/ValueTask.fs +++ b/tests/FSharpPlus.Tests/ValueTask.fs @@ -12,12 +12,36 @@ module ValueTask = exception TestException of string + + module ValueTask = + open System.Threading + + // Following is not available in F#6 + + /// Creates a that's completed successfully with the specified result. + /// The type of the result returned by the task. + /// The result to store into the completed task. + /// The successfully completed task. + let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result + + /// Creates a that's completed exceptionally with the specified exception. + /// The type of the result returned by the task. + /// The exception with which to complete the task. + /// The faulted task. + let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``) + + /// Creates a that's completed due to cancellation with the specified token. + /// The type of the result returned by the task. + /// The token with which to complete the task. + /// The canceled task. + let FromCanceled<'TResult> (cancellationToken: CancellationToken) = ValueTask<'TResult> (Task.FromCanceled<'TResult> cancellationToken) + module ValueTaskTests = let createValueTask isFailed value = - if not isFailed then ValueTask.FromResult value + if not isFailed then ValueTask.FromResult<_> value else - ValueTask.FromException (TestException (sprintf "Ouch, can't create: %A" value )) + ValueTask.FromException<_> (TestException (sprintf "Ouch, can't create: %A" value )) let (|AggregateException|_|) (x: exn) = match x with