From 764232fa13c0e22ae2a4a4a4d8631692c9408784 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Thu, 26 Aug 2021 14:34:00 -0700 Subject: [PATCH] Fixes for map (#27079) * fixes for map * fix bad merge * Automated change: Fix sanity tests * Automated change: Fix sanity tests Co-authored-by: ctiller --- build_autogenerated.yaml | 1 + src/core/lib/promise/detail/promise_factory.h | 9 +++----- src/core/lib/promise/detail/promise_like.h | 4 ++++ src/core/lib/promise/map.h | 23 +++++++++++++++++-- test/core/promise/map_test.cc | 6 +++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index f51c2b83a00..4ea31f35dd4 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -4224,6 +4224,7 @@ targets: - src/core/ext/upb-generated/google/rpc/status.upb.h - src/core/lib/gpr/alloc.h - src/core/lib/gpr/env.h + - src/core/lib/gpr/log_internal.h - src/core/lib/gpr/murmur_hash.h - src/core/lib/gpr/spinlock.h - src/core/lib/gpr/string.h diff --git a/src/core/lib/promise/detail/promise_factory.h b/src/core/lib/promise/detail/promise_factory.h index b6db4eb0f51..0b4d89a95f8 100644 --- a/src/core/lib/promise/detail/promise_factory.h +++ b/src/core/lib/promise/detail/promise_factory.h @@ -62,10 +62,6 @@ struct IsVoidCallable()())>> { static constexpr bool value = true; }; -// T -> T, const T& -> T -template -using RemoveCVRef = absl::remove_cv_t>; - // Given F(A,B,C,...), what's the return type? template struct ResultOfT; @@ -86,6 +82,7 @@ class Curried { public: Curried(F&& f, Arg&& arg) : f_(std::forward(f)), arg_(std::forward(arg)) {} + Curried(const F& f, Arg&& arg) : f_(f), arg_(std::forward(arg)) {} using Result = decltype(std::declval()(std::declval())); Result operator()() { return f_(arg_); } @@ -98,9 +95,9 @@ class Curried { // capturing A. template absl::enable_if_t>::value, - PromiseLike>> + PromiseLike, A>>> PromiseFactoryImpl(F&& f, A&& arg) { - return Curried(std::forward(f), std::forward(arg)); + return Curried, A>(std::forward(f), std::forward(arg)); } // Promote a callable() -> T|Poll to a PromiseFactory(A) -> Promise diff --git a/src/core/lib/promise/detail/promise_like.h b/src/core/lib/promise/detail/promise_like.h index eeaf768dbf3..57507141102 100644 --- a/src/core/lib/promise/detail/promise_like.h +++ b/src/core/lib/promise/detail/promise_like.h @@ -74,6 +74,10 @@ class PromiseLike { using Result = typename PollTraits::Type; }; +// T -> T, const T& -> T +template +using RemoveCVRef = absl::remove_cv_t>; + } // namespace promise_detail } // namespace grpc_core diff --git a/src/core/lib/promise/map.h b/src/core/lib/promise/map.h index a5b3e9e8377..aba0f259ecb 100644 --- a/src/core/lib/promise/map.h +++ b/src/core/lib/promise/map.h @@ -17,6 +17,8 @@ #include +#include + #include "absl/types/variant.h" #include "src/core/lib/promise/detail/promise_like.h" #include "src/core/lib/promise/poll.h" @@ -34,10 +36,12 @@ class Map { Map(Promise promise, Fn fn) : promise_(std::move(promise)), fn_(std::move(fn)) {} - using Result = typename PromiseLike::Result; + using PromiseResult = typename PromiseLike::Result; + using Result = + RemoveCVRef()(std::declval()))>; Poll operator()() { - Poll r = promise_(); + Poll r = promise_(); if (auto* p = absl::get_if(&r)) { return fn_(std::move(*p)); } @@ -59,6 +63,21 @@ promise_detail::Map Map(Promise promise, Fn fn) { return promise_detail::Map(std::move(promise), std::move(fn)); } +// Callable that takes a tuple and returns one element +template +struct JustElem { + template + auto operator()(std::tuple&& t) const + -> decltype(std::get(std::forward>(t))) { + return std::get(std::forward>(t)); + } + template + auto operator()(const std::tuple& t) const + -> decltype(std::get(t)) { + return std::get(t); + } +}; + } // namespace grpc_core #endif // GRPC_CORE_LIB_PROMISE_MAP_H diff --git a/test/core/promise/map_test.cc b/test/core/promise/map_test.cc index 0dfc50d6258..51bf047459e 100644 --- a/test/core/promise/map_test.cc +++ b/test/core/promise/map_test.cc @@ -23,6 +23,12 @@ TEST(MapTest, Works) { EXPECT_EQ(x(), Poll(21)); } +TEST(MapTest, JustElem) { + std::tuple t(1, 3.2); + EXPECT_EQ(JustElem<1>()(t), 3.2); + EXPECT_EQ(JustElem<0>()(t), 1); +} + } // namespace grpc_core int main(int argc, char** argv) {