Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
ctiller committed Feb 17, 2025
1 parent 9a641df commit 6bf65e1
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/core/lib/promise/detail/promise_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,39 @@ PromiseFactoryImpl(OnceToken, F f, A&&) {
return PromiseLike<F>(std::move(f));
}

// Promote a callable() -> T|Poll<T> to a PromiseFactory() -> Promise<T>
// Promote a callable() -> Poll<T> to a PromiseFactory() -> Promise<T>
template <typename F>
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline absl::enable_if_t<
!IsVoidCallable<ResultOf<F()>>::value, PromiseLike<RemoveCVRef<F>>>
!IsVoidCallable<ResultOf<F()>>::value &&
PollTraits<ResultOf<F()>>::is_poll(),
PromiseLike<RemoveCVRef<F>>>
PromiseFactoryImpl(OnceToken, F f) {
return PromiseLike<F>(std::move(f));
}

// Promote a callable() -> T to a PromiseFactory() -> Immediate<T>
template <typename F>
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline auto PromiseFactoryImpl(
std::enable_if_t<!IsVoidCallable<ResultOf<F()>>::value &&
!PollTraits<ResultOf<F()>>::is_poll() &&
!std::is_same_v<ResultOf<F()>, void>,
OnceToken>,
F f) {
auto f2 = [x = f()]() mutable { return std::move(x); };
return PromiseLike<decltype(f2)>(std::move(f2));
}
template <typename F>
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline auto PromiseFactoryImpl(
std::enable_if_t<!IsVoidCallable<ResultOf<F()>>::value &&
!PollTraits<ResultOf<F()>>::is_poll() &&
std::is_same_v<ResultOf<F()>, void>,
OnceToken>,
F f) {
f();
auto f2 = []() { return Empty{}; };
return PromiseLike<decltype(f2)>(std::move(f2));
}

// Given a callable(A) -> Promise<T>, name it a PromiseFactory and use it.
template <typename Token, typename A, typename F>
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline absl::enable_if_t<
Expand Down

0 comments on commit 6bf65e1

Please sign in to comment.