Skip to content

Commit 8a3ff94

Browse files
committed
Address feedback from wraith - including constraining state types to class types.
1 parent e516e3b commit 8a3ff94

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ private IAsyncResult BeginExecuteXmlReaderInternal(
264264
AsyncHelper.ContinueTaskWithState(
265265
taskToContinue: writeTask,
266266
taskCompletionSource: localCompletion,
267-
state: Tuple.Create(this, localCompletion),
268-
onSuccess: static state =>
267+
state1: this,
268+
state2: localCompletion,
269+
onSuccess: static (sqlCommand, localCompletion) =>
269270
{
270-
var parameters = (Tuple<SqlCommand, TaskCompletionSource<object>>)state;
271-
parameters.Item1.BeginExecuteXmlReaderInternalReadStage(parameters.Item2);
271+
sqlCommand.BeginExecuteXmlReaderInternalReadStage(localCompletion);
272272
});
273273
}
274274
else

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12286,9 +12286,9 @@ private Task GetTerminationTask(Task unterminatedWriteTask, object value, MetaTy
1228612286
{
1228712287
return AsyncHelper.CreateContinuationTaskWithState(
1228812288
unterminatedWriteTask,
12289-
state1: 0,
12289+
state1: this,
1229012290
state2: stateObj,
12291-
onSuccess: WriteInt);
12291+
onSuccess: static (parser, state) => parser.WriteInt(0, state));
1229212292
}
1229312293
}
1229412294
else

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Utilities/AsyncHelper.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ internal static void ContinueTask(
137137
/// is to allow the task completion source to be continued even more after this current
138138
/// continuation.
139139
/// </remarks>
140-
/// <typeparam name="TState">Type of the state object to provide to the callbacks</typeparam>
140+
/// <typeparam name="TState">
141+
/// Type of the state object to provide to the callbacks, constrained to class types to
142+
/// prevent accidental modification of pass-by-value types.
143+
/// </typeparam>
141144
/// <param name="taskToContinue">Task to continue with provided callbacks</param>
142145
/// <param name="taskCompletionSource">
143146
/// Completion source used to track completion of the continuation, see remarks for details
@@ -153,6 +156,7 @@ internal static void ContinueTaskWithState<TState>(
153156
Action<TState> onSuccess,
154157
Action<TState, Exception>? onFailure = null,
155158
Action<TState>? onCancellation = null)
159+
where TState : class
156160
{
157161
ContinuationState<TState> continuationState = new(
158162
OnCancellation: onCancellation,
@@ -237,8 +241,14 @@ internal static void ContinueTaskWithState<TState>(
237241
/// <param name="taskCompletionSource">
238242
/// Completion source used to track completion of the continuation, see remarks for details
239243
/// </param>
240-
/// <typeparam name="TState1">Type of the first state object to provide to callbacks</typeparam>
241-
/// <typeparam name="TState2">Type of the second state object to provide to callbacks</typeparam>
244+
/// <typeparam name="TState1">
245+
/// Type of the first state object to provide to the callbacks, constrained to class types
246+
/// to prevent accidental modification of pass-by-value types.
247+
/// </typeparam>
248+
/// <typeparam name="TState2">
249+
/// Type of the second state object to provide to the callbacks, constrained to class types
250+
/// to prevent accidental modification of pass-by-value types.
251+
/// </typeparam>
242252
/// <param name="state1">First state object to provide to callbacks</param>
243253
/// <param name="state2">Second state object to provide to callbacks</param>
244254
/// <param name="onSuccess">Callback to execute on successful completion of the task</param>
@@ -252,6 +262,8 @@ internal static void ContinueTaskWithState<TState1, TState2>(
252262
Action<TState1, TState2> onSuccess,
253263
Action<TState1, TState2, Exception>? onFailure = null,
254264
Action<TState1, TState2>? onCancellation = null)
265+
where TState1 : class
266+
where TState2 : class
255267
{
256268
ContinuationState<TState1, TState2> continuationState = new(
257269
OnCancellation: onCancellation,
@@ -428,7 +440,10 @@ internal static void ContinueTaskWithState<TState1, TState2>(
428440
/// task will be completed with the exception.
429441
/// * The task will be completed as successful.
430442
/// </remarks>
431-
/// <typeparam name="TState">Type of the state object to pass to callbacks</typeparam>
443+
/// <typeparam name="TState">
444+
/// Type of the state object to pass to callbacks, constrained to class types to prevent
445+
/// accidental modification of pass-by-value types.
446+
/// </typeparam>
432447
/// <param name="taskToContinue">
433448
/// Task to continue with provided callbacks, if <c>null</c>, <c>null</c> will be returned.
434449
/// </param>
@@ -442,6 +457,7 @@ internal static void ContinueTaskWithState<TState1, TState2>(
442457
Action<TState> onSuccess,
443458
Action<TState, Exception>? onFailure = null,
444459
Action<TState>? onCancellation = null)
460+
where TState : class
445461
{
446462
if (taskToContinue is null)
447463
{
@@ -533,8 +549,13 @@ internal static void ContinueTaskWithState<TState1, TState2>(
533549
/// task will be completed with the exception.
534550
/// * The task will be completed as successful.
535551
/// </remarks>
536-
/// <typeparam name="TState1">Type of the first state object to pass to callbacks</typeparam>
537-
/// <typeparam name="TState2">Type of the second state object to pass to callbacks</typeparam>
552+
/// <typeparam name="TState1">
553+
/// Type of the first state object to pass to callbacks, constrained to class types to
554+
/// prevent accidental modification of pass-by-value types.
555+
/// </typeparam>
556+
/// <typeparam name="TState2">
557+
/// Type of the second state object to pass to callbacks, constrained to class types to
558+
/// prevent accidental modification of pass-by-value types.</typeparam>
538559
/// <param name="taskToContinue">
539560
/// Task to continue with provided callbacks, if <c>null</c>, <c>null</c> will be returned.
540561
/// </param>
@@ -550,6 +571,8 @@ internal static void ContinueTaskWithState<TState1, TState2>(
550571
Action<TState1, TState2> onSuccess,
551572
Action<TState1, TState2, Exception>? onFailure = null,
552573
Action<TState1, TState2>? onCancellation = null)
574+
where TState1 : class
575+
where TState2 : class
553576
{
554577
if (taskToContinue is null)
555578
{
@@ -667,6 +690,10 @@ internal static void SetTimeoutException(
667690
/// exception returned is set as the exception that completes the task completion source.
668691
/// This overload provides a state object to the timeout callback.
669692
/// </summary>
693+
/// <typeparam name="TState">
694+
/// Type of the state object to pass to callbacks, constrained to class types to prevent
695+
/// accidental modification of pass-by-value types.
696+
/// </typeparam>
670697
/// <param name="taskCompletionSource">Task to execute with a timeout</param>
671698
/// <param name="timeoutInSeconds">Number of seconds to wait until timing out the task</param>
672699
/// <param name="state">State object to pass to the callback</param>
@@ -681,6 +708,7 @@ internal static void SetTimeoutExceptionWithState<TState>(
681708
TState state,
682709
Func<TState, Exception> onTimeout,
683710
CancellationToken cancellationToken)
711+
where TState : class
684712
{
685713
if (timeoutInSeconds <= 0)
686714
{
@@ -786,14 +814,17 @@ private record ContinuationState<TState>(
786814
Action<TState, Exception>? OnFailure,
787815
Action<TState> OnSuccess,
788816
TState State,
789-
TaskCompletionSource<object?> TaskCompletionSource);
817+
TaskCompletionSource<object?> TaskCompletionSource)
818+
where TState : class;
790819

791820
private record ContinuationState<TState1, TState2>(
792821
Action<TState1, TState2>? OnCancellation,
793822
Action<TState1, TState2, Exception>? OnFailure,
794823
Action<TState1, TState2> OnSuccess,
795824
TState1 State1,
796825
TState2 State2,
797-
TaskCompletionSource<object?> TaskCompletionSource);
826+
TaskCompletionSource<object?> TaskCompletionSource)
827+
where TState1 : class
828+
where TState2 : class;
798829
}
799830
}

0 commit comments

Comments
 (0)