Skip to content

Ready-made completed IAsyncOperation<T> / IAsyncAction (make_ready) #663

Description

@jonwis

Request

A helper that returns an IAsyncOperation<T> or IAsyncAction already in the Completed state, carrying a fixed result, with no coroutine frame. co_await make_ready(x), .get(), and a Completed handler all complete synchronously.

Why

Returning an already-known value from an async-typed API today spins up a coroutine frame and its promise/allocation just to hand back a value that's already in hand. A ready-made completed object skips the frame entirely — the common "the answer is cached / trivially available" path.

Where

wil/cppwinrt.h. This was prototyped in microsoft/cppwinrt#1608 and then pulled back out: cppwinrt should stay a pure projection, and the coroutine/async helpers belong in WIL (sylveon on #1608).

Implementation

Prototype: microsoft/cppwinrt@a889be0 (make_ready for pre-completed operations) and microsoft/cppwinrt@1f16ab0 (perfect-forward the value).

The canonical shape is Raymond Chen's series, "Creating an already-completed asynchronous activity in C++/WinRT" (parts 1–9, July 2024): part 1part 9. Follow the "next time" chain to the final form (parts 8–9): a winrt::implements type over a small winrt_async_traits table (covering IAsyncAction/IAsyncOperation<T> and their WithProgress variants, plus a failed_async for the error case), no mutex, Completed/Progress handlers fired inline rather than stored. The cppwinrt prototype additionally kept a single-assignment guard on Completed (throw hresult_illegal_delegate_assignment on a second set) to match the coroutine promise contract; worth keeping.

(via Copilot)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions