promise/promise.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_PROMISE_H
16 #define GRPC_CORE_LIB_PROMISE_PROMISE_H
17 
19 
20 #include <functional>
21 #include <type_traits>
22 #include <utility>
23 
24 #include "absl/status/status.h"
25 #include "absl/types/optional.h"
26 #include "absl/types/variant.h"
27 
30 
31 namespace grpc_core {
32 
33 // A Promise is any functor that takes no arguments and returns Poll<T>.
34 // Most of the time we just pass around the functor, but occasionally
35 // it pays to have a type erased variant, which we define here.
36 template <typename T>
37 using Promise = std::function<Poll<T>()>;
38 
39 // Helper to execute a promise immediately and return either the result or
40 // nothing.
41 template <typename Promise>
42 auto NowOrNever(Promise promise)
45  if (auto* p = absl::get_if<kPollReadyIdx>(&r)) {
46  return std::move(*p);
47  }
48  return {};
49 }
50 
51 // A promise that never completes.
52 template <typename T>
53 struct Never {
54  Poll<T> operator()() { return Pending(); }
55 };
56 
57 namespace promise_detail {
58 // A promise that immediately completes.
59 template <typename T>
60 class Immediate {
61  public:
62  explicit Immediate(T value) : value_(std::move(value)) {}
63 
65 
66  private:
68 };
69 } // namespace promise_detail
70 
71 // Return \a value immediately
72 template <typename T>
75 }
76 
77 // Return status Ok immediately
80 };
81 
82 // Typecheck that a promise returns the expected return type.
83 // usage: auto promise = WithResult<int>([]() { return 3; });
84 // NOTE: there are tests in promise_test.cc that are commented out because they
85 // should fail to compile. When modifying this code these should be uncommented
86 // and their miscompilation verified.
87 template <typename T, typename F>
88 auto WithResult(F f) ->
89  typename std::enable_if<std::is_same<decltype(f()), Poll<T>>::value,
90  F>::type {
91  return f;
92 }
93 
94 } // namespace grpc_core
95 
96 #endif // GRPC_CORE_LIB_PROMISE_PROMISE_H
grpc_core::promise_detail::Immediate
Definition: promise/promise.h:60
grpc_core::promise_detail::Immediate::value_
T value_
Definition: promise/promise.h:67
grpc_core::Never
Definition: promise/promise.h:53
grpc_core
Definition: call_metric_recorder.h:31
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::NowOrNever
auto NowOrNever(Promise promise) -> absl::optional< typename promise_detail::PromiseLike< Promise >::Result >
Definition: promise/promise.h:42
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::Pending
Definition: poll.h:29
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::promise_detail::Immediate::Immediate
Immediate(T value)
Definition: promise/promise.h:62
grpc_core::Never::operator()
Poll< T > operator()()
Definition: promise/promise.h:54
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
grpc_core::promise_detail::Immediate::operator()
Poll< T > operator()()
Definition: promise/promise.h:64
F
#define F(b, c, d)
Definition: md4.c:112
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::ImmediateOkStatus
Definition: promise/promise.h:78
grpc_core::WithResult
auto WithResult(F f) -> typename std::enable_if< std::is_same< decltype(f()), Poll< T >>::value, F >::type
Definition: promise/promise.h:88
promise_like.h
poll.h
grpc_core::Promise
std::function< Poll< T >()> Promise
Definition: promise/promise.h:37
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::Immediate
promise_detail::Immediate< T > Immediate(T value)
Definition: promise/promise.h:73
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::promise_detail::PromiseLike< Promise >
port_platform.h
grpc_core::ImmediateOkStatus::operator()
Poll< absl::Status > operator()()
Definition: promise/promise.h:79


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