[promises] Fix handling of `absl::Status` by `TrySeq` (#34162)

pull/34166/head
Craig Tiller 2 years ago committed by GitHub
parent eaa5784d4c
commit 6c86e79c95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/core/lib/promise/poll.h
  2. 6
      src/core/lib/promise/try_join.h
  3. 6
      test/core/promise/try_join_test.cc

@ -29,15 +29,13 @@ namespace grpc_core {
// A type that signals a Promise is still pending and not yet completed. // A type that signals a Promise is still pending and not yet completed.
// Allows writing 'return Pending{}' and with automatic conversions gets // Allows writing 'return Pending{}' and with automatic conversions gets
// upgraded to a Poll<> object. // upgraded to a Poll<> object.
struct Pending { struct Pending {};
constexpr bool operator==(Pending) const { return true; } inline bool operator==(const Pending&, const Pending&) { return true; }
};
// A type that contains no value. Useful for simulating 'void' in promises that // A type that contains no value. Useful for simulating 'void' in promises that
// always need to return some kind of value. // always need to return some kind of value.
struct Empty { struct Empty {};
constexpr bool operator==(Empty) const { return true; } inline bool operator==(const Empty&, const Empty&) { return true; }
};
// The result of polling a Promise once. // The result of polling a Promise once.
// //

@ -51,14 +51,20 @@ struct TryJoinTraits {
static bool IsOk(const absl::StatusOr<T>& x) { static bool IsOk(const absl::StatusOr<T>& x) {
return x.ok(); return x.ok();
} }
static bool IsOk(const absl::Status& x) { return x.ok(); }
template <typename T> template <typename T>
static T Unwrapped(absl::StatusOr<T> x) { static T Unwrapped(absl::StatusOr<T> x) {
return std::move(*x); return std::move(*x);
} }
static Empty Unwrapped(absl::Status) { return Empty{}; }
template <typename R, typename T> template <typename R, typename T>
static R EarlyReturn(absl::StatusOr<T> x) { static R EarlyReturn(absl::StatusOr<T> x) {
return x.status(); return x.status();
} }
template <typename R>
static R EarlyReturn(absl::Status x) {
return x;
}
}; };
// Implementation of TryJoin combinator. // Implementation of TryJoin combinator.

@ -76,6 +76,12 @@ TEST(TryJoinTest, Join2Fail2P) {
EXPECT_EQ(TryJoin(instant_fail<int>(), pending<int>())(), (fail<int, int>())); EXPECT_EQ(TryJoin(instant_fail<int>(), pending<int>())(), (fail<int, int>()));
} }
TEST(TryJoinTest, JoinStatus) {
EXPECT_EQ(TryJoin([]() { return absl::OkStatus(); },
[]() { return absl::OkStatus(); })(),
ok(Empty{}, Empty{}));
}
} // namespace grpc_core } // namespace grpc_core
int main(int argc, char** argv) { int main(int argc, char** argv) {

Loading…
Cancel
Save