From 6cb56340236357da4db0932d0b642ee62385c969 Mon Sep 17 00:00:00 2001 From: Eric Niebler Date: Mon, 23 Nov 2026 11:34:09 -0800 Subject: [PATCH] add a sender facade that derives completions and attributes from algorithm code An algorithm written with `__facade` completes only through `__send` and starts nested work only through `__start_nested`. Its handlers return effect tokens, and the facade walks them to derive the sender's completion signatures and its completion attributes (scheduler, domain, behavior). So these can no longer disagree with what the algorithm does. The tests use algorithms written for them. The `__mfor<__mlist_ptr>` specialization lets `__mapply` take an `__mlist_ptr`. Co-Authored-By: Claude Opus 5.5 --- include/stdexec/__detail/__meta.hpp | 7 + include/stdexec/__detail/__sender_facade.hpp | 1875 ++++++++++++++++++ include/stdexec/__detail/__utility.hpp | 2 + test/CMakeLists.txt | 1 + test/stdexec/detail/test_facade.cpp | 400 ++++ 5 files changed, 2285 insertions(+) create mode 100644 include/stdexec/__detail/__sender_facade.hpp create mode 100644 test/stdexec/detail/test_facade.cpp diff --git a/include/stdexec/__detail/__meta.hpp b/include/stdexec/__detail/__meta.hpp index 0a7f9fb31..51be0f3fa 100644 --- a/include/stdexec/__detail/__meta.hpp +++ b/include/stdexec/__detail/__meta.hpp @@ -640,6 +640,13 @@ namespace STDEXEC using __f = __minvoke<_Fn, _As...>; }; + template + struct __mfor<__mlist_ptr<_As...>> + { + template + using __f = __minvoke<_Fn, _As...>; + }; + template struct __mfor<_Ret(_As...)> { diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp new file mode 100644 index 000000000..8a0eb05a8 --- /dev/null +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -0,0 +1,1875 @@ +/* + * Copyright (c) 2026 NVIDIA Corporation + * + * Licensed under the Apache License Version 2.0 with LLVM Exceptions + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://llvm.org/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "__config.hpp" + +#if STDEXEC_USE_MODULES() && !defined(STDEXEC_IN_MODULE_PURVIEW) + +import stdexec; + +#else + +# include "__execution_fwd.hpp" + +// include these after __execution_fwd.hpp +# include "__completion_behavior.hpp" +# include "__completion_signatures_of.hpp" +# include "__concepts.hpp" +# include "__connect.hpp" +# include "__diagnostics.hpp" +# include "__domain.hpp" +# include "__env.hpp" +# include "__get_completion_signatures.hpp" +# include "__memory.hpp" +# include "__meta.hpp" +# include "__operation_states.hpp" +# include "__query.hpp" +# include "__receivers.hpp" +# include "__schedulers.hpp" +# include "__sender_concepts.hpp" +# include "__senders.hpp" +# include "__tuple.hpp" +# include "__type_traits.hpp" +# include "__utility.hpp" +# include "__variant.hpp" + +# if !STDEXEC_USE_MODULES() +# include +# include +# include +# include +# endif + +# include "__prologue.hpp" + +STDEXEC_PRAGMA_IGNORE_GNU("-Wmissing-braces") + +// The sender facade: a way to write a sender algorithm so that its completion +// signatures and its completion attributes (domain, behavior, scheduler) are +// derived from the code that performs its completions, rather than computed +// separately by hand. See MAINTAINERS.md for a guide to writing an algorithm. +// +// The design: +// +// 1. Effects. Algorithms complete with `__send`, which returns an *effect +// token* -- a type that records what was done. A handler +// returns the tokens of everything it did synchronously: `_Tag(_Args...)` +// for a completion, `__starts<_Key>` for starting a child or nested +// operation. Branches that complete differently are joined with +// combinators (`__choose`, `__when`, `__visit`, `__set_value_from`) whose +// result is the union of their branches' tokens. +// +// 2. Provenance. The facade owns every *trigger* -- `start()` and the +// receivers of all child and nested operations -- so it knows where each +// effect happened: `__from_start`, or `__via<_Key, _Tag, _Src>`, meaning +// "inside nested op _Key's _Tag completion, where _Key was started from +// _Src". The algorithm never mentions domains, schedulers, or behaviors. +// +// 3. Derivation. A consteval walk from `start()` through the tokens yields +// the set of (signature, provenance) events. The completion signatures +// are the event signatures. The attributes of a channel are a join over +// the provenance of that channel's events, computed lazily when the +// attributes are queried, so completion *schedulers* (runtime values) +// work too: +// - behavior: join over the provenances; `__via` composes sequentially +// - domain: the one domain, or indeterminate_domain +// - scheduler: reported only if every event on the channel gets its +// scheduler from the same place +// +// 4. Type errors. The walk *evaluates* each nested sender's +// get_completion_signatures, so an ill-typed child fails the walk the +// way it fails any other algorithm: by throwing when constexpr exceptions +// are available, or by returning an __mexception otherwise. Children are +// checked before any state type is computed, because state types may +// depend on the children's completions. A handler (or `__get_state`) +// that finds its own inputs ill-typed returns a token (or state type) +// holding an __mexception, which the walk raises the same way. The +// attributes are gated on the walk being a constant expression, so an +// ill-typed sender has no completion attributes rather than causing hard +// errors. +// +// 5. Environment dependence. A sender whose completions depend on its +// environment is *dependent*: asked how it completes without one, it +// reports dependent_sender_error. The walk asks the children without an +// environment when it has none, so a dependent child makes its parent +// dependent. An algorithm whose own completions depend on the +// environment opts in by defining +// template static constexpr bool __depends_on_env = true; +// +// A facade algorithm is a class that derives from `__facade::__defaults` and +// overrides the hooks it needs. Its senders are `__facade::__sender`s, products +// of a tag, the algorithm's data, and its children, and `__facade::__algorithm` +// maps the tag to the algorithm: +// +// template <> +// struct __facade::__algorithm : __upon::__algo +// {}; +// +// auto sndr = __facade::__sender{then_t(), fn, child}; + +// NOLINTNEXTLINE(bugprone-std-namespace-modification, modernize-concat-nested-namespaces) +namespace STDEXEC +{ + namespace __facade + { + //////////////////////////////////////////////////////////////////////////////////////// + // The type that a hook names: the type itself, or the type in an __mtype, which is how + // a hook names a type that it cannot give as a value + template + struct __unwrap_type + { + using __t = _Ty; + }; + + template + struct __unwrap_type<__mtype<_Ty>> + { + using __t = _Ty; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // 1. Effects + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __starts + {}; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __effects + { + // A token for fewer effects converts to a token for more effects, so a control-flow + // path that does less can return into a superset. Paths that do nothing return + // `__effects<>{}`. + template + requires(__one_of<_Items, _Others...> && ...) + constexpr operator __effects<_Others...>() const noexcept + { + return {}; + } + }; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + using __union_t = __mcall<__mconcat<__munique<__qq<__effects>>>, _Tokens...>; + + // A handler that finds its inputs ill-typed returns a token holding an __mexception + // that describes the problem. The walk raises it (see part 4 above). + STDEXEC_MODULE_EXPORT_AUTHORING + template + requires __merror<_Error> + [[nodiscard]] + constexpr auto __type_error(_Error) noexcept -> __effects<_Error> + { + return {}; + } + + //////////////////////////////////////////////////////////////////////////////////////// + // 2. Completing. __send is the only way an algorithm completes: it sends a completion to + // the algorithm's receiver, and returns the token that records the completion. + // __start_nested is the only way it starts a child or nested operation. (Call these + // with qualified names, so that argument-dependent lookup does not look in the + // receiver's namespaces.) + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto __send(_Receiver &__rcvr, _Tag, _As &&...__as) noexcept + -> __effects<_Tag(_As...)> + { + _Tag()(static_cast<_Receiver &&>(__rcvr), static_cast<_As &&>(__as)...); + return {}; + } + + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto __start_nested(_OpState &__op) noexcept -> __effects<__starts<_Key>> + { + STDEXEC::start(__op); + return {}; + } + + //////////////////////////////////////////////////////////////////////////////////////// + // 3. Combinators for control flow whose paths complete differently + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(host, device) + constexpr auto __choose(bool __cond, _Then __then_fn, _Else __else_fn) noexcept + -> __union_t<__call_result_t<_Then &>, __call_result_t<_Else &>> + { + if (__cond) + return __then_fn(); + else + return __else_fn(); + } + + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(host, device) + constexpr auto __when(bool __cond, _Then __then_fn) noexcept -> __call_result_t<_Then &> + { + if (__cond) + return __then_fn(); + else + return {}; + } + + // Visits a variant that holds a value. + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(host, device) + constexpr auto __visit(__variant<_Ts...> &__var, _Fn __fn) noexcept + -> __union_t<__effects<>, __call_result_t<_Fn &, _Ts &>...> + { + using __result_t = __union_t<__effects<>, __call_result_t<_Fn &, _Ts &>...>; + if constexpr (sizeof...(_Ts) == 0) + { + return __result_t(); + } + else + { + return STDEXEC::__visit([&](_Alt &__alt) noexcept -> __result_t + { return __fn(__alt); }, + __var); + } + } + + // The token of sending the result of __fn(__args...) as a value, and the + // exception as an error if the invocation can throw + template + consteval auto __set_value_from_token() noexcept + { + using __value_t = __effects<__single_value_sig_t<__invoke_result_t<_Fn, _Args...>>>; + if constexpr (__nothrow_invocable<_Fn, _Args...>) + return __value_t(); + else + return __union_t<__value_t, __effects>(); + } + + // The type error that algorithms report when a completion's arguments, which they must + // store, cannot be decay-copied + STDEXEC_MODULE_EXPORT_AUTHORING + template + [[nodiscard]] + consteval auto __decay_copy_error() noexcept + { + // The first argument that cannot be decay-copied + using __arg_t = + __mcall<__mfind_if<__mcompose_q<__mnot, __decay_copyable_t>, __q<__mfront>>, _Args...>; + return __facade::__type_error(__mexception<_WHAT_(_TYPE_IS_NOT_DECAY_COPYABLE_), + _WHERE_(_IN_ALGORITHM_, _AlgoTag), + _WITH_TYPE_<__arg_t>>()); + } + + // Invokes __fn(__args...) and sends the result as a value. If the invocation can + // throw, sends the exception as an error. If __fn cannot be invoked with __args, + // reports a type error on behalf of the algorithm _AlgoTag. + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(host, device) + constexpr auto __set_value_from(_Receiver &__rcvr, _Fn &&__fn, _Args &&...__args) noexcept + { + if constexpr (!__invocable<_Fn, _Args...>) + { + return __facade::__type_error(__callable_error_t<_AlgoTag, __decay_t<_Fn>, _Args...>()); + } + else + { + STDEXEC::__set_value_from(static_cast<_Receiver &&>(__rcvr), + static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + return __facade::__set_value_from_token<_Fn, _Args...>(); + } + } + + //////////////////////////////////////////////////////////////////////////////////////// + // 4. Provenance + STDEXEC_MODULE_EXPORT_AUTHORING + struct __from_start + {}; // synchronously within start() + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __via + {}; // within nested op _Key's _Tag completion; _Key was started from _Src + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __child + {}; // the key of the _Idx-th child sender + + // (Not __event, which is a keyword of MSVC.) + template + struct __walk_event + {}; + + //////////////////////////////////////////////////////////////////////////////////////// + // The senders of facade algorithms (defined at the end of this file) + + // The facade algorithm that gives the senders with tag _Tag their behavior. + // An algorithm specializes it for its tag: + // + // template <> + // struct __facade::__algorithm : __upon::__algo + // {}; + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __algorithm; + + // A sender of a facade algorithm: a tag, the algorithm's data, and its children + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __sender; + + //////////////////////////////////////////////////////////////////////////////////////// + // Access to the parts of a sender. The hooks that run at compile time and during + // attribute queries are given the sender, as a __sender const &. + + // The sender's data, with the sender's value category + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __get_data(_Sender &&__sndr) noexcept -> decltype(auto) + { + return STDEXEC::__get<1>(static_cast<_Sender &&>(__sndr)); + } + + // The sender's _Idx-th child, with the sender's value category + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __get_child(_Sender &&__sndr) noexcept -> decltype(auto) + { + return STDEXEC::__get<_Idx + 2>(static_cast<_Sender &&>(__sndr)); + } + + // The I-th child of a sender, with the sender's value category + STDEXEC_MODULE_EXPORT_AUTHORING + template + using __nth_child_of_t = + __copy_cvref_t<_Sender, + __mapply<__mbind_front_q<__m_at, __msize_t<_Idx + 2>>, __decay_t<_Sender>>>; + + // The data of a sender, with the sender's value category + STDEXEC_MODULE_EXPORT_AUTHORING + template + using __data_of_t = + __copy_cvref_t<_Sender, __mapply<__mbind_front_q<__m_at, __msize_t<1>>, __decay_t<_Sender>>>; + + // The number of children of a sender + STDEXEC_MODULE_EXPORT_AUTHORING + template + inline constexpr std::size_t __nbr_children_v = __mapply<__msize, __decay_t<_Sender>>::value + - 2; + + // The environment that the operation keyed by _Key sees, given the + // environment of the receiver connected to the algorithm's sender. See + // __defaults::__env_for. + template + using __env_t = decltype(_Algo::__env_for(_Key(), + __declval<__decay_t<_Sender> const &>(), + __declval<_Env>())); + + //////////////////////////////////////////////////////////////////////////////////////// + // The facade's runtime machinery: receivers and operation state. The tokens + // that the handlers return are for the walk; at run time, the receivers and + // the operation state discard them. + + // The receiver of a child operation that the facade connects. (Child + // operations are stored outside the algorithm's state.) _Sender and _Env + // are the algorithm's sender and the environment of the receiver connected + // to it. + template + struct __receiver : _Algo::__receiver_base + { + using receiver_concept = receiver_tag; + + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr explicit __receiver(_State *__state) noexcept + : __state_(__state) + {} + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr void set_value(_As &&...__as) noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_value_t(), static_cast<_As &&>(__as)...); + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr void set_error(_Error &&__err) noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_error_t(), static_cast<_Error &&>(__err)); + } + + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr void set_stopped() noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_stopped_t()); + } + + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto get_env() const noexcept -> __facade::__env_t<_Algo, _Key, _Sender, _Env> + { + using __env_t = __facade::__env_t<_Algo, _Key, _Sender, _Env>; + static_assert(__same_as, + "__get_env and __env_for must produce environments of the same type"); + return _Algo::__get_env(_Key(), *__state_); + } + + _State *__state_; + }; + + // The receiver of an operation that an algorithm stores in its state: a + // nested operation, or a child that the algorithm connects itself. Its + // member functions are not constexpr, so that their bodies are instantiated + // only once the state is complete. (The type of an operation can depend on + // its receiver's member functions, for example when the operation + // type-erases its receiver, and the state is incomplete while that type is + // computed. Clang instantiates constexpr functions eagerly.) + template + struct __nested_receiver : _Algo::__receiver_base + { + using receiver_concept = receiver_tag; + + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr explicit __nested_receiver(_State *__state) noexcept + : __state_(__state) + {} + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + void set_value(_As &&...__as) noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_value_t(), static_cast<_As &&>(__as)...); + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + void set_error(_Error &&__err) noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_error_t(), static_cast<_Error &&>(__err)); + } + + STDEXEC_ATTRIBUTE(always_inline, host, device) + void set_stopped() noexcept + { + (void) _Algo::__complete(_Key(), *__state_, set_stopped_t()); + } + + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + auto get_env() const noexcept -> __facade::__env_t<_Algo, _Key, _Sender, _Env> + { + using __env_t = __facade::__env_t<_Algo, _Key, _Sender, _Env>; + static_assert(__same_as, + "__get_env and __env_for must produce environments of the same type"); + return _Algo::__get_env(_Key(), *__state_); + } + + _State *__state_; + }; + + // The receiver of an operation that an algorithm stores in its state, when the state's + // receiver is an archetype (as in the walk). Its completions do nothing, so analyzing an + // algorithm does not instantiate the code that completes it. (That would instantiate + // the code that completes the nested operation's receiver, which can be device code: + // a CUDA stream operation completes a receiver that is not a stream receiver from a + // kernel. And the algorithm's handlers need not be callable there.) + template + struct __nested_receiver_archetype : __receiver_archetype<_Env> + { + __nested_receiver_archetype() = default; + + template + STDEXEC_ATTRIBUTE(host, device) + constexpr explicit __nested_receiver_archetype(_State *) noexcept + {} + }; + + template + struct __receiver_of + { + using __t = __nested_receiver<_Algo, _Key, _State, _Sender, env_of_t<_Receiver>>; + }; + + template + requires __std::derived_from<_Receiver, __receiver_archetype_base> + struct __receiver_of<_Algo, _Key, _State, _Sender, _Receiver> + { + using __t = + __nested_receiver_archetype<__facade::__env_t<_Algo, _Key, _Sender, env_of_t<_Receiver>>>; + }; + + // The receiver to which an algorithm connects an operation it stores in its state + STDEXEC_MODULE_EXPORT_AUTHORING + template + using __receiver_t = __receiver_of<_Algo, _Key, _State, _Sender, _Receiver>::__t; + + template + auto __starts_all_impl(__indices<_Is...>) -> __effects<__starts<__child<_Is>>...>; + + template + using __starts_all_t = decltype(__facade::__starts_all_impl(__make_indices<_Np>())); + + // The handle through which __start starts the children + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __children_ref + { + STDEXEC_ATTRIBUTE(host, device) + constexpr auto __start_all() noexcept -> __starts_all_t<_Np> + { + if constexpr (!__same_as<_Ops, void>) // _Ops is void during type-level analysis + { + STDEXEC::__apply([](auto &...__ops) noexcept { (STDEXEC::start(__ops), ...); }, *__ops_); + } + return {}; + } + + _Ops *__ops_; + }; + + template + using __state_type_t = decltype(_Algo::__get_state(__declval<_Sender>(), + __declval<_Receiver>())); + + template + struct __children_ops; + + template + struct __children_ops<_Algo, _Sender, _Receiver, __indices<_Is...>> + { + using __state_t = __state_type_t<_Algo, _Sender, _Receiver>; + + template + using __receiver_t = + __facade::__receiver<_Algo, __child<_Idx>, __state_t, _Sender, env_of_t<_Receiver>>; + + template + using __op_t = decltype(_Algo::__connect(__child<_Idx>(), + __declval<__state_t &>(), + __declval<__nth_child_of_t<_Sender, _Idx>>(), + __declval<__receiver_t<_Idx>>())); + + using __t = __tuple<__op_t<_Is>...>; + + // (Not __nothrow, which the Windows SDK defines as a macro.) + static constexpr bool __nothrow_v = + (noexcept(_Algo::__connect(__child<_Is>(), + __declval<__state_t &>(), + __declval<__nth_child_of_t<_Sender, _Is>>(), + __declval<__receiver_t<_Is>>())) + && ...); + + STDEXEC_ATTRIBUTE(host, device) + static constexpr auto + __connect(_Sender &&__sndr, __state_t &__state) noexcept(__nothrow_v) -> __t + { + return __t{_Algo::__connect(__child<_Is>(), + __state, + __facade::__get_child<_Is>(static_cast<_Sender &&>(__sndr)), + __receiver_t<_Is>{&__state})...}; + } + }; + + template + inline constexpr std::size_t __connected_children_v = _Algo::__connects_children + ? __nbr_children_v<_Sender> + : 0; + + template + using __children_ops_t = __children_ops<_Algo, + _Sender, + _Receiver, + __make_indices<__connected_children_v<_Algo, _Sender>>>; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __opstate + { + using operation_state_concept = operation_state_tag; + using __state_t = __state_type_t<_Algo, _Sender, _Receiver>; + using __children_t = __children_ops_t<_Algo, _Sender, _Receiver>; + using __ops_t = __children_t::__t; + + static constexpr bool __nothrow_v = noexcept(_Algo::__get_state(__declval<_Sender>(), + __declval<_Receiver>())) + && __children_t::__nothrow_v; + + STDEXEC_ATTRIBUTE(host, device) + constexpr explicit __opstate(_Sender &&__sndr, _Receiver __rcvr) noexcept(__nothrow_v) + : __state_( + _Algo::__get_state(static_cast<_Sender &&>(__sndr), static_cast<_Receiver &&>(__rcvr))) + , __ops_(__children_t::__connect(static_cast<_Sender &&>(__sndr), __state_)) + {} + + STDEXEC_IMMOVABLE(__opstate); + + STDEXEC_ATTRIBUTE(host, device) + constexpr void start() & noexcept + { + __children_ref<__connected_children_v<_Algo, _Sender>, __ops_t> __children{&__ops_}; + (void) _Algo::__start(__state_, __children); + } + + __state_t __state_; + __ops_t __ops_; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // The walk: a consteval traversal from start() through the effect tokens that finds + // every (signature, provenance) event. It evaluates each nested sender's + // get_completion_signatures, so an ill-typed nested sender fails the walk the same way + // it would fail any other algorithm: by throwing, or by returning the error. + + // The sender of the child or nested operation keyed by _Key + template + struct __nested_sender_of + { + using __t = __unwrap_type< + decltype(_Algo::__nested_sender(_Key(), + __declval<__decay_t<_Sender> const &>(), + __declval<__decay_t<_Env> const &>()...))>::__t; + }; + + template + struct __nested_sender_of<_Algo, _Sender, __child<_Idx>, _Env...> + { + using __t = __nth_child_of_t<_Sender, _Idx>; + }; + + // Is the nested sender keyed by _Key known only by its type (because it is created at + // run time), or can attribute queries inspect it? + template + inline constexpr bool __type_only_v = + __is_instance_of const &>(), + __declval<__decay_t<_Env> const &>()...)), + __mtype>; + + template + inline constexpr bool __type_only_v<_Algo, _Sender, __child<_Idx>, _Env...> = false; + + // The receiver archetype of a walk that has no environment. Its environment is env<>, + // but __completions_of_t recognizes it, and asks for completions without one. + STDEXEC_MODULE_EXPORT_AUTHORING + struct __no_env_receiver : __receiver_archetype_base + { + [[nodiscard]] + STDEXEC_ATTRIBUTE(host, device) constexpr auto get_env() const noexcept -> env<> + { + return {}; + } + }; + + template + struct __walk_receiver + { + using __t = __no_env_receiver; + }; + + template + struct __walk_receiver<_Env> + { + using __t = __receiver_archetype<_Env>; + }; + + // The completion signatures of the child or nested operation keyed by _Key of a sender + // connected to a receiver of type _Receiver, as the walk sees them. An algorithm whose + // operation state depends on them must use this, so that its state agrees with the + // walk even when the walk has no environment. + template + struct __completions_of + { + using __env_t = env_of_t<_Receiver>; + using __t = __completion_signatures_of_t< + typename __nested_sender_of<_Algo, _Sender, _Key, __env_t>::__t, + __facade::__env_t<_Algo, _Key, _Sender, __env_t>>; + }; + + template + struct __completions_of<_Algo, _Key, _Sender, __no_env_receiver> + { + using __t = + __completion_signatures_of_t::__t>; + }; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + using __completions_of_t = __completions_of<_Algo, _Key, _Sender, _Receiver>::__t; + + template + struct __ctx + { + using __algo_t = _Algo; + using __sndr_t = _Sender; + using __rcvr_t = __walk_receiver<_Env...>::__t; + + template + using __nested_sender_t = __nested_sender_of<_Algo, _Sender, _Key, _Env...>::__t; + + // A nested sender is asked for its completions in its environment, or in none if + // there is none, so that a dependent child makes the walk dependent. + template + static consteval auto __nested_completions() + { + return STDEXEC::get_completion_signatures< + __nested_sender_t<_Key>, + __facade::__env_t<_Algo, _Key, _Sender, _Env>...>(); + } + + template + static constexpr bool __nested_ok = + sender_in<__nested_sender_t<_Key>, __facade::__env_t<_Algo, _Key, _Sender, _Env>...>; + + // The algorithm's own completions depend on an environment, and there is none. + static constexpr bool __dependent = sizeof...(_Env) == 0 + && _Algo::template __depends_on_env<__decay_t<_Sender>>; + }; + + // Computed only once the children are known to type-check, because the state type may + // depend on their completions, and once the algorithm has any environment it needs. + template + using __state_of_t = + __state_type_t; + + // Concatenates lists of events, or returns the first error. (Errors are returned only + // when constexpr exceptions are not available; otherwise they are thrown.) The walk + // passes a list of events as a value of type __mtype<__mlist<_Events...>>. + template + consteval auto __concat_events(_Results...) noexcept + { + using __error_t = __mfind_error<_Results...>; + if constexpr (__merror<__error_t>) + return __error_t(); + else + return __mtype<__mcall<__mconcat<>, __t<_Results>...>>(); + } + + template + consteval auto __token_events(__effects<_Items...>); + + template + consteval auto __sig_events(__mtype<_Tag(_Args...)>) + { + using __token_t = decltype(_Ctx::__algo_t::__complete(_Key(), + __declval<__state_of_t<_Ctx> &>(), + _Tag(), + __declval<_Args>()...)); + return __facade::__token_events<_Ctx, __via<_Key, _Tag, _Src>>(__token_t()); + } + + template + consteval auto __sigs_events(completion_signatures<_Sigs...>) + { + return __facade::__concat_events( + __facade::__sig_events<_Ctx, _Key, _Src>(__mtype<_Sigs>())...); + } + + template + consteval auto __nested_events() + { + auto __sigs = _Ctx::template __nested_completions<_Key>(); + STDEXEC_IF_OK(__sigs) + { + return __facade::__sigs_events<_Ctx, _Key, _Src>(__sigs); + } + } + + template + consteval auto __item_events(__mtype<_Item>) + { + return __mtype<__mlist<__walk_event<_Item, _Src>>>(); + } + + // Is the operation keyed by _Key on the path to _Src? (Then starting it again from _Src + // is a loop.) + template + inline constexpr bool __on_path_v = false; + + template + inline constexpr bool __on_path_v<_Key, __via<_Key, _Tag, _Src>> = true; + + template + inline constexpr bool __on_path_v<_Key, __via<_Key2, _Tag, _Src>> = __on_path_v<_Key, _Src>; + + // (See "Attribute derivation" below.) + template + struct __resolve; + + // The place where the operations that are started from _Src start, as the attributes + // see it in the walk's environment: a behavior, a domain, and a scheduler expression + template + struct __place_of; + + template + struct __place_of<__ctx<_Algo, _Sender, _Env...>, _Src> + { + using __resolved_t = __resolve<__ctx<_Algo, _Sender, _Env...>, _Src, _Env...>; + using __t = __mlist<__completion_behavior::__constant_t<__resolved_t::__behavior>, + typename __resolved_t::__domain_t, + typename __resolved_t::__sched_t>; + }; + + // Was the operation keyed by _Key started in the place _Place earlier on the path to + // _Src? + template + inline constexpr bool __started_in_v = false; + + template + inline constexpr bool __started_in_v<_Ctx, _Key, _Place, __via<_Key2, _Tag, _Src>> = + __started_in_v<_Ctx, _Key, _Place, _Src>; + + template + inline constexpr bool __started_in_v<_Ctx, _Key, _Place, __via<_Key, _Tag, _Src>> = + __same_as<_Place, __t<__place_of<_Ctx, _Src>>> || __started_in_v<_Ctx, _Key, _Place, _Src>; + + // A nested operation that is started again from one of its own completions (directly, + // or through other operations) is a loop. An iteration starts where the one before it + // ended, which can be somewhere other than where the first one started, so the + // iterations can complete in different places. But the attributes of a place are few (a + // behavior, a domain, and a scheduler expression), and those of the completions of an + // operation depend only on those of the place where it starts (see __resolve). So the + // walk follows the loop until an iteration starts in a place where an earlier iteration + // on the same path started. From then on, the iterations complete as earlier ones did, so + // starting the operation there adds no events. + template + consteval auto __item_events(__mtype<__starts<_Key>>) + { + if constexpr (!__on_path_v<_Key, _Src>) + return __facade::__nested_events<_Ctx, _Key, _Src>(); // NOLINT(bugprone-branch-clone) + else if constexpr (__started_in_v<_Ctx, _Key, __t<__place_of<_Ctx, _Src>>, _Src>) + return __mtype<__mlist<>>(); + else + return __facade::__nested_events<_Ctx, _Key, _Src>(); + } + + template + consteval auto __token_events(__effects<_Items...>) + { + using __error_t = __mfind_error<_Items...>; + if constexpr (__merror<__error_t>) // a handler reported a type error + return STDEXEC::__throw_compile_time_error_r<__mtype<__mlist<>>>(__error_t()); + else + return __facade::__concat_events(__facade::__item_events<_Ctx, _Src>(__mtype<_Items>())...); + } + + template + consteval auto __error_or_nothing(_Sigs __sigs) + { + if constexpr (__merror<_Sigs>) + return __sigs; + else + return __mtype<__mlist<>>(); + } + + template + consteval auto __walk_impl(__indices<_Is...>) + { + if constexpr (!(_Ctx::template __nested_ok<__child<_Is>> && ...)) + { + // Evaluating an ill-typed child's get_completion_signatures throws its error (or + // returns it), which becomes this sender's error. + return __facade::__concat_events( + __facade::__error_or_nothing(_Ctx::template __nested_completions<__child<_Is>>())...); + } + else if constexpr (_Ctx::__dependent) + { + // Checked after the children, so that a child's type error is still reported when + // the sender is constructed. + return STDEXEC::__throw_dependent_sender_error_r<__mtype<__mlist<>>, + typename _Ctx::__sndr_t>(); + } + else if constexpr (__merror<__state_of_t<_Ctx>>) // __get_state reported a type error + { + return STDEXEC::__throw_compile_time_error_r<__mtype<__mlist<>>>(__state_of_t<_Ctx>()); + } + else + { + using __children_t = + __children_ref<__connected_children_v, + void>; + using __start_token_t = decltype(_Ctx::__algo_t::__start(__declval<__state_of_t<_Ctx> &>(), + __declval<__children_t &>())); + return __facade::__token_events<_Ctx, __from_start>(__start_token_t()); + } + } + + template + consteval auto __walk() + { + return __facade::__walk_impl<__ctx<_Algo, _Sender, _Env...>>( + __make_indices<__nbr_children_v<_Sender>>()); + } + + consteval auto __is_ok(auto __result) noexcept -> bool + { + return !__merror; + } + + // Did the walk succeed? This is false, rather than a hard error, when the walk throws. + // (The same technique as __constant_completion_signatures.) + template + concept __walk_ok = requires { + typename __mbool<__facade::__is_ok(__facade::__walk<_Algo, _Sender, _Env...>())>; + } && __facade::__is_ok(__facade::__walk<_Algo, _Sender, _Env...>()); + + template + using __events_t = __t())>; + + template + struct __sigs_of; + + template + struct __sigs_of<__mlist<__walk_event<_Sigs, _Srcs>...>> + { + using __t = __mcall<__munique<__qq>, _Sigs...>; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // Attribute derivation: resolve each provenance to a (behavior, domain, scheduler + // expression) triple, then join over the events of a channel. + using __behavior_t = __completion_behavior::__behavior; + + // NOT consteval: Clang 16 rejects the calls to __seq and __join in the static data + // member initializers below ("cannot take address of consteval function"). + + // Run one thing, then (from its completion) another: may complete inline only if + // both may; may be asynchronous or non-affine if either may. + constexpr auto __seq(__behavior_t __first, __behavior_t __second) noexcept -> __behavior_t + { + constexpr auto __inline_bit = std::uint8_t(__completion_behavior::__inline_); + auto const __a = static_cast(__first); + auto const __b = static_cast(__second); + return static_cast<__behavior_t>(((__a | __b) & ~__inline_bit) | (__a & __b & __inline_bit)); + } + + // One of several things: the union of the possibilities. + template + constexpr auto __join(_Bs... __bs) noexcept -> __behavior_t + { + return static_cast<__behavior_t>((std::uint8_t(0) | ... | static_cast(__bs))); + } + + // Domains are compile-time information, so a channel whose completions can happen in + // several domains reports all of them. + template + struct __leaf_domains + { + using __t = __mlist<_Domain>; + }; + + template + struct __leaf_domains> + { + using __t = __mcall<__mconcat<>, typename __leaf_domains<_Domains>::__t...>; + }; + + template + struct __domain_of_list + { + using __t = void; // no domains: unknown + }; + + template + struct __domain_of_list<__mlist<_Domain>> + { + using __t = _Domain; + }; + + // Several domains: their common type, if they have one (when one is a base of the + // others), and otherwise all of them. + template + consteval auto __domain_of_several() noexcept + { + if constexpr (__minvocable_q) + return __mtype>(); + else + return __mtype>(); + } + + template + struct __domain_of_list<__mlist<_D0, _D1, _Ds...>> + { + using __t = decltype(__facade::__domain_of_several<_D0, _D1, _Ds...>())::__t; + }; + + template + using __domain_join_t = __domain_of_list< + __mcall<__mconcat<__munique<>>, typename __leaf_domains<_Domains>::__t...>>::__t; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __attrs; + + // The facade sender that a sender is (it may be of a type derived from one) + template + auto __as_facade_sender(__sender<_Tag, _Data, _Child...> const &) + -> __sender<_Tag, _Data, _Child...>; + + template + struct __facade_sender_of + { + using __t = void; + }; + + template + requires requires { __facade::__as_facade_sender(__declval<__decay_t<_Sender> const &>()); } + struct __facade_sender_of<_Sender> + { + using __t = decltype(__facade::__as_facade_sender(__declval<__decay_t<_Sender> const &>())); + }; + + template + using __facade_sender_t = __facade_sender_of<_Sender>::__t; + + // A domain may customize a facade sender by giving its data and children to another + // facade algorithm (that is, by transforming __sender into + // __sender). Then the sender's operation is that algorithm's, and + // so are its completions and attributes. And because the parts are the same, the + // attributes can be computed from the sender as it is, without transforming it. + template + struct __retag + { + using __t = void; + }; + + template + requires(!__same_as<_Tag, _Tag2>) + struct __retag<__sender<_Tag, _Data, _Child...>, __sender<_Tag2, _Data, _Child...>> + { + using __t = __algorithm<_Tag2>; + }; + + // A domain that customizes facade algorithms by re-tagging their senders says so, with a + // member `static constexpr bool __retags_facade_senders = true;`. Then the facade sees + // through its customizations: in an environment in which such a domain customizes a + // sender by re-tagging it, the sender's completion attributes are the re-tagged + // sender's (see __attrs), as are its completion signatures (which get_completion_signatures + // computes for the customized sender). Seeing through a customization means asking the + // domain how it would customize the sender, so the facade asks only the domains that + // say they re-tag: a domain that decides how to customize a sender by asking the sender + // for its own attributes (as task_scheduler_domain does for bulk) would be asked again. + // And asking every domain would cost every attribute query a transformation. + template + concept __retagging_domain = requires { requires bool(_Domain::__retags_facade_senders); }; + + // The algorithm of the sender as the domain customizes it in the environment, if that + // is a re-tagging by a domain that says it re-tags; otherwise void + template + struct __customized_algo + { + using __t = void; + }; + + template + requires(!__same_as<__facade_sender_t<_Sender>, void>) + && __retagging_domain< + __call_result_t, env_of_t<_Sender>, _Env const &>> + && __callable, _Env const &> + struct __customized_algo<_Sender, _Env> + { + using __t = + __retag<__facade_sender_t<_Sender>, + __decay_t, _Env const &>>>::__t; + }; + + template + using __customized_algo_t = __customized_algo<_Sender, _Env...>::__t; + + // The attributes of the child or nested operation keyed by _Key: those of its sender, + // as its domain customizes it if that is a re-tagging, unless the algorithm says + // otherwise with a __nested_attrs hook + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __nested_attrs_of(_Key __key, + _Sender const &__sndr, + _Env const &...__env) noexcept -> decltype(auto) + { + if constexpr (requires { _Algo::__nested_attrs(__key, __sndr, __env...); }) + { + return _Algo::__nested_attrs(__key, __sndr, __env...); + } + else + { + using __nested_t = decltype(_Algo::__nested_sender(__key, __sndr, __env...)); + if constexpr (sizeof...(_Env) == 1 && __same_as<__nested_t, __nested_t &>) + { + using __algo2_t = + __customized_algo_t<__nested_t, + __facade::__env_t<_Algo, _Key, _Sender, _Env const &>...>; + if constexpr (!__same_as<__algo2_t, void>) + { + using __base_t = __facade_sender_t<__nested_t>; + return __attrs<__algo2_t, __base_t>{_Algo::__nested_sender(__key, __sndr, __env...)}; + } + else + { + return STDEXEC::get_env(_Algo::__nested_sender(__key, __sndr, __env...)); + } + } + else + { + return STDEXEC::get_env(_Algo::__nested_sender(__key, __sndr, __env...)); + } + } + } + + // Scheduler expressions, evaluated when the attributes are queried + struct __start_scheduler + { + template + STDEXEC_ATTRIBUTE(host, device) + static constexpr auto __eval(_Sender const &, _Env const &__env) noexcept + { + return STDEXEC::get_start_scheduler(__env); + } + }; + + template + struct __completion_scheduler_of + { + template + STDEXEC_ATTRIBUTE(host, device) + static constexpr auto __eval(_Sender const &__sndr, _Env const &...__env) noexcept + { + return STDEXEC::get_completion_scheduler<_Tag>(__facade::__nested_attrs_of<_Algo>(_Key(), + __sndr, + __env...), + _Algo::__env_for(_Key(), __sndr, __env)...); + } + }; + + template + struct __resolve; + + template + struct __resolve<_Ctx, __from_start, _QEnv...> + { + static constexpr __behavior_t __behavior = __behavior_t::__inline_completion; + static constexpr bool __at_start = true; + + static constexpr bool __has_start_sched = sizeof...(_QEnv) == 1 + && __callable; + + // Agrees with the start scheduler, if there is one (see __check_domain). + static consteval auto __get_domain() noexcept + { + if constexpr (__has_start_sched) + { + using __sch_t = __call_result_t; + return __mtype< + __call_result_t, __sch_t, _QEnv const &...>>(); + } + else if constexpr (sizeof...(_QEnv) == 1 && __callable) + return __mtype<__call_result_t>(); + else if constexpr (sizeof...(_QEnv) == 1) + return __mtype(); + else + return __mtype(); + } + + static consteval auto __get_sched() noexcept + { + if constexpr (__has_start_sched) + return __mtype<__start_scheduler>(); + else + return __mtype(); + } + + using __domain_t = decltype(__get_domain())::__t; + using __sched_t = decltype(__get_sched())::__t; + }; + + template + struct __resolve<_Ctx, __via<_Key, _Tag, _Src>, _QEnv...> + { + using __algo_t = _Ctx::__algo_t; + using __sndr_t = _Ctx::__sndr_t; + using __up_t = __resolve<_Ctx, _Src, _QEnv...>; + // A nested sender that is created at run time has only the attributes of its type. + using __attrs_t = __if_c<__type_only_v<__algo_t, __sndr_t, _Key, _QEnv...>, + env_of_t>, + __decay_t( + _Key(), + __declval<__decay_t<__sndr_t> const &>(), + __declval<_QEnv const &>()...))>>; + + using __own_behavior_t = + __call_result_t<__get_completion_behavior_t<_Tag>, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>; + static constexpr bool __affine = __completion_behavior::__is_affine(__own_behavior_t()); + static constexpr __behavior_t __behavior = __facade::__seq(__up_t::__behavior, + __own_behavior_t::value); + + static constexpr bool __has_own_domain = + __callable::__read_query_t, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>; + static constexpr bool __has_own_sched = + __callable::__read_query_t, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>; + + // A nested sender created at run time cannot be asked for its scheduler. + static constexpr bool __type_only = __type_only_v<__algo_t, __sndr_t, _Key, _QEnv...>; + static constexpr bool __own_sched = __has_own_sched && !__type_only; + + static constexpr bool __has_domain = + __callable, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>; + + // The nested operation's dispatch domain: the domain in which its customizations are + // found, which is the domain that implements it + static constexpr bool __has_dispatch_domain = + sizeof...(_QEnv) == 1 + && __callable, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>; + + // If the nested operation completes where it started, it completes on the scheduler + // on which it was started (at _Src), when that is known. Then its scheduler and domain + // are those of _Src. (So completions that started in the same place agree, even when + // they come from different operations.) + static constexpr bool __at_src = __affine && !__same_as; + + // Does the nested operation complete where the operation started? + static constexpr bool __at_start = __affine && __up_t::__at_start; + + // Does the environment of the nested operation say where it starts? (An algorithm must + // not give a nested operation the receiver's start scheduler if it does not start it + // there.) + static constexpr bool __env_says_where = + sizeof...(_QEnv) != 0 + && (__callable> + && ...); + + // Otherwise, it completes where its own scheduler says, if it has one; or, if it + // completes where it started, where it was started. But an operation that completes + // where it starts, in an environment that does not say where, can name a scheduler + // only relative to wherever it started: inline_scheduler names itself, which the one + // who asks takes to mean its own start scheduler. That is true only of an operation + // that was started where the operation started. If it was started somewhere else, + // nothing says where it completes. + static constexpr bool __believe_own_sched = __own_sched + && (!__affine || __env_says_where + || __up_t::__at_start); + + static consteval auto __get_domain() noexcept + { + if constexpr (__at_src) + return __mtype(); + else if constexpr (__believe_own_sched && __has_domain) + return __mtype< + __call_result_t, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>>(); + else if constexpr (__affine) + return __mtype(); + else if constexpr ((__has_own_domain || __has_own_sched) && __has_domain) + return __mtype< + __call_result_t, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>>(); + // Otherwise, nothing says where the nested operation completes this way. It completes + // as the domain that implements it completes it, which can be in ways that its + // sender's attributes do not mention. (A CUDA stream operation adds error completions, + // for example, which happen on the stream.) So it completes in that domain. + else if constexpr (__has_dispatch_domain) + return __mtype< + __call_result_t, + __attrs_t const &, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>>(); + else if constexpr (sizeof...(_QEnv) == 1) + return __mtype(); + else + return __mtype(); + } + + static consteval auto __get_sched() noexcept + { + if constexpr (__at_src) + return __mtype(); + else if constexpr (__believe_own_sched) + return __mtype<__completion_scheduler_of<_Key, _Tag>>(); + else + return __mtype(); + } + + using __domain_t = decltype(__get_domain())::__t; + using __sched_t = decltype(__get_sched())::__t; + }; + + template + struct __source_if + { + using __t = __mlist<>; + }; + + template + struct __source_if<_Tag, __walk_event<_Tag(_Args...), _Src>> + { + using __t = __mlist<_Src>; + }; + + template + struct __sources_of; + + template + struct __sources_of<_Tag, __mlist<_Events...>> + { + using __t = __mcall<__mconcat<__munique<>>, typename __source_if<_Tag, _Events>::__t...>; + }; + + // The sources of a channel's completions. A channel that has none has the source that + // the algorithm names for it, if any (see __defaults::__empty_channel_source). + template + struct __channel_sources + { + using __t = _Sources; + }; + + template + requires(!__same_as, void>) + struct __channel_sources<_Algo, _Tag, __mlist<>> + { + using __t = __mlist>; + }; + + template + struct __the_one_scheduler + { + using __t = void; + }; + + template + struct __the_one_scheduler<__mlist<_Sched>> + { + using __t = _Sched; + }; + + template + struct __channel_join; + + template + struct __channel_join<_Ctx, __mlist<_Srcs...>, _QEnv...> + { + static constexpr bool __has_events = sizeof...(_Srcs) != 0; + + static constexpr __behavior_t __behavior = __facade::__join( + __resolve<_Ctx, _Srcs, _QEnv...>::__behavior...); + + static consteval auto __get_domain() noexcept + { + if constexpr ((__same_as::__domain_t, void> + || ...)) + return __mtype(); + else + return __mtype< + __domain_join_t::__domain_t...>>(); + } + + using __domain_t = decltype(__get_domain())::__t; + + // A completion scheduler is a runtime value, so report one only if every event on + // this channel gets its scheduler from the same place. + using __sched_t = __the_one_scheduler< + __mcall<__munique<>, typename __resolve<_Ctx, _Srcs, _QEnv...>::__sched_t...>>::__t; + }; + + template + struct __dispatch_domain + { + using __t = __resolve<_Ctx, _Src, _QEnv...>::__domain_t; + }; + + template + struct __dispatch_domain<_Ctx, void, _QEnv...> + { + using __t = void; + }; + + // The domain of a nested operation's completion, if it can complete that way, or if its + // attributes say where it would (as the standard's SCHED-ATTRS do for continues_on, of a + // child that never completes with a value) + template + struct __dispatch_domain<_Ctx, __via<_Key, _Tag, _Src>, _QEnv...> + { + using __algo_t = _Ctx::__algo_t; + using __sndr_t = _Ctx::__sndr_t; + using __resolved_t = __resolve<_Ctx, __via<_Key, _Tag, _Src>, _QEnv...>; + + static consteval auto __get() noexcept + { + if constexpr (__sends<_Tag, + typename _Ctx::template __nested_sender_t<_Key>, + __facade::__env_t<__algo_t, _Key, __sndr_t, _QEnv const &>...>) + return __mtype(); + else if constexpr (__resolved_t::__has_own_domain || __resolved_t::__own_sched) + return __mtype(); + else + return __mtype(); + } + + using __t = decltype(__get())::__t; + }; + + // An object with no attributes + struct __no_attrs + { + void query() const noexcept = delete; + }; + + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __attrs + { + template + using __join_t = __channel_join< + __ctx<_Algo, _Sender, _QEnv...>, + typename __channel_sources< + _Algo, + _Tag, + typename __sources_of<_Tag, __events_t<_Algo, _Sender, _QEnv...>>::__t>::__t, + _QEnv...>; + + template + using __dispatch_domain_t = __dispatch_domain<__ctx<_Algo, _Sender, _QEnv...>, + typename _Algo::__dispatch_source, + _QEnv...>::__t; + + using __fwd_attrs_t = env_of_t()))>; + + // The domain in which to look for customizations of the algorithm: the dispatch domain, + // if the algorithm names one, and otherwise the domain of its value completions. Since + // it decides the customization, it is the sender's own, not the customized sender's. + template + static consteval auto __get_customizing_domain() noexcept + { + if constexpr (!__same_as<__dispatch_domain_t<_QEnv...>, void>) + return __mtype<__dispatch_domain_t<_QEnv...>>(); + else if constexpr (__walk_ok<_Algo, _Sender, _QEnv...>) + return __mtype::__domain_t>(); + else + return __mtype(); + } + + template + using __customizing_domain_t = decltype(__get_customizing_domain<_QEnv...>())::__t; + + // The algorithm of the sender as a domain that re-tags customizes it in the + // environment, or void if none does. (Only the sender's own algorithm asks: a + // customized sender's attributes are not customized again.) The customizing domain is + // checked first, so that the attributes answer get_completion_domain<>() before the + // domain is asked how it customizes the sender. (Otherwise that query would fall back + // to the value completion domain, which would ask how the domain customizes the + // sender.) + template + static consteval auto __get_customized_algo() noexcept + { + if constexpr (sizeof...(_QEnv) != 1 + || !__same_as<_Algo, __algorithm>) + return __mtype(); // NOLINT(bugprone-branch-clone) + else if constexpr (!__retagging_domain<__customizing_domain_t<_QEnv...>>) + return __mtype(); + else + return __mtype<__customized_algo_t<_Sender, _QEnv...>>(); + } + + template + using __customized_t = decltype(__get_customized_algo<_QEnv...>())::__t; + + template + static constexpr bool __customized_v = !__same_as<__customized_t<_QEnv...>, void>; + + // Without an environment, a sender is not customized, so a sender that a domain that + // re-tags customizes in an environment can answer differently there. And a query + // function that gets no answer in an environment asks again without one (see + // get_completion_domain_t::__read_query_t), which would mix the customized sender's + // answers with the uncustomized sender's. So a sender that such a domain would + // customize (its customizing domain without an environment is one, and that domain + // changes it in an empty environment) does not answer the completion queries without + // an environment. (A sender that the domain has customized already, it leaves as it is.) + template + static consteval auto __answers_without_env() noexcept -> bool + { + if constexpr (sizeof...(_QEnv) != 0 + || !__same_as<_Algo, __algorithm>) + return true; // NOLINT(bugprone-branch-clone) + else if constexpr (!__retagging_domain<__customizing_domain_t<>>) + return true; + else if constexpr (__callable const &>) + return __same_as<__decay_t<__call_result_t const &>>, + _Sender>; + else + return false; + } + + // Each completion query is available only if the sender type-checks in the query's + // environment. + template + requires(sizeof...(_QEnv) <= 1) && (!__customized_v<_QEnv...>) + && (__answers_without_env<_QEnv...>()) + && __walk_ok<_Algo, _Sender, _QEnv...> && __join_t<_Tag, _QEnv...>::__has_events + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(__get_completion_behavior_t<_Tag>, _QEnv const &...) const noexcept + { + return __completion_behavior::__constant_t<__join_t<_Tag, _QEnv...>::__behavior>(); + } + + // (get_completion_domain<>(), whose _Tag is void, is answered below.) + template + requires(sizeof...(_QEnv) <= 1) && (!__same_as<_Tag, void>) && (!__customized_v<_QEnv...>) + && (__answers_without_env<_QEnv...>()) && __walk_ok<_Algo, _Sender, _QEnv...> + && (!__same_as::__domain_t, void>) + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(get_completion_domain_t<_Tag>, _QEnv const &...) const noexcept -> + typename __join_t<_Tag, _QEnv...>::__domain_t + { + return {}; + } + + // The domain in which to look for customizations of the algorithm (see + // __get_customizing_domain). When the algorithm names a dispatch domain, this does not + // require the sender to type-check in the query's environment, unlike the other + // completion queries, because a customization can be what makes it type-check (for + // example, by giving a child a different environment). + template + requires(sizeof...(_QEnv) <= 1) && (!__same_as<__customizing_domain_t<_QEnv...>, void>) + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(get_completion_domain_t<>, _QEnv const &...) const noexcept + -> __customizing_domain_t<_QEnv...> + { + return {}; + } + + template + requires(sizeof...(_QEnv) <= 1) && (!__customized_v<_QEnv...>) + && (__answers_without_env<_QEnv...>()) && __walk_ok<_Algo, _Sender, _QEnv...> + && (!__same_as::__sched_t, void>) + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(get_completion_scheduler_t<_Tag>, _QEnv const &...__env) const noexcept + { + return __join_t<_Tag, _QEnv...>::__sched_t::template __eval<_Algo>(__sndr_, __env...); + } + + // In an environment in which a domain that re-tags customizes the sender, the + // completion queries other than get_completion_domain<>() are answered as the + // customized sender's are. (Its algorithm computes them from the sender as it is: the + // parts of a re-tagged sender are the same.) + template + requires __completion_query<_Query> && (!__same_as<_Query, get_completion_domain_t<>>) + && __customized_v<_QEnv> + && __queryable_with<__attrs<__customized_t<_QEnv>, _Sender>, _Query, _QEnv const &> + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(_Query, _QEnv const &__env) const noexcept -> decltype(auto) + { + return __attrs<__customized_t<_QEnv>, _Sender>{__sndr_}.query(_Query(), __env); + } + + // Other forwarding queries are answered by the attributes the algorithm names. (The + // attributes' type is a template parameter so that it is checked only on use.) + template + requires(!__completion_query<_Query>) && __queryable_with<_FwdAttrs, _Query, _Args...> + STDEXEC_ATTRIBUTE(nodiscard, host, device) + constexpr auto query(_Query, _Args &&...__args) const + noexcept(__nothrow_queryable_with<_FwdAttrs, _Query, _Args...>) + -> __query_result_t<_FwdAttrs, _Query, _Args...> + { + return STDEXEC::get_env(_Algo::__fwd_sender(__sndr_)) + .query(_Query(), static_cast<_Args &&>(__args)...); + } + + _Sender const &__sndr_; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // The attributes of a nested operation that schedules onto a scheduler, for + // an algorithm's __nested_attrs hook. Where the operation completes is + // reported by the scheduler (a scheduler is the completion scheduler of its + // schedule sender), and how it completes by the schedule sender. _Scheduler + // may be a reference type. + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __schedule_attrs + { + using __scheduler_t = __decay_t<_Scheduler>; + + template + requires __callable, __scheduler_t const &, _Env const &...> + [[nodiscard]] STDEXEC_ATTRIBUTE(host, device) constexpr auto query( + get_completion_scheduler_t<_Tag>, + _Env const &...__env) const noexcept + { + return STDEXEC::get_completion_scheduler<_Tag>(__sch_, __env...); + } + + template + requires __callable, __scheduler_t const &, _Env const &...> + [[nodiscard]] STDEXEC_ATTRIBUTE(host, + device) constexpr auto query(get_completion_domain_t<_Tag>, + _Env const &...) const noexcept + -> __call_result_t, __scheduler_t const &, _Env const &...> + { + return {}; + } + + template + [[nodiscard]] STDEXEC_ATTRIBUTE(host, device) constexpr auto query( + __get_completion_behavior_t<_Tag>, + _Env const &...) const noexcept + { + return STDEXEC::__get_completion_behavior<_Tag, + schedule_result_t<__scheduler_t const &>, + _Env...>(); + } + + _Scheduler __sch_; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // The default operation state of an algorithm: its receiver and its data + STDEXEC_MODULE_EXPORT_AUTHORING + template + struct __basic_state + { + template + STDEXEC_ATTRIBUTE(host, device) + constexpr explicit __basic_state(_Receiver __rcvr, _CvData &&__data) + noexcept(__nothrow_decay_copyable<_CvData>) + : __rcvr_(static_cast<_Receiver &&>(__rcvr)) + , __data_(STDEXEC::__allocator_aware_forward(static_cast<_CvData &&>(__data), __rcvr_)) + {} + + STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS + _Receiver __rcvr_; + STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS + _Data __data_; + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // Default implementations of the algorithm hooks + STDEXEC_MODULE_EXPORT_AUTHORING + struct __defaults + { + // The base class of the receivers that the facade connects the algorithm's children + // and nested operations to. (A domain's algorithm can use this to make them the kind + // of receiver that its senders complete differently, as a CUDA stream sender + // completes a stream receiver on the host.) + struct __receiver_base + {}; + + // Opt-in: do the algorithm's own completions depend on the receiver's environment? + // (Dependence on the environment through a child needs no opt-in.) If so, the sender + // is dependent when there is no environment. _Sender is the __sender type. + template + static constexpr bool __depends_on_env = false; + + // Does the facade connect the children, so that __start can start them? An algorithm + // that connects (some of) its children itself sets this to false. + static constexpr bool __connects_children = true; + + // The domain in which customizations of the algorithm are found, given as the + // provenance whose completion domain it is, or void for the domain of the value + // completions. + using __dispatch_source = void; + + // The provenance whose attributes a channel has when nothing completes on it, or void + // for none. Such attributes are vacuous, but they let an algorithm report, for example, + // the scheduler on which its value completions would happen, as the standard's + // SCHED-ATTRS does for continues_on. + template + using __empty_channel_source = void; + + // Connects the child or nested operation keyed by _Key. The facade connects the + // children with this, and an algorithm that connects operations in its state can too. + // (A domain's algorithm can use it to adapt a child that would complete the receiver + // in a way the algorithm cannot handle, as when a CUDA stream operation completes a + // receiver from a kernel.) + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __connect(_Key, _State &, _Sender &&__sndr, _Receiver __rcvr) + noexcept(__nothrow_connectable<_Sender, _Receiver>) -> connect_result_t<_Sender, _Receiver> + { + return STDEXEC::connect(static_cast<_Sender &&>(__sndr), static_cast<_Receiver &&>(__rcvr)); + } + + // The environment of the child or nested operation keyed by _Key, given the sender + // and the environment of the receiver connected to it. The environment is forwarded, + // so that the result's type is the one __get_env returns at run time (where the + // receiver's environment is whatever get_env returns). + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __env_for(_Key, _Sender const &, _Env &&__env) noexcept -> _Env + { + return static_cast<_Env &&>(__env); + } + + // The same environment, at run time + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __get_env(_Key, _State const &__state) noexcept -> decltype(auto) + { + return STDEXEC::get_env(__state.__rcvr_); + } + + // The sender of the child or nested operation keyed by _Key. It is used for its type, + // and by the attributes for its own attributes. (A nested sender that is created at + // run time is returned as an __mtype.) + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __nested_sender(__child<_Idx>, + _Sender const &__sndr, + _Env const &...) noexcept -> auto const & + { + return __facade::__get_child<_Idx>(__sndr); + } + + // The sender whose attributes answer forwarding queries that are not completion + // queries: the child, if there is exactly one. + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __fwd_sender(_Sender const &__sndr) noexcept -> decltype(auto) + { + if constexpr (__nbr_children_v<_Sender> == 1) + return __facade::__get_child<0>(__sndr); + else + return __no_attrs(); + } + + template + STDEXEC_ATTRIBUTE(host, device) + static constexpr auto __get_state(_Sender &&__sndr, _Receiver __rcvr) + noexcept(__nothrow_decay_copyable<__data_of_t<_Sender>>) + -> __basic_state<_Receiver, __decay_t<__data_of_t<_Sender>>> + { + return __basic_state<_Receiver, __decay_t<__data_of_t<_Sender>>>{ + static_cast<_Receiver &&>(__rcvr), + __facade::__get_data(static_cast<_Sender &&>(__sndr))}; + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __start(_State &, _Children &__children) noexcept + { + return __children.__start_all(); + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __complete(_Key, + _State &__state, + _Tag __tag, + _Args &&...__args) noexcept + { + return __facade::__send(__state.__rcvr_, __tag, static_cast<_Args &&>(__args)...); + } + }; + + //////////////////////////////////////////////////////////////////////////////////////// + // The senders of facade algorithms + + // The attributes of a sender: the ones that the facade derives, unless the algorithm + // gives them itself with a hook `__get_attrs(sndr)` + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __attrs_of(_Sender const &__sndr) noexcept + { + if constexpr (requires { _Algo::__get_attrs(__sndr); }) + return _Algo::__get_attrs(__sndr); + else + return __attrs<_Algo, _Sender>{__sndr}; + } + + // The completion signatures of a sender: the ones that the facade derives, unless the + // algorithm gives them itself with a hook `__get_completion_signatures()`. + // If the sender does not type-check, this throws (or returns) the walk's error. + template + consteval auto __completion_signatures_of() + { + if constexpr (requires { _Algo::template __get_completion_signatures<_Sender, _Env...>(); }) + { + return _Algo::template __get_completion_signatures<_Sender, _Env...>(); + } + else + { + auto __events = __facade::__walk<_Algo, _Sender, _Env...>(); + STDEXEC_IF_OK(__events) + { + return typename __sigs_of<__t>::__t(); + } + } + } + + // Like the senders of the standard algorithms, a facade sender is a product type of its + // tag, its data, and its children: + // + // auto& [tag, data, ...children] = sndr; + // + // An algorithm can give its senders a name of their own with a type derived from this. + // Such a sender is seen, by the algorithm's hooks, as a __sender. + template + struct __sender : __tuple<_Tag, _Data, _Child...> + { + using sender_concept = sender_tag; + using __tag_t = _Tag; + using __data_t = _Data; + using __children_t = __mlist<_Child...>; + + template + using __opstate_t = __opstate<__algorithm<_Tag>, __copy_cvref_t<_Self, __sender>, _Receiver>; + + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto get_env() const noexcept + { + return __facade::__attrs_of<__algorithm<_Tag>>(*this); + } + + template + static consteval auto get_completion_signatures() + { + static_assert(STDEXEC_IS_BASE_OF(__sender, __decay_t<_Self>)); + return __facade::__completion_signatures_of<__algorithm<_Tag>, + __copy_cvref_t<_Self, __sender>, + _Env...>(); + } + + template + requires sender_in<__copy_cvref_t<_Self, __sender>, env_of_t<_Receiver>> + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto STDEXEC_EXPLICIT_THIS_BEGIN(connect)(this _Self &&__self, _Receiver __rcvr) + noexcept(__opstate_t<_Self, _Receiver>::__nothrow_v) -> __opstate_t<_Self, _Receiver> + { + return __opstate_t<_Self, _Receiver>(STDEXEC::__c_upcast<__sender>( + static_cast<_Self &&>(__self)), + static_cast<_Receiver &&>(__rcvr)); + } + STDEXEC_EXPLICIT_THIS_END(connect) + }; + + template + STDEXEC_HOST_DEVICE_DEDUCTION_GUIDE + __sender(_Tag, _Data, _Child...) -> __sender<_Tag, _Data, _Child...>; + } // namespace __facade +} // namespace STDEXEC + +# include "__epilogue.hpp" +#endif // !STDEXEC_USE_MODULES() || defined(STDEXEC_IN_MODULE_PURVIEW) diff --git a/include/stdexec/__detail/__utility.hpp b/include/stdexec/__detail/__utility.hpp index cdf5ef693..f576615f8 100644 --- a/include/stdexec/__detail/__utility.hpp +++ b/include/stdexec/__detail/__utility.hpp @@ -213,6 +213,7 @@ namespace STDEXEC STDEXEC_PRAGMA_PUSH() STDEXEC_PRAGMA_IGNORE_GNU("-Wold-style-cast") + // NOLINTBEGIN(modernize-avoid-c-style-cast) // A derived-to-base cast that works even when the base is not accessible from // derived. @@ -236,6 +237,7 @@ namespace STDEXEC return (__copy_cvref_t<_Up&&, _Tp>) static_cast<_Up&&>(u); } + // NOLINTEND(modernize-avoid-c-style-cast) STDEXEC_PRAGMA_POP() template diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6975fd9f1..fe3709723 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -101,6 +101,7 @@ if(NOT STDEXEC_BUILD_MODULES) stdexec/cpos/test_cpo_bulk.cpp stdexec/cpos/test_cpo_upon_error.cpp stdexec/detail/test_any_allocator.cpp + stdexec/detail/test_facade.cpp stdexec/schedulers/test_parallel_scheduler.cpp) endif() diff --git a/test/stdexec/detail/test_facade.cpp b/test/stdexec/detail/test_facade.cpp new file mode 100644 index 000000000..6a9914b3b --- /dev/null +++ b/test/stdexec/detail/test_facade.cpp @@ -0,0 +1,400 @@ +/* + * Copyright (c) 2026 NVIDIA Corporation + * + * Licensed under the Apache License Version 2.0 with LLVM Exceptions + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://llvm.org/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Tests for the sender facade in , through algorithms +// that are written with it for the tests. None of them has hand-written completion +// signatures or hand-written completion attributes. + +// Catch2 must come before any potential import statements +#include + +#include +#include + +#if STDEXEC_USE_MODULES() +import std; +#else +# include +# include +# include +# include +# include +# include +# include +# include +#endif + +namespace ex = STDEXEC; + +namespace +{ + // The tag of a facade algorithm that is written for the tests + template + struct test_tag + {}; +} // namespace + +template +struct STDEXEC::__facade::__algorithm> : Algo +{}; + +namespace +{ + template + using test_sender_t = ex::__facade::__sender, Data, Children...>; + + using cb = ex::__completion_behavior; + + ////////////////////////////////////////////////////////////////////////////////////////// + // Probe senders whose channels have different attributes. (They are two senders because + // a sender that completes inline completes on its start scheduler, so it cannot also + // claim an unrelated domain; stdexec checks this.) + struct value_domain : ex::default_domain + {}; + + struct error_domain : ex::default_domain + {}; + + // Value completions happen in value_domain; error completions in error_domain. + struct domain_attrs + { + constexpr auto query(ex::get_completion_domain_t) const noexcept + { + return value_domain{}; + } + + constexpr auto query(ex::get_completion_domain_t) const noexcept + { + return error_domain{}; + } + }; + + // Value completions are asynchronous; error completions are inline. + struct behavior_attrs + { + constexpr auto query(ex::__get_completion_behavior_t) const noexcept + { + return cb::__asynchronous; + } + + constexpr auto query(ex::__get_completion_behavior_t) const noexcept + { + return cb::__inline_completion; + } + }; + + template + struct probe + { + using sender_concept = ex::sender_tag; + + template + static consteval auto get_completion_signatures() + { + return ex::completion_signatures(); + } + + template + struct opstate + { + using operation_state_concept = ex::operation_state_tag; + + void start() & noexcept + { + ex::set_error(static_cast(rcvr), 42); + } + + Rcvr rcvr; + }; + + template + auto connect(Rcvr rcvr) const -> opstate + { + return {static_cast(rcvr)}; + } + + auto get_env() const noexcept -> Attrs + { + return {}; + } + }; + + using domain_probe = probe; + using behavior_probe = probe; + + ////////////////////////////////////////////////////////////////////////////////////////// + // Helpers + template + inline constexpr bool sigs_subset_v = false; + + template + inline constexpr bool sigs_subset_v, Others...> = + (ex::__one_of && ...); + + template + inline constexpr bool same_sigs_v = false; + + template + inline constexpr bool + same_sigs_v, ex::completion_signatures> = + sigs_subset_v, Sigs2...> + && sigs_subset_v, Sigs1...>; + + template + inline constexpr bool has_sigs_v = same_sigs_v>, + ex::completion_signatures>; + + template + using domain_of_t = decltype(ex::get_completion_domain(ex::get_env(std::declval()), + std::declval()...)); + + template + inline constexpr auto behavior_of_v = ex::__get_completion_behavior(); + + template + concept has_completion_scheduler = requires(Sndr const &sndr, Env const &...env) { + ex::get_completion_scheduler(ex::get_env(sndr), env...); + }; + + // Is Domain indeterminate_domain for exactly the given set of domains? + template + inline constexpr bool is_indeterminate_of_v = false; + + template + inline constexpr bool is_indeterminate_of_v, Ds2...> = + sizeof...(Ds1) == sizeof...(Ds2) && (ex::__one_of && ...); + + using eptr_sig_t = ex::set_error_t(std::exception_ptr); + using run_loop_sch_t = decltype(std::declval().get_scheduler()); + using start_env_t = decltype(ex::prop{ex::get_start_scheduler, std::declval()}); + using stop_env_t = ex::env, + ex::prop>; + + // A function that may throw, and one that may not: + inline constexpr auto may_throw = [](auto...) + { + return 42; + }; + inline constexpr auto no_throw = [](auto...) noexcept + { + return 42; + }; + + ////////////////////////////////////////////////////////////////////////////////////////// + // 0. Algorithms written for the tests: t_just(vs...) completes from start(), + // t_then(sndr, fn) transforms the value completions of its child, and t_schedule(sch) + // completes from a nested schedule operation. + struct t_just_algo : ex::__facade::__defaults + { + template + static constexpr auto __start(State &state, Children &) noexcept + { + return ex::__apply( + [&](Ts &&...vs) noexcept + { return ex::__facade::__send(state.__rcvr_, ex::set_value, static_cast(vs)...); }, + std::move(state.__data_)); + } + }; + + struct t_then_algo : ex::__facade::__defaults + { + template + static constexpr auto + __complete(ex::__facade::__child<0>, State &state, Tag tag, Args &&...args) noexcept + { + if constexpr (std::is_same_v) + return ex::__facade::__set_value_from>(state.__rcvr_, + std::move(state.__data_), + static_cast(args)...); + else + return ex::__facade::__send(state.__rcvr_, tag, static_cast(args)...); + } + }; + + // The key of t_schedule's nested schedule operation + struct sched_key + {}; + + struct t_schedule_algo; + + template + struct t_schedule_state + { + using scheduler_t = std::decay_t>; + using sched_sndr_t = ex::schedule_result_t; + using sched_rcvr_t = + ex::__facade::__receiver_t; + + explicit t_schedule_state(Sender &&sndr, Receiver &&rcvr) + : __rcvr_(static_cast(rcvr)) + , op_(ex::connect(ex::schedule(ex::__facade::__get_data(sndr)), sched_rcvr_t{this})) + {} + + t_schedule_state(t_schedule_state &&) = delete; + + Receiver __rcvr_; + ex::connect_result_t op_; + }; + + // The completions of the schedule operation are passed on, by the default __complete. + struct t_schedule_algo : ex::__facade::__defaults + { + using ex::__facade::__defaults::__nested_sender; + + template + static constexpr auto __nested_sender(sched_key, Sender const &sndr, Env const &...) + { + return ex::schedule(ex::__facade::__get_data(sndr)); + } + + template + static constexpr auto + __get_state(Sender &&sndr, Receiver rcvr) -> t_schedule_state + { + return t_schedule_state(static_cast(sndr), + static_cast(rcvr)); + } + + template + static constexpr auto __start(State &state, Children &) noexcept + { + return ex::__facade::__start_nested(state.op_); + } + }; + + template + constexpr auto t_just(Ts... vs) + { + return ex::__facade::__sender{test_tag(), ex::__tuple{std::move(vs)...}}; + } + + template + constexpr auto t_then(Sndr sndr, Fn fn) + { + return ex::__facade::__sender{test_tag(), std::move(fn), std::move(sndr)}; + } + + template + constexpr auto t_schedule(Sch sch) + { + return ex::__facade::__sender{test_tag(), std::move(sch)}; + } + + using t_just_t = decltype(t_just(1)); + using t_schedule_t = decltype(t_schedule(std::declval())); + + // The completion signatures fall out of the code: + static_assert(has_sigs_v); + static_assert( + has_sigs_v); + static_assert(has_sigs_v); + static_assert(has_sigs_v); + + // A completion from start() happens inline, on the scheduler on which the operation + // starts. + static_assert(behavior_of_v == cb::__inline_completion); + static_assert(std::is_same_v( + ex::get_env(std::declval()), + std::declval())), + run_loop_sch_t>); + + // A completion from a child's completion happens where the child completes. When fn may + // throw, t_then's error completions also come from the child's *value* completions, so + // they may happen in either domain, inline or asynchronously. + using t_then_throw_t = decltype(t_then(domain_probe{}, may_throw)); + static_assert( + std::is_same_v>, value_domain>); + static_assert(is_indeterminate_of_v>, + value_domain, + error_domain>); + static_assert( + behavior_of_v> + == (cb::__inline_completion | cb::__asynchronous)); + + // When fn cannot throw, the error channel is exactly the child's. + using t_then_nothrow_t = decltype(t_then(domain_probe{}, no_throw)); + static_assert( + std::is_same_v>, error_domain>); + static_assert( + behavior_of_v> + == cb::__inline_completion); + + // A completion from a nested operation's completion happens where that operation + // completes, after the operation started (here, from start()). A run_loop's schedule + // sender can be stopped only if the receiver's stop token is stoppable, and the derived + // signatures track that. + static_assert(has_sigs_v); + static_assert(same_sigs_v, + ex::completion_signatures>); + static_assert(behavior_of_v == cb::__asynchronous); + static_assert(std::is_same_v( + ex::get_env(std::declval()), + std::declval())), + run_loop_sch_t>); + static_assert(std::is_same_v( + ex::get_env(t_then(std::declval(), no_throw)), + std::declval())), + run_loop_sch_t>); + static_assert(!has_completion_scheduler); + + // A handler that finds its inputs ill-typed reports a type error, and the sender is not + // well-formed. + static_assert( + !ex::__well_formed_sender); + + ////////////////////////////////////////////////////////////////////////////////////////// + // Run-time tests + + // Runs a run_loop on a thread of its own until destroyed + struct loop_thread + { + loop_thread() = default; + loop_thread(loop_thread &&) = delete; + + ~loop_thread() + { + loop.finish(); + worker.join(); + } + + ex::run_loop loop; + std::thread worker{[this] { loop.run(); }}; + }; + + // 0, continued. + TEST_CASE("facade: algorithms written for the tests run", "[detail][facade]") + { + auto result1 = ex::sync_wait(t_then(t_just(20), [](int i) { return 2 * i + 1; })); + REQUIRE(result1.has_value()); + CHECK(std::get<0>(*result1) == 41); + + loop_thread ctx; + auto sndr = t_then(t_schedule(ctx.loop.get_scheduler()), + [] { return std::this_thread::get_id(); }); + + // The completion scheduler is a run-time value read from the sender. + ex::run_loop loop; + auto env = ex::prop{ex::get_start_scheduler, loop.get_scheduler()}; + CHECK(ex::get_completion_scheduler(ex::get_env(sndr), env) + == ctx.loop.get_scheduler()); + + auto result2 = ex::sync_wait(std::move(sndr)); + REQUIRE(result2.has_value()); + CHECK(std::get<0>(*result2) == ctx.worker.get_id()); + } +} // namespace