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