Debian Codegraph @

src/core/lib/promise/promise.h

raw
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
18 #include <grpc/support/port_platform.h>
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
28 #include "src/core/lib/promise/detail/promise_like.h"
29 #include "src/core/lib/promise/poll.h"
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)
43 -> absl::optional<typename promise_detail::PromiseLike<Promise>::Result> {
44 auto r = promise_detail::PromiseLike<Promise>(std::move(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
64 Poll<T> operator()() { return std::move(value_); }
65
66 private:
67 T value_;
68 };
69 } // namespace promise_detail
70
71 // Return \a value immediately
72 template <typename T>
73 promise_detail::Immediate<T> Immediate(T value) {
74 return promise_detail::Immediate<T>(std::move(value));
75 }
76
77 // Return status Ok immediately
78 struct ImmediateOkStatus {
79 Poll<absl::Status> operator()() { return absl::OkStatus(); }
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