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

Implement coro::Future<void>::toFuture()

Summary: When converting to folly::Future we have to perform the void -> folly::Unit conversion.

Differential Revision: D8463177

fbshipit-source-id: 96312d0447dfc381a0ee586030f21b9cdbf4bee4
parent 184dd587
...@@ -78,10 +78,7 @@ class Future { ...@@ -78,10 +78,7 @@ class Future {
return get(); return get();
} }
folly::Future<T> toFuture() && { auto toFuture() &&;
auto executor = promise_->executor_;
return SemiFuture<T>::fromAwaitable(std::move(*this)).via(executor);
}
~Future() { ~Future() {
if (!promise_) { if (!promise_) {
...@@ -118,5 +115,23 @@ class Future { ...@@ -118,5 +115,23 @@ class Future {
Promise<T>* promise_; Promise<T>* promise_;
}; };
namespace detail {
SemiFuture<Unit> toSemiFuture(Future<void> future) {
co_await future;
co_return folly::unit;
}
template <typename T>
SemiFuture<T> toSemiFuture(Future<T> future) {
return SemiFuture<T>::fromAwaitable(std::move(future));
}
} // namespace detail
template <typename T>
auto Future<T>::toFuture() && {
auto executor = promise_->executor_;
return detail::toSemiFuture(std::move(*this)).via(executor);
}
} // namespace coro } // namespace coro
} // namespace folly } // namespace folly
...@@ -189,7 +189,7 @@ TEST(Coro, CurrentExecutor) { ...@@ -189,7 +189,7 @@ TEST(Coro, CurrentExecutor) {
EXPECT_EQ(42, future.get()); EXPECT_EQ(42, future.get());
} }
coro::Task<int> taskTimedWait() { coro::Task<void> taskTimedWait() {
auto fastFuture = auto fastFuture =
futures::sleep(std::chrono::milliseconds{50}).then([] { return 42; }); futures::sleep(std::chrono::milliseconds{50}).then([] { return 42; });
auto fastResult = co_await coro::timed_wait( auto fastResult = co_await coro::timed_wait(
...@@ -215,12 +215,12 @@ coro::Task<int> taskTimedWait() { ...@@ -215,12 +215,12 @@ coro::Task<int> taskTimedWait() {
std::move(slowFuture), std::chrono::milliseconds{100}); std::move(slowFuture), std::chrono::milliseconds{100});
EXPECT_FALSE(slowResult); EXPECT_FALSE(slowResult);
co_return 42; co_return;
} }
TEST(Coro, TimedWait) { TEST(Coro, TimedWait) {
ManualExecutor executor; ManualExecutor executor;
EXPECT_EQ(42, via(&executor, taskTimedWait()).toFuture().getVia(&executor)); via(&executor, taskTimedWait()).toFuture().getVia(&executor);
} }
#endif #endif
...@@ -840,6 +840,10 @@ class SemiFuture : private futures::detail::FutureBase<T> { ...@@ -840,6 +840,10 @@ class SemiFuture : private futures::detail::FutureBase<T> {
return {}; return {};
} }
void return_value(const T& value) {
promise_.setValue(value);
}
void return_value(T& value) { void return_value(T& value) {
promise_.setValue(std::move(value)); promise_.setValue(std::move(value));
} }
......
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