Skip to content

Commit a0c1de1

Browse files
snarkmasterfacebook-github-bot
authored andcommitted
Add makeNowTask / makeErrorNowTask
Summary: Analog of `folly::coro::makeTask`. The typical use-case is a "coroutine wrapper", i.e. a non-coro function that returns a trivial coroutine on some branches, and a potentially-suspending coro on other branches. The typical motivation for such wrappers (over just doing `co_return co_await subTask()`) is to guarantee that there's no coro frame overhead for the wrapper. Reviewed By: ispeters Differential Revision: D77400395 fbshipit-source-id: 5a2e5e81c439faf1f9f4d9d1d9dc02907d9b34c3
1 parent 62a695d commit a0c1de1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

third-party/folly/src/folly/coro/AsyncGenerator.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,8 @@ class AsyncGeneratorPromise final
695695
state_ = State::DONE;
696696
}
697697

698-
// FIXME: Much of this class is currenrly copy-pasted from `TaskPromiseBase`,
699-
// and should be refactored to be use that, so as to avoid divergent behavior
700-
// of `co_await`.
698+
// FIXME: Much of this class is currently copy-pasted from `TaskPromiseBase`,
699+
// Refactor this to use that, so as to avoid `co_await` behavior divergence.
701700

702701
template <
703702
typename Awaitable,

third-party/folly/src/folly/coro/safe/NowTask.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ using NowTaskBase =
9797
template <safe_alias, typename>
9898
class SafeTask;
9999

100+
template <safe_alias S, typename U>
101+
auto toNowTask(SafeTask<S, U>);
102+
100103
template <typename T>
101104
class FOLLY_CORO_TASK_ATTRS NowTask final : public detail::NowTaskBase<T> {
102105
protected:
@@ -120,6 +123,33 @@ auto toNowTask(NowTask<T> t) {
120123
return NowTask<T>{std::move(t).unwrapTask()};
121124
}
122125

126+
// Apparently, Clang 15 has a bug in prvalue semantics support, so it cannot
127+
// return immovable coroutines.
128+
#if !defined(__clang__) || __clang_major__ > 15
129+
130+
/// Make a `NowTask` that trivially returns a value.
131+
template <class T>
132+
NowTask<T> makeNowTask(T t) {
133+
co_return t;
134+
}
135+
136+
/// Make a `NowTask` that trivially returns no value
137+
inline NowTask<> makeNowTask() {
138+
co_return;
139+
}
140+
/// Same as makeNowTask(). See Unit
141+
inline NowTask<> makeNowTask(Unit) {
142+
co_return;
143+
}
144+
145+
/// Make a `NowTask` that will trivially yield an exception.
146+
template <class T>
147+
NowTask<T> makeErrorNowTask(exception_wrapper ew) {
148+
co_yield co_error(std::move(ew));
149+
}
150+
151+
#endif // no `makeNowTask` on old/buggy clang
152+
123153
} // namespace folly::coro
124154

125155
template <typename T>

0 commit comments

Comments
 (0)