Skip to content

add Task<T>.CastUp and ValueTask<T>.CastUp - #136898

Open
lagudafuadtosin wants to merge 3 commits into
dotnet:mainfrom
lagudafuadtosin:castup-89778
Open

lagudafuadtosin wants to merge 3 commits into
dotnet:mainfrom
lagudafuadtosin:castup-89778

Conversation

@lagudafuadtosin

Copy link
Copy Markdown
Contributor

fixes #89778

adds the approved api. Task.CastUp returns the task itself when it already is a Task, otherwise a task that completes with the same result, exceptions or cancellation. ValueTask.CastUp reuses its result or source and only wraps a task. tests added.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 3, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

@jkotas

jkotas commented Nov 3, 2026

Copy link
Copy Markdown
Member

Are there any places in the libraries in this repo where this API can be used?

#endregion

/// <summary>Task that completes with the outcome of a <see cref="Task{TDerived}"/>.</summary>
private sealed class CastUpPromise<TDerived> : Task<TResult>, ITaskCompletionAction where TDerived : class?, TResult

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With runtime-async, how is the performance comparing with simply async CastUp() => await task?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. i measured it and the wrapper was faster when the task had already completed, so i added a fast path for that case. CastUp is now faster in both cases (16.5 vs 21.5 ns completed, 89 vs 157 ns pending).

@lagudafuadtosin

Copy link
Copy Markdown
Contributor Author

Are there any places in the libraries in this repo where this API can be used?

yes, ZipArchiveEntry.Async.cs has CastToStreamAsync, an async helper that only exists to turn Task into Task. i've switched its 3 callers to Task.CastUp and removed it.

/// <param name="task">The task to cast.</param>
/// <returns>A <see cref="Task{TResult}"/> that completes when <paramref name="task"/> completes, with the same result, exceptions or cancellation. It may be the same instance as <paramref name="task"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="task"/> is <see langword="null"/>.</exception>
public static Task<TResult> CastUp<TDerived>(Task<TDerived> task) where TDerived : class?, TResult

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VSadov @jakobbotsch Is this implementation the best we can do with runtime async?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Threading.Tasks community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Task<T>.CastUp

3 participants