promise_like.h
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H
16 #define GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H
17 
19 
20 #include <utility>
21 
22 #include "absl/meta/type_traits.h"
23 
25 
26 // A Promise is a callable object that returns Poll<T> for some T.
27 // Often when we're writing code that uses promises, we end up wanting to also
28 // deal with code that completes instantaneously - that is, it returns some T
29 // where T is not Poll.
30 // PromiseLike wraps any callable that takes no parameters and implements the
31 // Promise interface. For things that already return Poll, this wrapping does
32 // nothing. For things that do not return Poll, we wrap the return type in Poll.
33 // This allows us to write things like:
34 // Seq(
35 // [] { return 42; },
36 // ...)
37 // in preference to things like:
38 // Seq(
39 // [] { return Poll<int>(42); },
40 // ...)
41 // or:
42 // Seq(
43 // [] -> Poll<int> { return 42; },
44 // ...)
45 // leading to slightly more concise code and eliminating some rules that in
46 // practice people find hard to deal with.
47 
48 namespace grpc_core {
49 namespace promise_detail {
50 
51 template <typename T>
52 struct PollWrapper {
53  static Poll<T> Wrap(T&& x) { return Poll<T>(std::forward<T>(x)); }
54 };
55 
56 template <typename T>
57 struct PollWrapper<Poll<T>> {
58  static Poll<T> Wrap(Poll<T>&& x) { return std::forward<Poll<T>>(x); }
59 };
60 
61 template <typename T>
62 auto WrapInPoll(T&& x) -> decltype(PollWrapper<T>::Wrap(std::forward<T>(x))) {
63  return PollWrapper<T>::Wrap(std::forward<T>(x));
64 }
65 
66 template <typename F>
67 class PromiseLike {
68  private:
70 
71  public:
72  // NOLINTNEXTLINE - internal detail that drastically simplifies calling code.
73  PromiseLike(F&& f) : f_(std::forward<F>(f)) {}
74  auto operator()() -> decltype(WrapInPoll(f_())) { return WrapInPoll(f_()); }
75  using Result = typename PollTraits<decltype(WrapInPoll(f_()))>::Type;
76 };
77 
78 // T -> T, const T& -> T
79 template <typename T>
81 
82 } // namespace promise_detail
83 } // namespace grpc_core
84 
85 #endif // GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
absl::remove_cv_t
typename std::remove_cv< T >::type remove_cv_t
Definition: abseil-cpp/absl/meta/type_traits.h:579
grpc_core::promise_detail::WrapInPoll
auto WrapInPoll(T &&x) -> decltype(PollWrapper< T >::Wrap(std::forward< T >(x)))
Definition: promise_like.h:62
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::promise_detail::PromiseLike::f_
GPR_NO_UNIQUE_ADDRESS F f_
Definition: promise_like.h:69
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::promise_detail::PromiseLike::PromiseLike
PromiseLike(F &&f)
Definition: promise_like.h:73
grpc_core::PollTraits
Definition: poll.h:54
grpc_core::promise_detail::PollWrapper< Poll< T > >::Wrap
static Poll< T > Wrap(Poll< T > &&x)
Definition: promise_like.h:58
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
GPR_NO_UNIQUE_ADDRESS
#define GPR_NO_UNIQUE_ADDRESS
Definition: impl/codegen/port_platform.h:692
F
#define F(b, c, d)
Definition: md4.c:112
grpc_core::promise_detail::PromiseLike< Promise >::Result
typename PollTraits< decltype(WrapInPoll(f_()))>::Type Result
Definition: promise_like.h:75
grpc_core::promise_detail::PollWrapper::Wrap
static Poll< T > Wrap(T &&x)
Definition: promise_like.h:53
poll.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::promise_detail::PromiseLike::operator()
auto operator()() -> decltype(WrapInPoll(f_()))
Definition: promise_like.h:74
grpc_core::promise_detail::RemoveCVRef
absl::remove_cv_t< absl::remove_reference_t< T > > RemoveCVRef
Definition: promise_like.h:80
absl::forward
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:230
grpc_core::promise_detail::PollWrapper
Definition: promise_like.h:52
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::promise_detail::PromiseLike
Definition: promise_like.h:67
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:55