Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address follow-up feedback to CachedCompletedInt32Task #63968

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Address follow-up feedback to CachedCompletedInt32Task
  • Loading branch information
stephentoub committed Jan 18, 2022
commit 796a8709cbd17f5fe0fff18a9c031c4975363daf
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,22 @@ internal struct CachedCompletedInt32Task
{
private Task<int>? _task;

/// <summary>
/// Gets a completed <see cref="Task{Int32}"/> whose result is <paramref name="result"/>.
/// </summary>
/// <remarks>
/// This method will try to return an already cached task if available.
/// </remarks>
/// <param name="result">The task's result.</param>
/// <summary>Gets a completed <see cref="Task{Int32}"/> whose result is <paramref name="result"/>.</summary>
/// <remarks>This method will try to return an already cached task if available.</remarks>
/// <param name="result">The result value for which a <see cref="Task{Int32}"/> is needed.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Task<int> GetTask(int result)
{
Task<int>? task;
#pragma warning disable CA1849 // Call async methods when in an async method
if ((task = _task) is not null && task.Result == result)
#pragma warning restore CA1849 // Call async methods when in an async method
if (_task is Task<int> task)
{
Debug.Assert(task.IsCompletedSuccessfully,
"Expected that a stored last task completed successfully");
return task;
}
else
{
return _task = Task.FromResult(result);
Debug.Assert(task.IsCompletedSuccessfully, "Expected that a stored last task completed successfully");
if (task.Result == result)
{
return task;
}
}

return _task = Task.FromResult(result);
}
}
}