Skip to content

Commit 2ca6c21

Browse files
committed
Move ValueTask constructors to more readable module extensions
1 parent 21b07d1 commit 2ca6c21

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ open System.Threading.Tasks.Sources
1313

1414
open FSharp.Core.CompilerServices
1515
open FSharp.Core.CompilerServices.StateMachineHelpers
16+
open FSharp.Control
1617

1718

1819
[<AutoOpen>]
@@ -279,14 +280,14 @@ and [<NoComparison; NoEquality>] TaskSeq<'Machine, 'T
279280
if this._machine.ResumptionPoint = -1 then // can't use as IAsyncEnumerator before IAsyncEnumerable
280281
logInfo "at MoveNextAsync: Resumption point = -1"
281282

282-
ValueTask<bool>()
283+
ValueTask.False
283284

284285
elif this._machine.Data.completed then
285286
logInfo "at MoveNextAsync: completed = true"
286287

287288
// return False when beyond the last item
288289
this._machine.Data.promiseOfValueOrEnd.Reset()
289-
ValueTask<bool>()
290+
ValueTask.False
290291

291292
else
292293
logInfo "at MoveNextAsync: normal resumption scenario"
@@ -343,18 +344,18 @@ and [<NoComparison; NoEquality>] TaskSeq<'Machine, 'T
343344
// the Current value
344345
data.current <- ValueNone
345346

346-
ValueTask<bool>(result)
347+
ValueTask.FromResult result
347348

348349
| ValueTaskSourceStatus.Faulted
349350
| ValueTaskSourceStatus.Canceled
350351
| ValueTaskSourceStatus.Pending as state ->
351352
logInfo ("at MoveNextAsyncResult: case ", state)
352353

353-
ValueTask<bool>(this, version) // uses IValueTaskSource<'T>
354+
ValueTask.ofIValueTaskSource this version
354355
| _ ->
355356
logInfo "at MoveNextAsyncResult: Unexpected state"
356357
// assume it's a possibly new, not yet supported case, treat as default
357-
ValueTask<bool>(this, version) // uses IValueTaskSource<'T>
358+
ValueTask.ofIValueTaskSource this version
358359

359360
and TaskSeqCode<'T> = ResumableCode<TaskSeqStateMachineData<'T>, unit>
360361
and TaskSeqStateMachine<'T> = ResumableStateMachine<TaskSeqStateMachineData<'T>>

src/FSharp.Control.TaskSeq/Utils.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@ open System.Threading.Tasks
55
[<AutoOpen>]
66
module ValueTaskExtensions =
77
/// Extensions for ValueTask that are not available in NetStandard 2.1, but are
8-
/// available in .NET 5+.
8+
/// available in .NET 5+. We put them in Extension space to mimic the behavior of NetStandard 2.1
99
type ValueTask with
1010

1111
/// (Extension member) Gets a task that has already completed successfully.
1212
static member inline CompletedTask = Unchecked.defaultof<ValueTask>
1313

14+
15+
module ValueTask =
16+
/// A successfully completed ValueTask of boolean that has the value false.
17+
let False = ValueTask<bool>()
18+
19+
/// A successfully completed ValueTask of boolean that has the value true.
20+
let True = ValueTask<bool> true
21+
22+
/// Creates a ValueTask with the supplied result of the successful operation.
23+
let inline FromResult (x: 'T) = ValueTask<'T> x
24+
25+
/// Creates a ValueTask with an IValueTaskSource representing the operation
26+
let inline ofIValueTaskSource taskSource version = ValueTask<bool>(taskSource, version)
27+
1428
module Task =
1529
/// Convert an Async<'T> into a Task<'T>
1630
let inline ofAsync (async: Async<'T>) = task { return! async }

0 commit comments

Comments
 (0)