Commit 115fe40f authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Support co_return Try<T> from Task<T>

Summary: This can be useful to avoid the cost of re-throwing exceptions.

Reviewed By: yfeldblum, LeeHowes

Differential Revision: D16628722

fbshipit-source-id: 434842d99369d0293c4eeb894e6feac15ec16173
parent 4785adac
......@@ -123,6 +123,21 @@ class TaskPromise : public TaskPromiseBase {
result_.emplace(static_cast<U&&>(value));
}
void return_value(const Try<StorageType>& t) {
DCHECK(t.hasValue() || t.hasException());
result_ = t;
}
void return_value(Try<StorageType>& t) {
DCHECK(t.hasValue() || t.hasException());
result_ = t;
}
void return_value(Try<StorageType>&& t) {
DCHECK(t.hasValue() || t.hasException());
result_ = std::move(t);
}
Try<StorageType>& result() {
return result_;
}
......
......@@ -539,4 +539,29 @@ TEST(Coro, FutureTry) {
}());
}
TEST(Coro, CoReturnTry) {
EXPECT_EQ(42, folly::coro::blockingWait([]() -> folly::coro::Task<int> {
co_return folly::Try<int>(42);
}()));
struct ExpectedException : public std::runtime_error {
ExpectedException() : std::runtime_error("ExpectedException") {}
};
EXPECT_THROW(
folly::coro::blockingWait([]() -> folly::coro::Task<int> {
co_return folly::Try<int>(ExpectedException());
}()),
ExpectedException);
folly::Try<int> t(42);
EXPECT_EQ(42, folly::coro::blockingWait([&]() -> folly::coro::Task<int> {
co_return t;
}()));
const folly::Try<int> tConst(42);
EXPECT_EQ(42, folly::coro::blockingWait([&]() -> folly::coro::Task<int> {
co_return tConst;
}()));
}
#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