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