[promises] poll_cast improvements (#35302)

Make it possible to `poll_cast<>` from a normal non-poll type to a Poll<T>

Closes #35302

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35302 from ctiller:poll-review 216916e7c3
PiperOrigin-RevId: 590979951
pull/35306/head^2
Craig Tiller 12 months ago committed by Copybara-Service
parent 3b1b2cabdc
commit 96ac691531
  1. 51
      src/core/lib/promise/poll.h

@ -186,19 +186,6 @@ class Poll<Pending>;
template <class T> template <class T>
class Poll<Poll<T>>; class Poll<Poll<T>>;
template <typename T>
bool operator==(const Poll<T>& a, const Poll<T>& b) {
if (a.pending() && b.pending()) return true;
if (a.ready() && b.ready()) return a.value() == b.value();
return false;
}
template <typename T, typename U>
Poll<T> poll_cast(Poll<U> poll) {
if (poll.pending()) return Pending{};
return static_cast<T>(std::move(poll.value()));
}
// PollTraits tells us whether a type is Poll<> or some other type, and is // PollTraits tells us whether a type is Poll<> or some other type, and is
// leveraged in the PromiseLike/PromiseFactory machinery to select the // leveraged in the PromiseLike/PromiseFactory machinery to select the
// appropriate implementation of those concepts based upon the return type of a // appropriate implementation of those concepts based upon the return type of a
@ -214,6 +201,44 @@ struct PollTraits<Poll<T>> {
static constexpr bool is_poll() { return true; } static constexpr bool is_poll() { return true; }
}; };
template <typename T>
bool operator==(const Poll<T>& a, const Poll<T>& b) {
if (a.pending() && b.pending()) return true;
if (a.ready() && b.ready()) return a.value() == b.value();
return false;
}
template <typename T, typename U, typename SfinaeVoid = void>
struct PollCastImpl;
template <typename T, typename U>
struct PollCastImpl<T, Poll<U>> {
static Poll<T> Cast(Poll<U>&& poll) {
if (poll.pending()) return Pending{};
return static_cast<T>(std::move(poll.value()));
}
};
template <typename T, typename U>
struct PollCastImpl<T, U, std::enable_if<!PollTraits<U>::is_poll()>> {
static Poll<T> Cast(U&& poll) { return Poll<T>(T(std::move(poll))); }
};
template <typename T>
struct PollCastImpl<T, T> {
static Poll<T> Cast(T&& poll) { return Poll<T>(std::move(poll)); }
};
template <typename T>
struct PollCastImpl<T, Poll<T>> {
static Poll<T> Cast(Poll<T>&& poll) { return std::move(poll); }
};
template <typename T, typename U>
Poll<T> poll_cast(U poll) {
return PollCastImpl<T, U>::Cast(std::move(poll));
}
// Convert a poll to a string // Convert a poll to a string
template <typename T, typename F> template <typename T, typename F>
std::string PollToString( std::string PollToString(

Loading…
Cancel
Save