Skip to content

Commit

Permalink
Remove FromX methods from ValueTask
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Dec 2, 2022
1 parent 68588db commit 5b61ffc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/FSharpPlus/Control/Monad.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
31 changes: 5 additions & 26 deletions src/FSharpPlus/Extensions/ValueTask.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,14 @@ namespace FSharpPlus
/// Additional operations on ValueTask<'T>
[<RequireQualifiedAccess>]
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)

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed successfully with the specified result.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="result">The result to store into the completed task.</param>
/// <returns>The successfully completed task.</returns>
let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed exceptionally with the specified exception.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="exception">The exception with which to complete the task.</param>
/// <returns>The faulted task.</returns>
let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)
else Faulted (t.AsTask().Exception.InnerExceptions)

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed due to cancellation with the specified token.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="cancellationToken">The token with which to complete the task.</param>
/// <returns>The canceled task.</returns>
let FromCanceled<'TResult> (cancellationToken: CancellationToken) = ValueTask<'TResult> (Task.FromCanceled<'TResult> cancellationToken)

/// <summary>Creates a <see cref="ValueTask{TResult}"/> from a <see cref="Task{TResult}"/>.</summary>
/// <param name="source">Task workflow.</param>
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
Expand All @@ -47,6 +24,8 @@ module ValueTask =
aw.OnCompleted (fun () -> f x)

/// <summary>Creates a ValueTask workflow from 'source' another, mapping its result with 'f'.</summary>
/// <param name="f">The mapping function.</param>
/// <param name="source">ValueTask workflow.</param>
let map (f: 'T -> 'U) (source: ValueTask<'T>) : ValueTask<'U> =
let tcs = TaskCompletionSource<'U> ()
continueTask tcs source (fun x ->
Expand Down Expand Up @@ -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
27 changes: 25 additions & 2 deletions tests/FSharpPlus.Tests/ValueTask.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,35 @@ module ValueTask =

exception TestException of string


module ValueTask =

// Following is not available in F#6

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed successfully with the specified result.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="result">The result to store into the completed task.</param>
/// <returns>The successfully completed task.</returns>
let FromResult<'TResult> (result: 'TResult) = ValueTask<'TResult> result

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed exceptionally with the specified exception.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="exception">The exception with which to complete the task.</param>
/// <returns>The faulted task.</returns>
let FromException<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)

/// <summary>Creates a <see cref="ValueTask{TResult}"/> that's completed due to cancellation with the specified token.</summary>
/// <typeparam name="TResult">The type of the result returned by the task.</typeparam>
/// <param name="cancellationToken">The token with which to complete the task.</param>
/// <returns>The canceled task.</returns>
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
Expand Down

0 comments on commit 5b61ffc

Please sign in to comment.