Debian Codegraph @

src/core/lib/promise/try_seq.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_TRY_SEQ_H
16 #define GRPC_CORE_LIB_PROMISE_TRY_SEQ_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <utility>
21
22 #include "absl/meta/type_traits.h"
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
25
26 #include "src/core/lib/promise/detail/basic_seq.h"
27 #include "src/core/lib/promise/detail/promise_like.h"
28 #include "src/core/lib/promise/detail/status.h"
29 #include "src/core/lib/promise/poll.h"
30
31 namespace grpc_core {
32
33 namespace promise_detail {
34
35 template <typename T, typename Ignored = void>
36 struct TrySeqTraitsWithSfinae {
37 using UnwrappedType = T;
38 using WrappedType = absl::StatusOr<T>;
39 template <typename Next>
40 static auto CallFactory(Next* next, T&& value) {
41 return next->Make(std::forward<T>(value));
42 }
43 template <typename F, typename Elem>
44 static auto CallSeqFactory(F& f, Elem&& elem, T&& value)
45 -> decltype(f(std::forward<Elem>(elem), std::forward<T>(value))) {
46 return f(std::forward<Elem>(elem), std::forward<T>(value));
47 }
48 template <typename Result, typename RunNext>
49 static Poll<Result> CheckResultAndRunNext(T prior, RunNext run_next) {
50 return run_next(std::move(prior));
51 }
52 };
53
54 template <typename T>
55 struct TrySeqTraitsWithSfinae<absl::StatusOr<T>> {
56 using UnwrappedType = T;
57 using WrappedType = absl::StatusOr<T>;
58 template <typename Next>
59 static auto CallFactory(Next* next, absl::StatusOr<T>&& status) {
60 return next->Make(std::move(*status));
61 }
62 template <typename F, typename Elem>
63 static auto CallSeqFactory(F& f, Elem&& elem, absl::StatusOr<T> value)
64 -> decltype(f(std::forward<Elem>(elem), std::move(*value))) {
65 return f(std::forward<Elem>(elem), std::move(*value));
66 }
67 template <typename Result, typename RunNext>
68 static Poll<Result> CheckResultAndRunNext(absl::StatusOr<T> prior,
69 RunNext run_next) {
70 if (!prior.ok()) return StatusCast<Result>(prior.status());
71 return run_next(std::move(prior));
72 }
73 };
74 // If there exists a function 'IsStatusOk(const T&) -> bool' then we assume that
75 // T is a status type for the purposes of promise sequences, and a non-OK T
76 // should terminate the sequence and return.
77 template <typename T>
78 struct TrySeqTraitsWithSfinae<
79 T, absl::enable_if_t<
80 std::is_same<decltype(IsStatusOk(std::declval<T>())), bool>::value,
81 void>> {
82 using UnwrappedType = void;
83 using WrappedType = T;
84 template <typename Next>
85 static auto CallFactory(Next* next, T&&) {
86 return next->Make();
87 }
88 template <typename Result, typename RunNext>
89 static Poll<Result> CheckResultAndRunNext(T prior, RunNext run_next) {
90 if (!IsStatusOk(prior)) return Result(std::move(prior));
91 return run_next(std::move(prior));
92 }
93 };
94 template <>
95 struct TrySeqTraitsWithSfinae<absl::Status> {
96 using UnwrappedType = void;
97 using WrappedType = absl::Status;
98 template <typename Next>
99 static auto CallFactory(Next* next, absl::Status&&) {
100 return next->Make();
101 }
102 template <typename Result, typename RunNext>
103 static Poll<Result> CheckResultAndRunNext(absl::Status prior,
104 RunNext run_next) {
105 if (!prior.ok()) return StatusCast<Result>(std::move(prior));
106 return run_next(std::move(prior));
107 }
108 };
109
110 template <typename T>
111 using TrySeqTraits = TrySeqTraitsWithSfinae<T>;
112
113 template <typename... Fs>
114 using TrySeq = BasicSeq<TrySeqTraits, Fs...>;
115
116 template <typename I, typename F, typename Arg>
117 struct TrySeqIterTraits {
118 using Iter = I;
119 using Factory = F;
120 using Argument = Arg;
121 using IterValue = decltype(*std::declval<Iter>());
122 using StateCreated = decltype(std::declval<F>()(std::declval<IterValue>(),
123 std::declval<Arg>()));
124 using State = PromiseLike<StateCreated>;
125 using Wrapped = typename State::Result;
126
127 using Traits = TrySeqTraits<Wrapped>;
128 };
129
130 template <typename Iter, typename Factory, typename Argument>
131 struct TrySeqIterResultTraits {
132 using IterTraits = TrySeqIterTraits<Iter, Factory, Argument>;
133 using Result = BasicSeqIter<IterTraits>;
134 };
135
136 } // namespace promise_detail
137
138 // Try a sequence of operations.
139 // * Run the first functor as a promise.
140 // * Feed its success result into the second functor to create a promise,
141 // then run that.
142 // * ...
143 // * Feed the second-final success result into the final functor to create a
144 // promise, then run that, with the overall success result being that
145 // promises success result.
146 // If any step fails, fail everything.
147 // Functors can return StatusOr<> to signal that a value is fed forward, or
148 // Status to indicate only success/failure. In the case of returning Status,
149 // the construction functors take no arguments.
150 template <typename... Functors>
151 promise_detail::TrySeq<Functors...> TrySeq(Functors... functors) {
152 return promise_detail::TrySeq<Functors...>(std::move(functors)...);
153 }
154
155 // Try a sequence of operations of unknown length.
156 // Asynchronously:
157 // for (element in (begin, end)) {
158 // auto r = wait_for factory(element, argument);
159 // if (!r.ok()) return r;
160 // argument = *r;
161 // }
162 // return argument;
163 template <typename Iter, typename Factory, typename Argument>
164 typename promise_detail::TrySeqIterResultTraits<Iter, Factory, Argument>::Result
165 TrySeqIter(Iter begin, Iter end, Argument argument, Factory factory) {
166 using Result =
167 typename promise_detail::TrySeqIterResultTraits<Iter, Factory,
168 Argument>::Result;
169 return Result(begin, end, std::move(factory), std::move(argument));
170 }
171
172 } // namespace grpc_core
173
174 #endif // GRPC_CORE_LIB_PROMISE_TRY_SEQ_H