Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 62 additions & 71 deletions include/exec/into_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,116 +15,107 @@
*/
#pragma once

#include "../stdexec/__detail/__basic_sender.hpp"
#include "../stdexec/__detail/__meta.hpp"
#include "../stdexec/execution.hpp"
#include "completion_signatures.hpp"

namespace experimental::execution
{
namespace __into_tuple
namespace _into_tuple
{
using namespace STDEXEC;

struct into_tuple_t;
struct _THE_INPUT_SENDER_MUST_HAVE_AT_EXACTLY_ONE_POSSIBLE_VALUE_COMPLETION_
{};
struct THE_INPUT_SENDER_MUST_HAVE_AT_EXACTLY_ONE_POSSIBLE_VALUE_COMPLETION;

template <class _Sender, class _Env>
using __too_many_completions_error =
__mexception<_WHAT_(_INVALID_ARGUMENT_),
_WHY_(_THE_INPUT_SENDER_MUST_HAVE_AT_EXACTLY_ONE_POSSIBLE_VALUE_COMPLETION_),
_WHERE_(_IN_ALGORITHM_, into_tuple_t),
_WITH_SENDER_<_Sender>,
_WITH_ENVIRONMENT_(_Env)>;

template <class _Sender, class... _Env>
using __try_result_tuple_t = __value_types_t<__completion_signatures_of_t<_Sender, _Env...>,
__q<__decayed_std_tuple>,
__q<__msingle>>;

template <class _Sender, class... _Env>
using __result_tuple_t =
__minvoke<__mtry_catch_q<__try_result_tuple_t, __q<__too_many_completions_error>>,
_Sender,
_Env...>;

template <class _Tuple>
using __tuple_completions_t =
STDEXEC::completion_signatures<set_error_t(std::exception_ptr), set_value_t(_Tuple)>;

template <class _Sender, class... _Env>
using __completions_t = __transform_completion_signatures_t<
__completion_signatures_of_t<_Sender, _Env...>,
__minvoke_q<__tuple_completions_t, __result_tuple_t<_Sender, _Env...>>,
__mconst<STDEXEC::completion_signatures<>>::__f>;

struct __into_tuple_impl : __sexpr_defaults
struct _impl : __sexpr_defaults
{
template <class _Receiver, class _Tuple>
template <class Receiver>
struct __state
{
using __tuple_t = _Tuple;
STDEXEC_IMMOVABLE_NO_UNIQUE_ADDRESS
_Receiver __rcvr_;
Receiver __rcvr_;
};

template <class _Sender, class... _Env>
static constexpr auto _value_cmplsigs = []<class... Args>() noexcept
{
constexpr bool __is_nothrow = __nothrow_decay_copyable<Args...>;
return exec::concat_completion_signatures(
STDEXEC::completion_signatures<set_value_t(__decayed_std_tuple<Args...>)>(),
__eptr_completion_unless_t<__mbool<__is_nothrow>>());
};

template <class Sender, class... Env>
static consteval auto __get_completion_signatures()
{
// TODO: port this to use constant evaluation
return __completions_t<__child_of<_Sender>, _Env...>{};
auto completions = STDEXEC::get_completion_signatures<__child_of<Sender>, Env...>();
STDEXEC_IF_OK(completions)
{
if constexpr (completions.__count(set_value) != 1)
{
return exec::throw_compile_time_error<
_WHAT_(_INVALID_ARGUMENT_),
_WHY_(THE_INPUT_SENDER_MUST_HAVE_AT_EXACTLY_ONE_POSSIBLE_VALUE_COMPLETION),
_WHERE_(_IN_ALGORITHM_, into_tuple_t),
_WITH_SENDER_<Sender>,
__fn_t<_WITH_ENVIRONMENT_, Env>...>();
}
else
{
return exec::transform_completion_signatures(completions, _value_cmplsigs);
}
}
}

static constexpr auto __get_state =
[]<class _Sender, class _Receiver>(_Sender &&, _Receiver &&__rcvr)
static constexpr auto __get_state = []<class Receiver>(__ignore, Receiver &&__rcvr) noexcept
{
using __tuple_t = __result_tuple_t<__child_of<_Sender>, env_of_t<_Receiver>>;
return __state<_Receiver, __tuple_t>{static_cast<_Receiver &&>(__rcvr)};
return __state<Receiver>{static_cast<Receiver &&>(__rcvr)};
};

static constexpr auto __complete =
[]<class _State, class _Tag, class... _Args>(__ignore,
_State &__state,
_Tag,
_Args &&...__args) noexcept -> void
[]<class State, class Tag, class... Args>(__ignore,
State &__state,
Tag,
Args &&...__args) noexcept -> void
{
if constexpr (__std::same_as<_Tag, set_value_t>)
if constexpr (Tag() == set_value)
{
using __tuple_t = _State::__tuple_t;
STDEXEC_TRY
{
set_value(static_cast<_State &&>(__state).__rcvr_,
__tuple_t{static_cast<_Args &&>(__args)...});
set_value(static_cast<State &&>(__state).__rcvr_,
__decayed_std_tuple<Args...>{static_cast<Args &&>(__args)...});
}
STDEXEC_CATCH_ALL
{
STDEXEC::set_error(static_cast<_State &&>(__state).__rcvr_, std::current_exception());
if constexpr (__nothrow_decay_copyable<Args...>)
__std::unreachable();
else
set_error(static_cast<State &&>(__state).__rcvr_, std::current_exception());
}
}
else
{
_Tag()(static_cast<_State &&>(__state).__rcvr_, static_cast<_Args &&>(__args)...);
Tag()(static_cast<State &&>(__state).__rcvr_, static_cast<Args &&>(__args)...);
}
};
};
} // namespace _into_tuple

struct into_tuple_t
struct into_tuple_t
{
template <STDEXEC::sender Sender>
constexpr auto operator()(Sender &&__sndr) const //
noexcept(STDEXEC::__nothrow_decay_copyable<Sender>)
{
template <sender _Sender>
constexpr auto operator()(_Sender &&__sndr) const
{
return __sexpr{into_tuple_t(), __(), static_cast<_Sender &&>(__sndr)};
}
using namespace STDEXEC;
return __sexpr{into_tuple_t(), __(), static_cast<Sender &&>(__sndr)};
}

STDEXEC_ATTRIBUTE(always_inline)
constexpr auto operator()() const noexcept
{
return __closure(*this);
}
};
} // namespace __into_tuple
STDEXEC_ATTRIBUTE(always_inline)
constexpr auto operator()() const noexcept
{
return STDEXEC::__closure(*this);
}
};

using __into_tuple::into_tuple_t;
inline constexpr into_tuple_t into_tuple{};
} // namespace experimental::execution

Expand All @@ -133,6 +124,6 @@ namespace exec = experimental::execution;
namespace STDEXEC
{
template <>
struct __sexpr_impl<exec::__into_tuple::into_tuple_t> : exec::__into_tuple::__into_tuple_impl
struct __sexpr_impl<exec::into_tuple_t> : exec::_into_tuple::_impl
{};
} // namespace STDEXEC
25 changes: 17 additions & 8 deletions test/exec/test_into_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ using namespace STDEXEC;

namespace
{
struct throwing_move_only
{
throwing_move_only() = default;
throwing_move_only(throwing_move_only &&) {}
};

TEST_CASE("into_tuple is a sender", "[adaptors][into_tuple]")
{
Expand All @@ -35,17 +40,21 @@ namespace
{
using void_sender = result_of_t<exec::into_tuple, a_sender_of<set_value_t()>>;
STATIC_REQUIRE(sender_in<void_sender, ex::env<>>);
STATIC_REQUIRE(
set_equivalent<
completion_signatures_of_t<void_sender, ex::env<>>,
completion_signatures<set_error_t(std::exception_ptr), set_value_t(std::tuple<>)>>);
STATIC_REQUIRE(set_equivalent<completion_signatures_of_t<void_sender, ex::env<>>,
completion_signatures<set_value_t(std::tuple<>)>>);

using ints_sender = result_of_t<exec::into_tuple, a_sender_of<set_value_t(int &, int)>>;
STATIC_REQUIRE(sender_in<ints_sender, ex::env<>>);
STATIC_REQUIRE(set_equivalent<completion_signatures_of_t<ints_sender, ex::env<>>,
completion_signatures<set_value_t(std::tuple<int, int>)>>);

using ints_sender = result_of_t<exec::into_tuple, a_sender_of<set_value_t(int&, int)>>;
using throwing_move_only_sender =
result_of_t<exec::into_tuple, a_sender_of<set_value_t(throwing_move_only)>>;
STATIC_REQUIRE(sender_in<ints_sender, ex::env<>>);
STATIC_REQUIRE(
set_equivalent<
completion_signatures_of_t<ints_sender, ex::env<>>,
completion_signatures<set_error_t(std::exception_ptr), set_value_t(std::tuple<int, int>)>>);
set_equivalent<completion_signatures_of_t<throwing_move_only_sender, ex::env<>>,
completion_signatures<set_error_t(std::exception_ptr),
set_value_t(std::tuple<throwing_move_only>)>>);
}

TEST_CASE("into_tuple rejects senders unless they have a single value completion",
Expand Down
Loading