From f2eae409abdd571aad18ee20f15a73c144ac224b Mon Sep 17 00:00:00 2001 From: 1eyewonder Date: Tue, 17 Dec 2024 12:49:08 -0600 Subject: [PATCH 1/2] Removed commented out code from legacy v4 functions --- .../CancellableTaskOption.fs | 107 -------- .../CancellableTaskResultBuilderBase.fs | 208 --------------- .../CancellableTaskResultCE.fs | 95 ------- .../CancellableTaskValidationCE.fs | 252 +----------------- 4 files changed, 1 insertion(+), 661 deletions(-) diff --git a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs index 13f3b9cd..4b43cf24 100644 --- a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs +++ b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs @@ -232,10 +232,6 @@ type ValueTaskValueOptionBuilderBase() = : CancellableValueTaskValueOption<'T> = taskOption -// member inline this.Source(taskOption: ValueTask<'T option>) : CancellableTaskOption<'T> = -// taskOption.AsTask() - - type ValueTaskValueOptionBuilder() = inherit ValueTaskValueOptionBuilderBase() @@ -355,76 +351,6 @@ type TaskOptionBuilder() = return ValueOption.toOption r } - -// type BackgroundTaskOptionBuilder() = - -// inherit TaskOptionBuilderBase() - -// static member RunDynamic(code: TaskOptionCode<'T, 'T>) : CancellableTaskOption<'T> = -// // backgroundTask { .. } escapes to a background thread where necessary -// // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/ -// if -// isNull SynchronizationContext.Current -// && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) -// then -// TaskOptionBuilder.RunDynamic(code) -// else -// Task.Run<'T option>(fun () -> TaskOptionBuilder.RunDynamic(code)) - - -// //// Same as TaskBuilder.Run except the start is inside Task.Run if necessary -// member inline _.Run(code: TaskOptionCode<'T, 'T>) : CancellableTaskOption<'T> = -// if __useResumableCode then -// __stateMachine, CancellableTaskOption<'T>> -// (MoveNextMethodImpl<_>(fun sm -> -// //-- RESUMABLE CODE START -// __resumeAt sm.ResumptionPoint - -// try -// let __stack_code_fin = code.Invoke(&sm) - -// if -// __stack_code_fin -// && not sm.Data.IsTaskCompleted -// then -// sm.Data.MethodBuilder.SetResult(sm.Data.Result.Value) -// with exn -> -// sm.Data.MethodBuilder.SetException exn -// //-- RESUMABLE CODE END -// )) -// (SetStateMachineMethodImpl<_>(fun sm state -> -// sm.Data.MethodBuilder.SetStateMachine(state) -// )) -// (AfterCode<_, CancellableTaskOption<'T>>(fun sm -> -// // backgroundTask { .. } escapes to a background thread where necessary -// // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/ -// if -// isNull SynchronizationContext.Current -// && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) -// then -// sm.Data.MethodBuilder <- AsyncTaskOptionMethodBuilder<'T>.Create() -// sm.Data.MethodBuilder.Start(&sm) -// sm.Data.MethodBuilder.Task -// else -// let sm = sm // copy contents of state machine so we can capture it - -// Task.Run<'T option>(fun () -> -// let mutable sm = sm // host local mutable copy of contents of state machine on this thread pool thread -// sm.Data.MethodBuilder <- AsyncTaskOptionMethodBuilder<'T>.Create() -// sm.Data.MethodBuilder.Start(&sm) -// sm.Data.MethodBuilder.Task -// ) -// )) -// else -// BackgroundTaskOptionBuilder.RunDynamic(code) - -// [] -// module TaskOptionBuilder = - -// let taskOption = ValueTaskValueOptionBuilder() -// let backgroundTaskOption = BackgroundTaskOptionBuilder() - - [] module TaskOptionCEExtensionsLowPriority = // Low priority extensions @@ -626,36 +552,3 @@ module TaskOptionCEExtensionsHighPriority = this.Bind(task, (fun v -> this.Return v)) member inline _.Source(s: #seq<_>) = s - -// [] -// module TaskOptionCEExtensionsMediumPriority = - -// // Medium priority extensions -// type TaskOptionBuilderBase with - -// member inline this.Source(t: Task<'T>) : CancellableTaskOption<'T> = -// t -// |> Task.map Some - -// member inline this.Source(t: Task) : CancellableTaskOption = -// task { -// do! t -// return Some() -// } - -// member inline this.Source(t: ValueTask<'T>) : CancellableTaskOption<'T> = -// t -// |> Task.mapV Some - -// member inline this.Source(t: ValueTask) : CancellableTaskOption = -// task { -// do! t -// return Some() -// } - -// member inline this.Source(opt: Option<'T>) : CancellableTaskOption<'T> = Task.FromResult opt - -// member inline this.Source(computation: Async<'T>) : CancellableTaskOption<'T> = -// computation -// |> Async.map Some -// |> Async.StartImmediateAsTask diff --git a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultBuilderBase.fs b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultBuilderBase.fs index 8e79edc1..858f2947 100644 --- a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultBuilderBase.fs +++ b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultBuilderBase.fs @@ -648,15 +648,6 @@ module CancellableTaskResultBuilderBase = continuation = (fun v -> this.Return v) ) - - // [] - // member inline this.BindReturn - // ( - // [] getAwaiterT: CancellationToken -> 'Awaiter, - // mapper: 'TResult1 -> 'TResult2 - // ) : CancellableTaskResultBuilderBaseCode<_, _, _, _> = - // this.Bind((fun ct -> getAwaiterT ct), (fun v -> this.Return(mapper v))) - /// /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code. /// @@ -761,124 +752,12 @@ module CancellableTaskResultBuilderBase = : CancellableTaskResultBuilderBaseCode<_, _, _, 'Builder> = this.Bind(awaiterT = awaiterT, continuation = (fun v -> this.Return v)) - // [] - // member inline this.BindReturn - // ( - // awaiterT: 'Awaiter, - // [] mapper: 'a -> 'TResult2 - // ) : CancellableTaskResultBuilderBaseCode<'TResult2, 'TResult2, 'Error, 'Builder> = - // this.Bind(awaiterT = awaiterT, continuation = (fun v -> this.Return(mapper v))) - - /// [] module LowPriority = // Low priority extensions type CancellableTaskResultBuilderBase with - // /// - // /// The entry point for the dynamic implementation of the corresponding operation. Do not use directly, only used when executing quotations that involve tasks or other reflective execution of F# code. - // /// - // [] - // static member inline BindDynamic - // ( - // sm: - // byref>>, - // [] getAwaiter: CancellationToken -> 'Awaiter, - // continuation: - // ('TResult1 - // -> CancellableTaskResultBuilderBaseCode<'TOverall, 'TResult2, 'Error, 'Builder>) - // ) : bool = - // sm.Data.ThrowIfCancellationRequested() - - // let mutable awaiter = getAwaiter sm.Data.CancellationToken - - // let cont = - // (CancellableTaskResultBuilderBaseResumptionFunc<'TOverall, 'Error, _>(fun sm -> - // let result = Awaiter.GetResult awaiter - - // match result with - // | Ok result -> (continuation result).Invoke(&sm) - // | Error e -> - // sm.Data.Result <- Error e - // true - // )) - - // // shortcut to continue immediately - // if Awaiter.IsCompleted awaiter then - // cont.Invoke(&sm) - // else - // sm.ResumptionDynamicInfo.ResumptionData <- - // (awaiter :> ICriticalNotifyCompletion) - - // sm.ResumptionDynamicInfo.ResumptionFunc <- cont - // false - - // /// Creates A CancellableTask that runs computation, and when - // /// computation generates a result T, runs binder res. - // /// - // /// A cancellation check is performed when the computation is executed. - // /// - // /// The existence of this method permits the use of let! in the - // /// cancellableTask { ... } computation expression syntax. - // /// - // /// The computation to provide an unbound result. - // /// The function to bind the result of computation. - // /// - // /// A CancellableTask that performs a monadic bind on the result - // /// of computation. - // [] - // member inline _.Bind - // ( - // [] getAwaiterTResult: CancellationToken -> 'Awaiter, - // continuation: - // ('TResult1 - // -> CancellableTaskResultBuilderBaseCode<'TOverall, 'TResult2, 'Error, 'Builder>) - // ) : CancellableTaskResultBuilderBaseCode<'TOverall, 'TResult2, 'Error, 'Builder> = - - // CancellableTaskResultBuilderBaseCode<'TOverall, 'TResult2, 'Error, 'Builder>(fun sm -> - // if __useResumableCode then - // //-- RESUMABLE CODE START - // sm.Data.ThrowIfCancellationRequested() - // // Get an awaiter from the Awaiter - // let mutable awaiter = getAwaiterTResult sm.Data.CancellationToken - - // let mutable __stack_fin = true - - // if not (Awaiter.IsCompleted awaiter) then - // // This will yield with __stack_yield_fin = false - // // This will resume with __stack_yield_fin = true - // let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm) - // __stack_fin <- __stack_yield_fin - - // if __stack_fin then - // let result = Awaiter.GetResult awaiter - - // match result with - // | Ok result -> (continuation result).Invoke(&sm) - // | Error e -> - // sm.Data.Result <- Error e - // true - // else - // let mutable awaiter = awaiter :> ICriticalNotifyCompletion - - // MethodBuilder.AwaitUnsafeOnCompleted( - // &sm.Data.MethodBuilder, - // &awaiter, - // &sm - // ) - - // false - // else - // CancellableTaskResultBuilderBase.BindDynamic( - // &sm, - // getAwaiterTResult, - // continuation - // ) - // //-- RESUMABLE CODE END - // ) - - /// Delegates to the input computation. /// /// The existence of this method permits the use of return! in the @@ -896,16 +775,6 @@ module CancellableTaskResultBuilderBase = continuation = (fun v -> this.Return v) ) - - // [] - // member inline this.BindReturn - // ( - // [] getAwaiterTResult: CancellationToken -> 'Awaiter, - // mapper: 'TResult1 -> 'TResult2 - // ) : CancellableTaskResultBuilderBaseCode<_, _, _, _> = - // this.Bind((fun ct -> getAwaiterTResult ct), (fun v -> this.Return(mapper v))) - - /// Allows the computation expression to turn other types into CancellationToken -> 'Awaiter /// /// This turns a CancellationToken -> 'Awaitable into a CancellationToken -> 'Awaiter. @@ -1060,18 +929,6 @@ module CancellableTaskResultBuilderBase = : CancellableTaskResultBuilderBaseCode<_, _, _, 'Builder> = this.Bind(awaiterTResult = awaiterTResult, continuation = (fun v -> this.Return v)) - // [] - // member inline this.BindReturn - // ( - // awaiterTResult: 'Awaiter, - // [] mapper: 'a -> 'TResult2 - // ) : CancellableTaskResultBuilderBaseCode<'TResult2, 'TResult2, 'Error, 'Builder> = - // this.Bind( - // awaiterTResult = awaiterTResult, - // continuation = (fun v -> this.Return(mapper v)) - // ) - - /// Allows the computation expression to turn other types into CancellationToken -> 'Awaiter /// /// This is the identify function. @@ -1213,71 +1070,6 @@ module CancellableTaskResultBuilderBase = : Async<_> = Async.AwaitCancellableTaskResult t - - // type AsyncEx with - - // /// Return an asynchronous computation that will wait for the given task to complete and return - // /// its result. - // /// - // /// - // /// This is based on Async.Await overload (esp. AwaitTask without throwing AggregateException) - // /// - // static member inline AwaitCancellableTask - // ([] t: CancellationToken -> Task<'T>) - // = - // asyncEx { - // let! ct = Async.CancellationToken - // return! t ct - // } - - // /// Return an asynchronous computation that will wait for the given task to complete and return - // /// its result. - // /// - // /// - // /// This is based on Async.Await overload (esp. AwaitTask without throwing AggregateException) - // /// - // static member inline AwaitCancellableTask - // ([] t: CancellationToken -> Task) - // = - // asyncEx { - // let! ct = Async.CancellationToken - // return! t ct - // } - - // type Microsoft.FSharp.Control.Async with - - // /// Return an asynchronous computation that will wait for the given task to complete and return - // /// its result. - // static member inline AwaitCancellableTask - // ([] t: CancellationToken -> Task<'T>) - // = - // async { - // let! ct = Async.CancellationToken - - // return! - // t ct - // |> Async.AwaitTask - // } - - // /// Return an asynchronous computation that will wait for the given task to complete and return - // /// its result. - // static member inline AwaitCancellableTask - // ([] t: CancellationToken -> Task) - // = - // async { - // let! ct = Async.CancellationToken - - // return! - // t ct - // |> Async.AwaitTask - // } - - // /// Runs an asynchronous computation, starting on the current operating system thread. - // static member inline AsCancellableTask - // (computation: Async<'T>) - // : CancellationToken -> Task<_> = - // fun ct -> Async.StartImmediateAsTask(computation, cancellationToken = ct) - // High priority extensions type CancellableTaskResultBuilderBase with diff --git a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultCE.fs b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultCE.fs index 47b92aa5..b1088bbe 100644 --- a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultCE.fs +++ b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskResultCE.fs @@ -139,58 +139,6 @@ module CancellableTaskResultCE = : CancellationToken -> Awaiter, _> = fun ct -> Awaitable.GetTaskAwaiter(x ct) - // member inline this.MergeSources - // ( - // [] left: CancellationToken -> 'Awaiter1, - // [] right: CancellationToken -> 'Awaiter2 - // ) = - // this.Run( - // this.Bind( - // left, - // fun leftR -> this.BindReturn(right, (fun rightR -> struct (leftR, rightR))) - // ) - // ) - // >> Awaitable.GetTaskAwaiter - - - // member inline this.MergeSources - // ( - // left: 'Awaiter1, - // [] right: CancellationToken -> 'Awaiter2 - // ) = - // this.Run( - // this.Bind( - // left, - // fun leftR -> this.BindReturn(right, (fun rightR -> struct (leftR, rightR))) - // ) - // ) - // >> Awaitable.GetTaskAwaiter - - - // member inline this.MergeSources - // ( - // [] left: CancellationToken -> 'Awaiter1, - // right: 'Awaiter2 - // ) = - // this.Run( - // this.Bind( - // left, - // fun leftR -> this.BindReturn(right, (fun rightR -> struct (leftR, rightR))) - // ) - // ) - // >> Awaitable.GetTaskAwaiter - - - // member inline this.MergeSources(left: 'Awaiter1, right: 'Awaiter2) = - // this.Run( - // this.Bind( - // left, - // fun leftR -> this.BindReturn(right, (fun rightR -> struct (leftR, rightR))) - // ) - // ) - // >> Awaitable.GetTaskAwaiter - - /// Contains methods to build CancellableTasks using the F# computation expression syntax type BackgroundCancellableTaskResultBuilder() = @@ -306,49 +254,6 @@ module CancellableTaskResultCE = let backgroundCancellableTaskResult = BackgroundCancellableTaskResultBuilder() -// /// -// /// A set of extension methods making it possible to bind against in async computations. -// /// -// [] -// module AsyncExtensions = - -// type AsyncExBuilder with - -// member inline this.Source([] t: CancellableTask<'T>) : Async<'T> = -// AsyncEx.AwaitCancellableTask t - -// member inline this.Source([] t: CancellableTask) : Async = -// AsyncEx.AwaitCancellableTask t - -// type Microsoft.FSharp.Control.AsyncBuilder with - -// member inline this.Bind -// ( -// [] t: CancellableTask<'T>, -// [] binder: ('T -> Async<'U>) -// ) : Async<'U> = -// this.Bind(Async.AwaitCancellableTask t, binder) - -// member inline this.ReturnFrom([] t: CancellableTask<'T>) : Async<'T> = -// this.ReturnFrom(Async.AwaitCancellableTask t) - -// member inline this.Bind -// ( -// [] t: CancellableTask, -// [] binder: (unit -> Async<'U>) -// ) : Async<'U> = -// this.Bind(Async.AwaitCancellableTask t, binder) - -// member inline this.ReturnFrom([] t: CancellableTask) : Async = -// this.ReturnFrom(Async.AwaitCancellableTask t) - -// // There is explicitly no Binds for `CancellableTasks` in `Microsoft.FSharp.Control.TaskBuilderBase`. -// // You need to explicitly pass in a `CancellationToken`to start it, you can use `CancellationToken.None`. -// // Reason is I don't want people to assume cancellation is happening without the caller being explicit about where the CancellationToken came from. -// // Similar reasoning for `IcedTasks.ColdTasks.ColdTaskBuilderBase`. - -// // Contains a set of standard functional helper function - [] module CancellableTaskResult = open System.Threading.Tasks diff --git a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskValidationCE.fs b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskValidationCE.fs index 1ff8ba5e..cdc350b0 100644 --- a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskValidationCE.fs +++ b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskValidationCE.fs @@ -545,23 +545,6 @@ module CTVMergeSourcesExtensionsCT1CT2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) (Ok r1) @@ -580,23 +563,6 @@ module CTVMergeSourcesExtensionsCV1CT2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 (Ok r1) @@ -616,23 +582,6 @@ module CTVMergeSourcesExtensionsCT1CV2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) rightR) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) r1 @@ -652,23 +601,6 @@ module CTVMergeSourcesExtensionsCV1CV2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR rightR) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 r1 @@ -688,22 +620,6 @@ module CTVMergeSourcesExtensionsCT1T2 = right: 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) (Ok r1) @@ -722,22 +638,6 @@ module CTVMergeSourcesExtensionsCV1T2 = right: 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 (Ok r1) @@ -757,22 +657,6 @@ module CTVMergeSourcesExtensionsCT1TV2 = right: 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) rightR) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) r1 @@ -792,22 +676,6 @@ module CTVMergeSourcesExtensionsCV1TV2 = right: 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let left = left ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR (rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 r1 @@ -826,22 +694,6 @@ module CTVMergeSourcesExtensionsT1CT2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) (Ok r1) @@ -860,22 +712,6 @@ module CTVMergeSourcesExtensionsTV1CT2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 (Ok r1) @@ -895,26 +731,10 @@ module CTVMergeSourcesExtensionsT1CV2 = [] right: CancellationToken -> 'Awaiter2 ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) rightR) - // ) - // )) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) r1 } - // ) ) [] @@ -928,39 +748,12 @@ module CTVMergeSourcesExtensionsTV1CV2 = ( left: 'Awaiter1, [] right: CancellationToken -> 'Awaiter2 - ) = + ) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // let right = right ct - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR rightR) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 r1 } - // fun ct -> Awaitable.GetTaskAwaiter(IcedTasks.Polyfill.Task.Tasks.TaskBuilder.task { - // let r1 = right ct - // let l1 = left - // let! struct (l1, r1) = IcedTasks.Polyfill.Task.Tasks.TaskBuilder.task.MergeSources(l1, r1) - - // // let foo = cancellableTask.MergeSources(left, right) ct - // return Validation.zip l1 r1 - // }) - // |> ignore - - // Unchecked.defaultof<_> ) @@ -972,20 +765,6 @@ module CTVMergeSourcesExtensionsT1T2 = [] member inline this.MergeSources(left: 'Awaiter1, right: 'Awaiter2) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) (Ok r1) @@ -1000,21 +779,6 @@ module CTVMergeSourcesExtensionsTV1T2 = [] member inline this.MergeSources(left: 'Awaiter1, right: 'Awaiter2) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip leftR (Ok rightR)) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip l1 (Ok r1) @@ -1030,20 +794,6 @@ module CTVMergeSourcesExtensionsT1TV2 = [] member inline this.MergeSources(left: 'Awaiter1, right: 'Awaiter2) = this.Source( - // cancellableTask.Run( - // cancellableTask.Bind( - // (fun ct -> cancellableTask.Source(ValueTask<_> ct)), - // fun ct -> - // (cancellableTask.Bind( - // left, - // fun leftR -> - // cancellableTask.BindReturn( - // right, - // (fun rightR -> Validation.zip (Ok leftR) rightR) - // ) - // )) - // ) - // ) cancellableTask { let! struct (l1, r1) = cancellableTask.MergeSources(left, right) return Validation.zip (Ok l1) r1 From 01648154d5f204230f2539ff2158201277c468e5 Mon Sep 17 00:00:00 2001 From: 1eyewonder Date: Tue, 17 Dec 2024 12:53:09 -0600 Subject: [PATCH 2/2] Reverted changes to CancellableTaskOption since it isn't implemented yet --- .../CancellableTaskOption.fs | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs index 4b43cf24..13f3b9cd 100644 --- a/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs +++ b/src/FsToolkit.ErrorHandling.IcedTasks/CancellableTaskOption.fs @@ -232,6 +232,10 @@ type ValueTaskValueOptionBuilderBase() = : CancellableValueTaskValueOption<'T> = taskOption +// member inline this.Source(taskOption: ValueTask<'T option>) : CancellableTaskOption<'T> = +// taskOption.AsTask() + + type ValueTaskValueOptionBuilder() = inherit ValueTaskValueOptionBuilderBase() @@ -351,6 +355,76 @@ type TaskOptionBuilder() = return ValueOption.toOption r } + +// type BackgroundTaskOptionBuilder() = + +// inherit TaskOptionBuilderBase() + +// static member RunDynamic(code: TaskOptionCode<'T, 'T>) : CancellableTaskOption<'T> = +// // backgroundTask { .. } escapes to a background thread where necessary +// // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/ +// if +// isNull SynchronizationContext.Current +// && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) +// then +// TaskOptionBuilder.RunDynamic(code) +// else +// Task.Run<'T option>(fun () -> TaskOptionBuilder.RunDynamic(code)) + + +// //// Same as TaskBuilder.Run except the start is inside Task.Run if necessary +// member inline _.Run(code: TaskOptionCode<'T, 'T>) : CancellableTaskOption<'T> = +// if __useResumableCode then +// __stateMachine, CancellableTaskOption<'T>> +// (MoveNextMethodImpl<_>(fun sm -> +// //-- RESUMABLE CODE START +// __resumeAt sm.ResumptionPoint + +// try +// let __stack_code_fin = code.Invoke(&sm) + +// if +// __stack_code_fin +// && not sm.Data.IsTaskCompleted +// then +// sm.Data.MethodBuilder.SetResult(sm.Data.Result.Value) +// with exn -> +// sm.Data.MethodBuilder.SetException exn +// //-- RESUMABLE CODE END +// )) +// (SetStateMachineMethodImpl<_>(fun sm state -> +// sm.Data.MethodBuilder.SetStateMachine(state) +// )) +// (AfterCode<_, CancellableTaskOption<'T>>(fun sm -> +// // backgroundTask { .. } escapes to a background thread where necessary +// // See spec of ConfigureAwait(false) at https://devblogs.microsoft.com/dotnet/configureawait-faq/ +// if +// isNull SynchronizationContext.Current +// && obj.ReferenceEquals(TaskScheduler.Current, TaskScheduler.Default) +// then +// sm.Data.MethodBuilder <- AsyncTaskOptionMethodBuilder<'T>.Create() +// sm.Data.MethodBuilder.Start(&sm) +// sm.Data.MethodBuilder.Task +// else +// let sm = sm // copy contents of state machine so we can capture it + +// Task.Run<'T option>(fun () -> +// let mutable sm = sm // host local mutable copy of contents of state machine on this thread pool thread +// sm.Data.MethodBuilder <- AsyncTaskOptionMethodBuilder<'T>.Create() +// sm.Data.MethodBuilder.Start(&sm) +// sm.Data.MethodBuilder.Task +// ) +// )) +// else +// BackgroundTaskOptionBuilder.RunDynamic(code) + +// [] +// module TaskOptionBuilder = + +// let taskOption = ValueTaskValueOptionBuilder() +// let backgroundTaskOption = BackgroundTaskOptionBuilder() + + [] module TaskOptionCEExtensionsLowPriority = // Low priority extensions @@ -552,3 +626,36 @@ module TaskOptionCEExtensionsHighPriority = this.Bind(task, (fun v -> this.Return v)) member inline _.Source(s: #seq<_>) = s + +// [] +// module TaskOptionCEExtensionsMediumPriority = + +// // Medium priority extensions +// type TaskOptionBuilderBase with + +// member inline this.Source(t: Task<'T>) : CancellableTaskOption<'T> = +// t +// |> Task.map Some + +// member inline this.Source(t: Task) : CancellableTaskOption = +// task { +// do! t +// return Some() +// } + +// member inline this.Source(t: ValueTask<'T>) : CancellableTaskOption<'T> = +// t +// |> Task.mapV Some + +// member inline this.Source(t: ValueTask) : CancellableTaskOption = +// task { +// do! t +// return Some() +// } + +// member inline this.Source(opt: Option<'T>) : CancellableTaskOption<'T> = Task.FromResult opt + +// member inline this.Source(computation: Async<'T>) : CancellableTaskOption<'T> = +// computation +// |> Async.map Some +// |> Async.StartImmediateAsTask