Commit 2044c0c6 authored by Shai Szulanski's avatar Shai Szulanski Committed by Facebook GitHub Bot

Drop unit in folly::coro::toTask

Summary:
To preserve void type in roundtrip
Doing this generically is pretty gross (you need to check if awaiter_type<decltype(co_viaIfAsync({}, SemiAwaitable))> has a member await_resume_try), while the cost of adding explicit overloads is low as there are only two types we care about, so I chose to do it that way.

Reviewed By: yfeldblum

Differential Revision: D23407270

fbshipit-source-id: cc952e2d8fa4297dbe363e22e99192fdf858134f
parent 659deb38
...@@ -40,6 +40,16 @@ Task<semi_await_result_t<SemiAwaitable>> toTask( ...@@ -40,6 +40,16 @@ Task<semi_await_result_t<SemiAwaitable>> toTask(
co_return co_await a.get(); co_return co_await a.get();
}); });
} }
inline Task<void> toTask(Future<Unit> a) {
return co_invoke([a = std::move(a)]() mutable -> Task<void> {
co_yield co_result(co_await co_awaitTry(std::move(a)));
});
}
inline Task<void> toTask(SemiFuture<Unit> a) {
return co_invoke([a = std::move(a)]() mutable -> Task<void> {
co_yield co_result(co_await co_awaitTry(std::move(a)));
});
}
// Converts the given SemiAwaitable to a SemiFuture (without starting it) // Converts the given SemiAwaitable to a SemiFuture (without starting it)
template <typename SemiAwaitable> template <typename SemiAwaitable>
......
...@@ -31,6 +31,9 @@ static folly::coro::Task<int> makeTask() { ...@@ -31,6 +31,9 @@ static folly::coro::Task<int> makeTask() {
static folly::coro::AsyncGenerator<int> makeGen() { static folly::coro::AsyncGenerator<int> makeGen() {
co_yield 42; co_yield 42;
} }
static folly::coro::Task<void> makeVoidTask() {
co_return;
}
TEST(FutureUtilTest, ToTask) { TEST(FutureUtilTest, ToTask) {
EXPECT_EQ(folly::coro::blockingWait(folly::coro::toTask(makeTask())), 42); EXPECT_EQ(folly::coro::blockingWait(folly::coro::toTask(makeTask())), 42);
...@@ -101,4 +104,12 @@ TEST(FutureUtilTest, ToFuture) { ...@@ -101,4 +104,12 @@ TEST(FutureUtilTest, ToFuture) {
ex.drain(); ex.drain();
EXPECT_TRUE(fut3.isReady()); EXPECT_TRUE(fut3.isReady());
} }
TEST(FutureUtilTest, VoidRoundtrip) {
folly::coro::Task<void> task = makeVoidTask();
folly::SemiFuture<folly::Unit> semi =
folly::coro::toSemiFuture(std::move(task));
task = folly::coro::toTask(std::move(semi));
folly::coro::blockingWait(std::move(task));
}
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment