Skip to content

Commit

Permalink
let coro::get_awaiter be a function-object and evade ADL
Browse files Browse the repository at this point in the history
Reviewed By: cjhawley

Differential Revision: D69710057

fbshipit-source-id: 5e8d92d41f36e62ee90eec15d6b283c5bbbd010b
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Feb 17, 2025
1 parent 58b6c5a commit 513e1b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
69 changes: 36 additions & 33 deletions folly/coro/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,42 @@ constexpr bool is_awaitable_v = is_awaitable<T>::value;
/// for that type.
///
/// This encapsulates calling 'operator co_await()' if it exists.
template <
typename Awaitable,
std::enable_if_t<
folly::Conjunction<
is_awaiter<Awaitable>,
folly::Negation<detail::_has_free_operator_co_await<Awaitable>>,
folly::Negation<detail::_has_member_operator_co_await<Awaitable>>>::
value,
int> = 0>
Awaitable& get_awaiter(Awaitable&& awaitable) {
return static_cast<Awaitable&>(awaitable);
}

template <
typename Awaitable,
std::enable_if_t<
detail::_has_member_operator_co_await<Awaitable>::value,
int> = 0>
decltype(auto) get_awaiter(Awaitable&& awaitable) {
return static_cast<Awaitable&&>(awaitable).operator co_await();
}

template <
typename Awaitable,
std::enable_if_t<
folly::Conjunction<
detail::_has_free_operator_co_await<Awaitable>,
folly::Negation<detail::_has_member_operator_co_await<Awaitable>>>::
value,
int> = 0>
decltype(auto) get_awaiter(Awaitable&& awaitable) {
return operator co_await(static_cast<Awaitable&&>(awaitable));
}
struct get_awaiter_fn {
template <
typename Awaitable,
std::enable_if_t<
folly::Conjunction<
is_awaiter<Awaitable>,
folly::Negation<detail::_has_free_operator_co_await<Awaitable>>,
folly::Negation<
detail::_has_member_operator_co_await<Awaitable>>>::value,
int> = 0>
Awaitable& operator()(Awaitable&& awaitable) const {
return static_cast<Awaitable&>(awaitable);
}

template <
typename Awaitable,
std::enable_if_t<
detail::_has_member_operator_co_await<Awaitable>::value,
int> = 0>
decltype(auto) operator()(Awaitable&& awaitable) const {
return static_cast<Awaitable&&>(awaitable).operator co_await();
}

template <
typename Awaitable,
std::enable_if_t<
folly::Conjunction<
detail::_has_free_operator_co_await<Awaitable>,
folly::Negation<
detail::_has_member_operator_co_await<Awaitable>>>::value,
int> = 0>
decltype(auto) operator()(Awaitable&& awaitable) const {
return operator co_await(static_cast<Awaitable&&>(awaitable));
}
};
constexpr inline get_awaiter_fn get_awaiter{};

/// awaiter_type<Awaitable>
///
Expand Down
9 changes: 4 additions & 5 deletions folly/coro/ViaIfAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ class StackAwareViaIfAsyncAwaiter {
: viaCoroutine_(CoroutineType::create(std::move(executor))),
awaitable_(folly::coro::co_withAsyncStack(
static_cast<Awaitable&&>(awaitable))),
awaiter_(folly::coro::get_awaiter(
static_cast<WithAsyncStackAwaitable&&>(awaitable_))) {}
awaiter_(
get_awaiter(static_cast<WithAsyncStackAwaitable&&>(awaitable_))) {}

decltype(auto) await_ready() noexcept(noexcept(awaiter_.await_ready())) {
return awaiter_.await_ready();
Expand Down Expand Up @@ -301,8 +301,7 @@ class ViaIfAsyncAwaiter {
explicit ViaIfAsyncAwaiter(
folly::Executor::KeepAlive<> executor, Awaitable&& awaitable)
: viaCoroutine_(CoroutineType::create(std::move(executor))),
awaiter_(
folly::coro::get_awaiter(static_cast<Awaitable&&>(awaitable))) {}
awaiter_(get_awaiter(static_cast<Awaitable&&>(awaitable))) {}

decltype(auto) await_ready() noexcept(noexcept(awaiter_.await_ready())) {
return awaiter_.await_ready();
Expand Down Expand Up @@ -581,7 +580,7 @@ class TryAwaiter {

public:
explicit TryAwaiter(Awaitable&& awaiter)
: awaiter_(folly::coro::get_awaiter(static_cast<Awaitable&&>(awaiter))) {}
: awaiter_(get_awaiter(static_cast<Awaitable&&>(awaiter))) {}

auto await_ready() noexcept(noexcept(std::declval<Awaiter&>().await_ready()))
-> decltype(std::declval<Awaiter&>().await_ready()) {
Expand Down
2 changes: 1 addition & 1 deletion folly/coro/WithAsyncStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class WithAsyncStackAwaiter {

public:
explicit WithAsyncStackAwaiter(Awaitable&& awaitable)
: awaiter_(folly::coro::get_awaiter(static_cast<Awaitable&&>(awaitable))),
: awaiter_(get_awaiter(static_cast<Awaitable&&>(awaitable))),
coroWrapper_(WithAsyncStackCoroutine::create()) {}

auto await_ready() noexcept(noexcept(std::declval<Awaiter&>().await_ready()))
Expand Down

0 comments on commit 513e1b9

Please sign in to comment.