Commit dd674591 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Let makeSemiFutureWith accept functions returning Future

Summary: [Folly] Let `makeSemiFutureWith` accept functions returning `Future`, joining what would otherwise be the resulting `SemiFuture<Future<...>>` into `SemiFuture<...>`.

Reviewed By: LeeHowes

Differential Revision: D8505834

fbshipit-source-id: 10b626fae00b5002877295b59716066f089cb6a8
parent 7e2b9654
......@@ -676,10 +676,11 @@ SemiFuture<typename std::decay<T>::type> makeSemiFuture(T&& t) {
// makeSemiFutureWith(SemiFuture<T>()) -> SemiFuture<T>
template <class F>
typename std::
enable_if<isSemiFuture<invoke_result_t<F>>::value, invoke_result_t<F>>::type
makeSemiFutureWith(F&& func) {
using InnerType = typename isSemiFuture<invoke_result_t<F>>::Inner;
typename std::enable_if<
isFutureOrSemiFuture<invoke_result_t<F>>::value,
SemiFuture<typename invoke_result_t<F>::value_type>>::type
makeSemiFutureWith(F&& func) {
using InnerType = typename isFutureOrSemiFuture<invoke_result_t<F>>::Inner;
try {
return std::forward<F>(func)();
} catch (std::exception& e) {
......@@ -695,7 +696,7 @@ typename std::
// makeSemiFutureWith(void()) -> SemiFuture<Unit>
template <class F>
typename std::enable_if<
!(isSemiFuture<invoke_result_t<F>>::value),
!(isFutureOrSemiFuture<invoke_result_t<F>>::value),
SemiFuture<lift_unit_t<invoke_result_t<F>>>>::type
makeSemiFutureWith(F&& func) {
using LiftedResult = lift_unit_t<invoke_result_t<F>>;
......
......@@ -143,15 +143,16 @@ SemiFuture<Unit> makeSemiFuture();
// makeSemiFutureWith(SemiFuture<T>()) -> SemiFuture<T>
template <class F>
typename std::
enable_if<isSemiFuture<invoke_result_t<F>>::value, invoke_result_t<F>>::type
makeSemiFutureWith(F&& func);
typename std::enable_if<
isFutureOrSemiFuture<invoke_result_t<F>>::value,
SemiFuture<typename invoke_result_t<F>::value_type>>::type
makeSemiFutureWith(F&& func);
// makeSemiFutureWith(T()) -> SemiFuture<T>
// makeSemiFutureWith(void()) -> SemiFuture<Unit>
template <class F>
typename std::enable_if<
!(isSemiFuture<invoke_result_t<F>>::value),
!(isFutureOrSemiFuture<invoke_result_t<F>>::value),
SemiFuture<typename lift_unit<invoke_result_t<F>>::type>>::type
makeSemiFutureWith(F&& func);
......
......@@ -317,6 +317,10 @@ TEST(SemiFuture, makeSemiFuture) {
EXPECT_NO_THROW(makeSemiFutureWith(failfunf));
EXPECT_THROW(makeSemiFutureWith(failfunf).value(), eggs_t);
auto futurefun = [] { return makeFuture<int>(44); };
EXPECT_TYPE(makeSemiFutureWith(futurefun), SemiFuture<int>);
EXPECT_EQ(44, makeSemiFutureWith(futurefun).value());
EXPECT_TYPE(makeSemiFuture(), SemiFuture<Unit>);
}
......
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