Skip to content

Refactor variadic when_any to return positional variant#190

Open
mvandeberg wants to merge 1 commit intocppalliance:developfrom
mvandeberg:pr/when-any-variant
Open

Refactor variadic when_any to return positional variant#190
mvandeberg wants to merge 1 commit intocppalliance:developfrom
mvandeberg:pr/when-any-variant

Conversation

@mvandeberg
Copy link
Contributor

@mvandeberg mvandeberg commented Mar 2, 2026

Replace pair<size_t, deduplicated_variant> return type with a plain std::variant that preserves one alternative per input task. This makes variant::index() the single mechanism for identifying the winner and eliminates the type-deduplication machinery (unique_variant_t and friends). Internally, variant construction uses in_place_index instead of in_place_type to support same-type alternatives.

The homogeneous range overloads (vector) are unchanged.

Summary by CodeRabbit

  • API Updates

    • when_any return value structure has been restructured.
    • Updated mechanism for identifying which concurrent operation completes first.
  • Documentation

    • Examples updated to reflect the new when_any usage patterns.

Replace pair<size_t, deduplicated_variant> return type with a plain
std::variant that preserves one alternative per input task. This makes
variant::index() the single mechanism for identifying the winner and
eliminates the type-deduplication machinery (unique_variant_t and
friends). Internally, variant construction uses in_place_index<I>
instead of in_place_type<T> to support same-type alternatives.

The homogeneous range overloads (vector) are unchanged.
@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

This PR refactors the when_any API to return a positional variant instead of a pair containing a winner index and result. The variant's alternative index now directly indicates which task completed first, eliminating the separate index component and enabling direct access to results via variant.index() and std::get.

Changes

Cohort / File(s) Summary
API Example Update
example/when-any-cancellation/when_any_cancellation.cpp
Updated example to replace tuple unpacking of when_any result with variant-based access pattern using result.index() to determine winner and std::visit to extract values.
Core Implementation Refactor
include/boost/capy/when_any.hpp
Redesigned when_any return type from std::pair<std::size_t, result_t> to positional when_any_variant_t<T0, Ts...>. Updated state structure to use in_place_index<I> for variant construction, added indexed runner factory make_when_any_runner<I>, and modified result storage and return semantics throughout variadic and range-based overloads.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 The rabbit hops through variant fields,
No more pairs—just indices reveal,
Position marks the winning race,
Variants dance in every place! ✨

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately and concisely summarizes the main change: refactoring variadic when_any to return a positional variant instead of a pair with deduplicated types.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cppalliance-bot
Copy link

An automated preview of the documentation is available at https://190.capy.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-03-02 22:27:41 UTC

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
include/boost/capy/when_any.hpp (1)

368-372: Use non-Doxygen comments for detail:: helper documentation.

These comments are attached to implementation symbols in detail::; please avoid /** ... */ javadoc style here to keep generated API docs focused on public surface.

Proposed doc-only adjustment
-/** Indexed overload for heterogeneous when_any (compile-time index).
-
-    Uses compile-time index I for variant construction via in_place_index.
-    Called from when_any_launcher::launch_one<I>().
-*/
+// Indexed overload for heterogeneous when_any (compile-time index).
+// Uses compile-time index I for variant construction via in_place_index.
+// Called from when_any_launcher::launch_one<I>().
 template<std::size_t I, IoAwaitable Awaitable, typename StateType>
 when_any_runner<StateType>
 make_when_any_runner(Awaitable inner, StateType* state)

-/** Runtime-index overload for homogeneous when_any (range path).
+// Runtime-index overload for homogeneous when_any (range path).
 
-    Uses requires-expressions to detect state capabilities:
-    - set_winner_void(): for heterogeneous void tasks (stores monostate)
-    - set_winner_result(): for non-void tasks
-    - Neither: for homogeneous void tasks (no result storage)
-*/
+// Uses requires-expressions to detect state capabilities:
+// - set_winner_void(): for heterogeneous void tasks (stores monostate)
+// - set_winner_result(): for non-void tasks
+// - Neither: for homogeneous void tasks (no result storage)
 template<IoAwaitable Awaitable, typename StateType>
 when_any_runner<StateType>
 make_when_any_runner(Awaitable inner, StateType* state, std::size_t index)
As per coding guidelines: "Don't document detail:: or implementation symbols in javadoc".

Also applies to: 401-407

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@include/boost/capy/when_any.hpp` around lines 368 - 372, The block comment
above the indexed overload uses Doxygen/Javadoc style (/** ... */) for internal
detail symbols; replace those doc-style comments with non-Doxygen comments
(e.g., // or /* ... */) so implementation helpers in detail:: (including the
comment that references when_any_launcher::launch_one<I>() and the other similar
blocks around the lines noted) are not picked up as public API docs—update both
the comment at the indexed overload and the similar comment block that covers
lines 401-407 to use ordinary comments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@include/boost/capy/when_any.hpp`:
- Around line 368-372: The block comment above the indexed overload uses
Doxygen/Javadoc style (/** ... */) for internal detail symbols; replace those
doc-style comments with non-Doxygen comments (e.g., // or /* ... */) so
implementation helpers in detail:: (including the comment that references
when_any_launcher::launch_one<I>() and the other similar blocks around the lines
noted) are not picked up as public API docs—update both the comment at the
indexed overload and the similar comment block that covers lines 401-407 to use
ordinary comments.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33209e0 and 7018285.

⛔ Files ignored due to path filters (3)
  • doc/modules/ROOT/pages/4.coroutines/4f.composition.adoc is excluded by !**/doc/**
  • doc/unlisted/coroutines-when-any.adoc is excluded by !**/doc/**
  • test/unit/when_any.cpp is excluded by !**/test/**
📒 Files selected for processing (2)
  • example/when-any-cancellation/when_any_cancellation.cpp
  • include/boost/capy/when_any.hpp

@cppalliance-bot
Copy link

GCOVR code coverage report https://190.capy.prtest3.cppalliance.org/gcovr/index.html
LCOV code coverage report https://190.capy.prtest3.cppalliance.org/genhtml/index.html
Coverage Diff Report https://190.capy.prtest3.cppalliance.org/diff-report/index.html

Build time: 2026-03-02 22:39:17 UTC

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants