/* * Copyright (c) 2021-2024 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 "__basic_sender.hpp" # include "__diagnostics.hpp" # include "__env.hpp" # include "__meta.hpp" # include "__schedulers.hpp" # include "__sender_adaptor_closure.hpp" # include "__senders.hpp" # include "__submit.hpp" # include "__transform_completion_signatures.hpp" # include "__utility.hpp" # include "__variant.hpp" # include "../functional.hpp" # if !STDEXEC_USE_MODULES() # include # endif # include "__prologue.hpp" namespace STDEXEC { ////////////////////////////////////////////////////////////////////////////// // [exec.let] namespace __let { template struct __let_t; template using __env2_t = __secondary_env_t<_Sender, _Env, _SetTag>; template using __result_env_t = __join_env_t<__env2_t<_SetTag, _Sender, _Env>, _Env>; template struct __rcvr_env { using receiver_concept = receiver_tag; _Receiver& __rcvr_; _Env2 const & __env_; template constexpr void set_value(_As&&... __as) noexcept { STDEXEC::set_value(static_cast<_Receiver&&>(__rcvr_), static_cast<_As&&>(__as)...); } template constexpr void set_error(_Error&& __err) noexcept { STDEXEC::set_error(static_cast<_Receiver&&>(__rcvr_), static_cast<_Error&&>(__err)); } constexpr void set_stopped() noexcept { STDEXEC::set_stopped(static_cast<_Receiver&&>(__rcvr_)); } [[nodiscard]] constexpr auto get_env() const noexcept // -> decltype(__env::__join(__env_, STDEXEC::get_env(__rcvr_))) { return __env::__join(__env_, STDEXEC::get_env(__rcvr_)); } }; struct _FUNCTION_MUST_RETURN_A_VALID_SENDER_IN_THE_CURRENT_ENVIRONMENT_; template struct _NESTED_ERROR_; // BUGBUG: when get_completion_signatures is using constexpr exceptions, // this __try_completion_signatures_of_t machinery hides the nested error // from trying to compute the result senders. template using __try_completion_signatures_of_t = __minvoke_or_q<__completion_signatures_of_t, __unrecognized_sender_error_t<_Sender, _Env...>, _Sender, _Env...>; template using __bad_result_sender_t = __mexception< _WHAT_(_FUNCTION_MUST_RETURN_A_VALID_SENDER_IN_THE_CURRENT_ENVIRONMENT_), _WHERE_(_IN_ALGORITHM_, _LetTag), _WITH_SENDER_<_Sender>, __fn_t<_WITH_ENVIRONMENT_, _JoinEnv2>..., __mapply_q<_NESTED_ERROR_, __try_completion_signatures_of_t<_Sender, _JoinEnv2...>>>; template using __not_decay_copyable_error_t = __mexception<_WHAT_(_PREDECESSOR_RESULTS_ARE_NOT_DECAY_COPYABLE_), _WHERE_(_IN_ALGORITHM_, _LetTag), _WITH_ARGUMENTS_(_Args...)>; template concept __potentially_valid_sender_in = sender_in<_Sender, _JoinEnv2...> || (sender<_Sender> && (sizeof...(_JoinEnv2) == 0)); //! Metafunction creating the operation state needed to connect the result //! of calling the sender factory function, `_Fun`, and passing its result //! to a receiver. template struct __submit_datum_for { // compute the result of calling submit with the result of executing _Fun // with _Args. if the result is void, substitute with __ignore. template using __f = submit_result<__invoke_result_t<_Fun, __decay_t<_Args>&...>, __rcvr_env<_Receiver, _Env2>>; }; // A metafunction to check whether the predecessor's completion results are // nothrow decay-copyable and whether connecting the secondary sender is // nothrow. template struct __has_nothrow_completions_fn { template using __sndr2_t = __invoke_result_t<_Fn, __decay_t<_Ts>&...>; using __rcvr2_t = __receiver_archetype<_Env2>; template using __f = __mbool<__nothrow_decay_copyable<_Ts...> // && __nothrow_invocable<_Fn, __decay_t<_Ts>&...> // && __nothrow_connectable<__sndr2_t<_Ts...>, __rcvr2_t>>; }; template using __has_nothrow_completions_t = __gather_completions_t< completion_signatures_of_t<_Child, _Env>, _SetTag, __has_nothrow_completions_fn<_Fn, __result_env_t<_SetTag, _Child, _Env>>, __qq<__mand_t>>; //! The core of the operation state for `let_*`. This gets bundled up into a //! larger operation state (`__detail::__op_state<...>`). template struct __opstate_base { using __env2_t = _Env2; using __second_rcvr_t = __rcvr_env<_Receiver, _Env2>; template constexpr explicit __opstate_base(_SetTag, _Sender const & __sndr, _Fun __fn, _Receiver&& __rcvr) noexcept : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __fn_(STDEXEC::__allocator_aware_forward(static_cast<_Fun&&>(__fn), __rcvr_)) // TODO(ericniebler): this needs a fallback: , __env2_(__mk_secondary_env_t<_SetTag>()(__sndr, STDEXEC::get_env(__rcvr_))) {} STDEXEC_IMMOVABLE(__opstate_base); constexpr virtual void __start_next() = 0; template constexpr void __impl(_Tag, _Args&&... __args) noexcept { if constexpr (__same_as<_SetTag, _Tag>) { using __sender_t = __invoke_result_t<_Fun, __decay_t<_Args>&...>; using __submit_t = submit_result<__sender_t, __rcvr_env<_Receiver, _Env2>>; constexpr bool __nothrow_store = (__nothrow_decay_copyable<_Args> && ...); constexpr bool __nothrow_invoke = __nothrow_invocable<_Fun, __decay_t<_Args>&...>; constexpr bool __nothrow_submit = __nothrow_constructible_from<__submit_t, __sender_t, __second_rcvr_t>; STDEXEC_TRY { __args_.__emplace_from(__mktuple, static_cast<_Args&&>(__args)...); __start_next(); } STDEXEC_CATCH_ALL { if constexpr (!(__nothrow_store && __nothrow_invoke && __nothrow_submit)) { STDEXEC::set_error(static_cast<_Receiver&&>(this->__rcvr_), std::current_exception()); } } } else { _Tag()(static_cast<_Receiver&&>(this->__rcvr_), static_cast<_Args&&>(__args)...); } } _Receiver __rcvr_; STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS _Fun __fn_; STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS _Env2 __env2_; //! Variant to hold the child sender's results before passing them to the //! function: __variant<_Tuples...> __args_{__no_init}; }; template struct __first_rcvr { using receiver_concept = STDEXEC::receiver_tag; template constexpr void set_value(_Args&&... __args) noexcept { __state_->__impl(STDEXEC::set_value, static_cast<_Args&&>(__args)...); } template constexpr void set_error(_Args&&... __args) noexcept { __state_->__impl(STDEXEC::set_error, static_cast<_Args&&>(__args)...); } template constexpr void set_stopped(_Args&&... __args) noexcept { __state_->__impl(STDEXEC::set_stopped, static_cast<_Args&&>(__args)...); } [[nodiscard]] constexpr auto get_env() const noexcept -> env_of_t<_Receiver> { return STDEXEC::get_env(__state_->__rcvr_); } __opstate_base<_SetTag, _Fun, _Receiver, _Env2, _Tuples...>* __state_; }; constexpr auto __mk_result_sndr = [](_Fun& __fn, _Args&... __args) noexcept( __nothrow_invocable<_Fun, _Args&...>) -> decltype(auto) { return STDEXEC::__invoke(static_cast<_Fun&&>(__fn), __args...); }; inline constexpr auto __start_next_fn = []( _Fun& __fn, _Receiver& __rcvr, _Env2& __env2, _Storage& __storage, _Tuple& __tupl) { decltype(auto) __sndr = STDEXEC::__apply(__mk_result_sndr, __tupl, __fn); using __sender_t = decltype(__sndr); using __submit_t = submit_result<__sender_t, __rcvr_env<_Receiver, _Env2>>; using __second_rcvr_t = __rcvr_env<_Receiver, _Env2>; __second_rcvr_t __rcvr2{__rcvr, static_cast<_Env2&&>(__env2)}; auto& __op = __storage.template emplace<__submit_t>(static_cast<__sender_t&&>(__sndr), static_cast<__second_rcvr_t&&>(__rcvr2)); __op.submit(static_cast<__sender_t&&>(__sndr), static_cast<__second_rcvr_t&&>(__rcvr2)); }; //! The core of the operation state for `let_*`. This gets bundled up into a //! larger operation state (`__detail::__op_state<...>`). template struct __opstate final : __opstate_base<_SetTag, _Fun, _Receiver, __let::__env2_t<_SetTag, _CvChild, env_of_t<_Receiver>>, _Tuples...> { using __env2_t = __opstate::__opstate_base::__env2_t; using __first_rcvr_t = __first_rcvr<_SetTag, _Fun, _Receiver, __env2_t, _Tuples...>; using __second_rcvr_t = __opstate::__opstate_base::__second_rcvr_t; using __op_state_variant_t = __variant, __mapply<__submit_datum_for<_Receiver, _Fun, _SetTag, __env2_t>, _Tuples>...>; constexpr explicit __opstate(_CvChild&& __child, _Fun __fn, _Receiver&& __rcvr) noexcept(__nothrow_connectable<_CvChild, __first_rcvr_t> && __nothrow_move_constructible<_Fun>) : __opstate::__opstate_base(_SetTag(), __child, static_cast<_Fun&&>(__fn), static_cast<_Receiver&&>(__rcvr)) { __storage_.__emplace_from(STDEXEC::connect, static_cast<_CvChild&&>(__child), __first_rcvr_t{this}); } constexpr void start() noexcept { STDEXEC_ASSERT(__storage_.index() == 0); STDEXEC::start(__var::__get<0>(__storage_)); }; constexpr void __start_next() final { STDEXEC_ASSERT(__storage_.index() == 0); if constexpr (sizeof...(_Tuples) != 0) { STDEXEC::__visit(__start_next_fn, this->__args_, this->__fn_, this->__rcvr_, this->__env2_, __storage_); } } //! Variant type for holding the operation state of the currently in //! flight operation __op_state_variant_t __storage_{__no_init}; }; // // The set_value completions of: // // // // * a let_value sender are: // // * the value completions of the secondary senders // // // // * a let_error sender are: // // * the value completions of the predecessor sender // // * the value completions of the secondary senders // // // // * a let_stopped sender are: // // * the value completions of the predecessor sender // // * the value completions of the secondary sender // // // // The set_error completions of: // // // // * a let_value sender are: // // * the error completions of the predecessor sender // // * the error completions of the secondary senders // // * the value completions of the predecessor sender if decay copying the arguments can throw // // // // * a let_error sender are: // // * the error completions of the secondary senders // // * the error completions of the predecessor sender if decay copying the errors can throw // // // // * a let_stopped sender are: // // * the error completions of the predecessor sender // // * the error completions of the secondary senders // // // // The set_stopped completions of: // // // // * a let_value sender are: // // * the stopped completions of the predecessor sender // // * the stopped completions of the secondary senders // // // // * a let_error sender are: // // * the stopped completions of the predecessor sender // // * the stopped completions of the secondary senders // // // // * a let_stopped sender are: // // * the stopped completions of the secondary sender // // // template // struct __result_completion_behavior_fn // { // template // [[nodiscard]] // static constexpr auto __impl() noexcept // { // using __sndr_t = // __minvoke_or_q<__invoke_result_t, __not_a_sender<>, _Fun, __decay_t<_Ts>&...>; // return STDEXEC::__get_completion_behavior<_SetTag, __sndr_t, _JoinEnv2...>(); // } // template // using __f = decltype(__impl<_Ts...>()); // }; // template // struct __domain_transform_fn // { // template // using __f = __completion_domain_of_t<_SetTag, // __invoke_result_t<_Fun, __decay_t<_As>&...>, // __result_env_t<_SetTag, _Sender, _Env>...>; // }; // //! @tparam _LetTag The tag type for the let_ operation. // //! @tparam _SetTag The completion signal of the let_ sender itself that is being // //! queried. For example, you may be querying a let_value sender for its set_error // //! completion domain. // template // [[nodiscard]] // consteval auto __get_completion_domain() noexcept // { // if constexpr (sender_in<_Sndr, _Env...>) // { // using __domain_transform_fn = __let::__domain_transform_fn<_SetTag, _Fun, _Sndr, _Env...>; // return __minvoke_or_q<__gather_completions_t, // indeterminate_domain<>, // __t<_LetTag>, // __completion_signatures_of_t<_Sndr, _Env...>, // __domain_transform_fn, // __qq<__common_domain_t>>(); // } // else // { // return indeterminate_domain<>{}; // } // } // template // using __let_completion_domain_t = __unless_one_of_t< // __result_of<__let::__get_completion_domain<_LetTag, _SetTag, _Sndr, _Fun, _Env...>>, // indeterminate_domain<>>; // template // struct __attrs // { // using __set_tag_t = STDEXEC::__t<_LetTag>; // template // constexpr auto query(get_completion_scheduler_t<_Tag>) const = delete; // template // [[nodiscard]] // constexpr auto query(get_completion_domain_t<__set_tag_t>, _Env const &...) const noexcept // -> __ensure_valid_domain_t< // __let_completion_domain_t<_LetTag, __set_tag_t, _Sndr, _Fun, _Env...>> // { // return {}; // } // template <__one_of _Tag, class... _Env> // requires(__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value && ...) // [[nodiscard]] // constexpr auto query(get_completion_domain_t<_Tag>, _Env const &...) const noexcept // -> __ensure_valid_domain_t< // __common_domain_t<__completion_domain_of_t<_Tag, _Sndr, __fwd_env_t<_Env>...>, // __let_completion_domain_t<_LetTag, _Tag, _Sndr, _Fun, _Env...>>> // { // return {}; // } // template // requires(!__has_nothrow_completions_t<__set_tag_t, _Sndr, _Fun, _Env>::value) // [[nodiscard]] // constexpr auto query(get_completion_domain_t, _Env const &) const noexcept // -> __ensure_valid_domain_t< // __common_domain_t<__completion_domain_of_t<__set_tag_t, _Sndr, __fwd_env_t<_Env>>, // __completion_domain_of_t>, // __let_completion_domain_t<_LetTag, set_error_t, _Sndr, _Fun, _Env>>> // { // return {}; // } // template // [[nodiscard]] // constexpr auto query(__get_completion_behavior_t<__set_tag_t>, _Env const &...) const noexcept // { // if constexpr (sender_in<_Sndr, __fwd_env_t<_Env>...>) // { // // The completion behavior of let_value(sndr, fn) is the union of the completion // // behavior of sndr and all the senders that fn can potentially produce. (MSVC // // needs the constexpr computation broken up, hence the local variables.) // using __transform_fn = // __result_completion_behavior_fn<__set_tag_t, // _Fun, // __result_env_t<__set_tag_t, _Sndr, _Env>...>; // using __completions_t = __completion_signatures_of_t<_Sndr, __fwd_env_t<_Env>...>; // constexpr auto __pred_behavior = // STDEXEC::__get_completion_behavior<__set_tag_t, _Sndr, __fwd_env_t<_Env>...>(); // constexpr auto __result_behaviors = __gather_completions_t< // __set_tag_t, // __completions_t, // __transform_fn, // __mbind_front_q<__call_result_t, __completion_behavior::__common_t>>(); // return __pred_behavior | __result_behaviors; // } // else // { // return __completion_behavior::__unknown; // } // } // }; //! Implementation of the `let_*_t` types, where `_SetTag` is, e.g., //! `set_value_t` for `let_value`. template struct __let_t { template constexpr auto operator()(_Sender&& __sndr, _Fun __fn) const -> __well_formed_sender auto { return __sexpr{_LetTag(), static_cast<_Fun&&>(__fn), static_cast<_Sender&&>(__sndr)}; } template STDEXEC_ATTRIBUTE(always_inline) constexpr auto operator()(_Fun __fn) const { return __closure(*this, static_cast<_Fun&&>(__fn)); } private: friend _LetTag; __let_t() = default; }; template struct __impls : __sexpr_defaults { private: using __set_t = __t<_LetTag>; using __eptr_sig_t = set_error_t(std::exception_ptr); template using __fn_t = __decay_t<__data_of<_CvSender>>; template using __env2_t = __let::__result_env_t<__set_t, _CvSender, _Env>; template using __rcvr2_t = __receiver_archetype<__env2_t<_CvSender, _Env>>; template using __opstate_t = __gather_completions_of_t< __set_t, __child_of<_CvSender>, __fwd_env_t>, __q<__decayed_tuple>, __mbind_front_q<__opstate, __set_t, __child_of<_CvSender>, __fn_t<_CvSender>, _Receiver>>; template static consteval auto __get_transform_fn() { return []() { if constexpr (__decay_copyable<_Args...> && __invocable<_Fun, __decay_t<_Args>&...>) { using __sndr2_t = __invoke_result_t<_Fun, __decay_t<_Args>&...>; if constexpr (!__potentially_valid_sender_in<__sndr2_t, __env2_t<_Child, _Env>...>) { using __what_t = __bad_result_sender_t<__sndr2_t, _LetTag, __env2_t<_Child, _Env>...>; return STDEXEC::__throw_compile_time_error(__what_t()); } else if constexpr (__nothrow_decay_copyable<_Args...> && __nothrow_invocable<_Fun, __decay_t<_Args>&...> && sizeof...(_Env) == 0) { auto __completions = STDEXEC::get_completion_signatures<__sndr2_t, __env2_t<_Child, _Env>...>(); STDEXEC_IF_OK(__completions) { if constexpr (__completions.template __contains<__eptr_sig_t>()) { return __completions; } else { return STDEXEC::__throw_dependent_sender_error<__sndr2_t>(); } } } else if constexpr (__nothrow_decay_copyable<_Args...> && __nothrow_invocable<_Fun, __decay_t<_Args>&...> && (__nothrow_connectable<__sndr2_t, __rcvr2_t<_Child, _Env>> || ...)) { return STDEXEC::get_completion_signatures<__sndr2_t, __env2_t<_Child, _Env>...>(); } else { return STDEXEC::__concat_completion_signatures( STDEXEC::get_completion_signatures<__sndr2_t, __env2_t<_Child, _Env>...>(), __eptr_completion_t()); } } else if constexpr (__decay_copyable<_Args...>) { using __what_t = __callable_error_t<_LetTag, _Fun, __decay_t<_Args>&...>; return STDEXEC::__throw_compile_time_error(__what_t()); } else { using __what_t = __not_decay_copyable_error_t<_LetTag, _Args...>; return STDEXEC::__throw_compile_time_error(__what_t()); } }; } public: static constexpr auto __get_attrs = [](__ignore, __ignore, _Child const & __child) noexcept -> decltype(auto) { // TODO(ericniebler): this needs a proper implementation return __fwd_env(STDEXEC::get_env(__child)); }; template static consteval auto __get_completion_signatures() { static_assert(__sender_for<_CvSender, _LetTag>); using __child_t = __child_of<_CvSender>; auto __completions = STDEXEC::get_completion_signatures<__child_t, _Env...>(); STDEXEC_IF_OK(__completions) { auto __transform = __get_transform_fn<__fn_t<_CvSender>, __child_t, _Env...>(); if constexpr (!__decay_copyable<_CvSender>) { return STDEXEC::__throw_compile_time_error<_SENDER_TYPE_IS_NOT_DECAY_COPYABLE_, _WITH_SENDER_<_CvSender>>(); } else if constexpr (__t<_LetTag>() == STDEXEC::set_value) { return STDEXEC::__transform_completion_signatures(__completions, __transform); } else if constexpr (__t<_LetTag>() == STDEXEC::set_error) { return STDEXEC::__transform_completion_signatures(__completions, {}, __transform); } else { return STDEXEC::__transform_completion_signatures(__completions, {}, {}, __transform); } } } static constexpr auto __connect = [](_CvSender&& __sndr, _Receiver __rcvr) noexcept( __nothrow_constructible_from<__opstate_t<_CvSender, _Receiver>, __child_of<_CvSender>, __data_of<_CvSender>, _Receiver>) -> __opstate_t<_CvSender, _Receiver> { static_assert(__sender_for<_CvSender, _LetTag>); auto& [__tag, __fn, __child] = __sndr; return __opstate_t<_CvSender, _Receiver>(STDEXEC::__forward_like<_CvSender>(__child), STDEXEC::__forward_like<_CvSender>(__fn), static_cast<_Receiver&&>(__rcvr)); }; }; } // namespace __let //! @brief A pipeable sender adaptor that chains a *sender-returning* //! function onto a predecessor's value completion. //! //! @c let_value is the way to launch another asynchronous operation based on //! the values produced by a predecessor sender. Where @ref then_t "then" //! takes a function returning a *value*, @c let_value takes a function //! returning a *sender*, which is then connected and started, becoming the //! tail of the pipeline. //! //! Both call syntaxes are supported (the second is the *pipeable* form): //! //! @code{.cpp} //! auto s1 = stdexec::let_value(sndr, f); // direct invocation //! auto s2 = sndr | stdexec::let_value(f); // pipe syntax //! @endcode //! //! The signature of the operator overloads (inherited from a detail base) is: //! //! @code{.cpp} //! template //! auto operator()(Sender&& sndr, Fun fun) const -> sender auto; // direct //! //! template //! auto operator()(Fun fun) const; // closure //! @endcode //! //! The two forms are expression-equivalent. See [exec.let] in the //! C++26 working draft for the normative specification. //! //! **Completion signatures.** //! //! Given a predecessor sender @c sndr with completion signatures //! //! @code{.cpp} //! set_value_t(Vs...) // one or more value completions //! set_error_t(Es)... // forwarded unchanged //! set_stopped_t() // forwarded unchanged (if present) //! @endcode //! //! the sender produced by `let_value(sndr, f)` has completion //! signatures equal to the *union* of: //! //! - the completion signatures of every sender returned by an invocation //! `std::invoke(f, vs...)` for each value-completion argument pack //! @c (vs...) of @c sndr, *plus* //! - the @c set_error_t completions of @c sndr (forwarded unchanged), //! - the @c set_stopped_t completion of @c sndr (forwarded unchanged), and //! - @c set_error_t(std::exception_ptr) if invoking @c f or connecting its //! returned sender may throw. //! //! @c f must be invocable with every value-completion argument pack of //! @c sndr, and every such invocation must return a type satisfying the //! @c sender concept. Otherwise the program is ill-formed at the point //! where the resulting sender is connected to a receiver. //! //! **Use** @ref then_t "then" **vs.** @c let_value: choose @c then when the //! callable returns a *value*; choose @c let_value when the callable returns //! a *sender* (i.e. when the next step is itself asynchronous). Passing a //! sender-returning function to @c then would forward the sender as a //! value — almost never what you want. //! //! **Exception behavior.** //! //! If invoking @c f throws, or if connecting the sender returned by @c f //! throws, the exception is delivered through //! @c set_error_t(std::exception_ptr) on the resulting sender. When both //! steps are @c noexcept, no @c std::exception_ptr error completion is added. //! //! **Cancellation.** //! //! @c let_value does not introduce stop-token interaction of its own. If //! @c sndr completes with @c set_stopped, @c f is not invoked and the //! stopped completion is forwarded. //! //! **Example.** //! //! @code{.cpp} //! #include //! #include //! //! int main() { //! using namespace stdexec; //! //! auto fetch_async = [](int id) { //! return just(id * 10); // pretend this is a non-trivial async op //! }; //! //! auto sndr = just(7) //! | let_value(fetch_async); //! //! auto [v] = sync_wait(std::move(sndr)).value(); //! assert(v == 70); //! } //! @endcode //! //! @see stdexec::then — adapt the value channel with a //! value-returning function //! @see stdexec::let_error — adapt the error channel with a //! sender-returning function //! @see stdexec::let_stopped — adapt the stopped channel with a //! sender-returning function struct let_value_t : __let::__let_t { using __t = set_value_t; let_value_t() = default; }; //! @brief A pipeable sender adaptor that chains a *sender-returning* //! function onto a predecessor's error completion. //! //! @c let_error is to @ref upon_error_t "upon_error" what @ref let_value_t //! "let_value" is to @ref then_t "then": it lets the recovery step be //! *another asynchronous operation*. When the predecessor completes with //! @c set_error_t(e), @c f is invoked with @c e and is expected to return a //! sender; that sender is connected and started, and its completions become //! the completions of the overall pipeline. //! //! Both call syntaxes are supported (the second is the *pipeable* form): //! //! @code{.cpp} //! auto s1 = stdexec::let_error(sndr, f); // direct invocation //! auto s2 = sndr | stdexec::let_error(f); // pipe syntax //! @endcode //! //! See [exec.let] in the C++26 working draft for the normative specification. //! //! **Completion signatures.** //! //! Given a predecessor sender @c sndr with completion signatures //! //! @code{.cpp} //! set_value_t(Vs...) // forwarded unchanged //! set_error_t(Es)... // one or more error completions //! set_stopped_t() // forwarded unchanged (if present) //! @endcode //! //! the sender produced by `let_error(sndr, f)` has completion //! signatures equal to the *union* of: //! //! - the @c set_value_t completions of @c sndr (forwarded unchanged), //! - the completion signatures of every sender returned by an invocation //! `std::invoke(f, e)` for each error type @c e of @c sndr, //! - the @c set_stopped_t completion of @c sndr (forwarded unchanged), and //! - @c set_error_t(std::exception_ptr) if invoking @c f or connecting its //! returned sender may throw. //! //! All original @c set_error_t completions are *consumed*: only errors //! produced by the senders that @c f returns (or thrown by @c f itself) //! survive on the error channel. //! //! **Use** @ref upon_error_t "upon_error" **vs.** @c let_error: choose //! @c upon_error when the recovery is synchronous (returns a value); choose //! @c let_error when the recovery is itself asynchronous (returns a sender). //! //! **Exception behavior.** //! //! If invoking @c f throws, or if connecting the sender returned by @c f //! throws, the exception is delivered through //! @c set_error_t(std::exception_ptr) on the resulting sender. //! //! **Cancellation.** //! //! @c let_error does not introduce stop-token interaction of its own. A //! @c set_stopped completion of @c sndr is forwarded without invoking @c f. //! //! **Example.** //! //! @code{.cpp} //! #include //! #include //! #include //! //! int main() { //! using namespace stdexec; //! //! auto retry_async = [](std::error_code) { return just(7); }; //! //! auto sndr = just_error(std::error_code{ENOENT, std::system_category()}) //! | let_error(retry_async); //! //! auto [v] = sync_wait(std::move(sndr)).value(); //! assert(v == 7); //! } //! @endcode //! //! @see stdexec::upon_error — adapt the error channel with a //! value-returning function //! @see stdexec::let_value — adapt the value channel with a //! sender-returning function //! @see stdexec::let_stopped — adapt the stopped channel with a //! sender-returning function struct let_error_t : __let::__let_t { using __t = set_error_t; let_error_t() = default; }; //! @brief A pipeable sender adaptor that chains a *sender-returning* //! nullary function onto a predecessor's stopped completion. //! //! @c let_stopped is to @ref upon_stopped_t "upon_stopped" what //! @ref let_value_t "let_value" is to @ref then_t "then": it lets the //! recovery from cancellation be *another asynchronous operation*. When the //! predecessor completes with @c set_stopped, @c f is invoked with no //! arguments and is expected to return a sender; that sender is connected //! and started, and its completions become the completions of the overall //! pipeline. //! //! Both call syntaxes are supported (the second is the *pipeable* form): //! //! @code{.cpp} //! auto s1 = stdexec::let_stopped(sndr, f); // direct invocation //! auto s2 = sndr | stdexec::let_stopped(f); // pipe syntax //! @endcode //! //! See [exec.let] in the C++26 working draft for the normative specification. //! //! **Completion signatures.** //! //! Given a predecessor sender @c sndr with completion signatures //! //! @code{.cpp} //! set_value_t(Vs...) // forwarded unchanged //! set_error_t(Es)... // forwarded unchanged //! set_stopped_t() // (must be present) //! @endcode //! //! the sender produced by `let_stopped(sndr, f)` has completion //! signatures equal to the *union* of: //! //! - the @c set_value_t completions of @c sndr (forwarded unchanged), //! - the @c set_error_t completions of @c sndr (forwarded unchanged), //! - the completion signatures of the sender returned by //! `std::invoke(f)`, and //! - @c set_error_t(std::exception_ptr) if invoking @c f or connecting its //! returned sender may throw. //! //! The original @c set_stopped_t completion is *consumed*: it appears on //! the resulting sender only if the sender returned by @c f itself //! completes via @c set_stopped. //! //! **Use** @ref upon_stopped_t "upon_stopped" **vs.** @c let_stopped: choose //! @c upon_stopped when the recovery is synchronous (returns a value); //! choose @c let_stopped when the recovery is itself asynchronous //! (returns a sender). //! //! **Exception behavior.** //! //! If invoking @c f throws, or if connecting the sender returned by @c f //! throws, the exception is delivered through //! @c set_error_t(std::exception_ptr) on the resulting sender. //! //! **Cancellation.** //! //! @c let_stopped reacts to the predecessor's @c set_stopped; it does not //! initiate cancellation. The sender returned by @c f sees the receiver's //! stop token as normal. //! //! **Example.** //! //! @code{.cpp} //! #include //! #include //! //! int main() { //! using namespace stdexec; //! //! auto fallback_async = [] { return just(42); }; //! //! auto sndr = just_stopped() //! | let_stopped(fallback_async); //! //! auto [v] = sync_wait(std::move(sndr)).value(); //! assert(v == 42); //! } //! @endcode //! //! @see stdexec::upon_stopped — adapt the stopped channel with a //! value-returning function //! @see stdexec::let_value — adapt the value channel with a //! sender-returning function //! @see stdexec::let_error — adapt the error channel with a //! sender-returning function struct let_stopped_t : __let::__let_t { using __t = set_stopped_t; let_stopped_t() = default; }; //! @brief The customization point object for the @c let_value sender adaptor. //! //! @c let_value is an instance of @c let_value_t. See @ref let_value_t //! for the full description, completion-signature transformation rules, //! exception and cancellation behavior, and a usage example. //! //! @hideinitializer inline constexpr let_value_t let_value{}; //! @brief The customization point object for the @c let_error sender adaptor. //! //! @c let_error is an instance of @c let_error_t. See @ref let_error_t //! for the full description and example. //! //! @hideinitializer inline constexpr let_error_t let_error{}; //! @brief The customization point object for the @c let_stopped sender //! adaptor. //! //! @c let_stopped is an instance of @c let_stopped_t. See //! @ref let_stopped_t for the full description and example. //! //! @hideinitializer inline constexpr let_stopped_t let_stopped{}; template <> struct __sexpr_impl : __let::__impls {}; template <> struct __sexpr_impl : __let::__impls {}; template <> struct __sexpr_impl : __let::__impls {}; } // namespace STDEXEC # include "__epilogue.hpp" #endif // !STDEXEC_USE_MODULES() || defined(STDEXEC_IN_MODULE_PURVIEW)