Commit 8541be76 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

futures::when semifuture support

Summary: Allow futures::when to pass through a SemiFuture-returning function.

Reviewed By: yfeldblum

Differential Revision: D17001974

fbshipit-source-id: dbe8b347f0a4c3fd19774c68c4e58e3b523b971f
parent 6a469b3f
......@@ -2448,7 +2448,8 @@ Future<T> Future<T>::filter(F&& predicate) && {
}
template <class F>
Future<Unit> when(bool p, F&& thunk) {
auto when(bool p, F&& thunk)
-> decltype(std::declval<invoke_result_t<F>>().unit()) {
return p ? std::forward<F>(thunk)().unit() : makeFuture();
}
......
......@@ -1872,13 +1872,6 @@ class Future : private futures::detail::FutureBase<T> {
template <class T2>
friend Future<T2> makeFuture(Try<T2>);
/// Carry out the computation contained in the given future if
/// the predicate holds.
///
/// thunk behaves like std::function<Future<T2>(void)>
template <class F>
friend Future<Unit> when(bool p, F&& thunk);
template <class FT>
friend Future<FT> futures::detail::convertFuture(
SemiFuture<FT>&& sf,
......@@ -2086,6 +2079,15 @@ auto mapTry(Executor& exec, Collection&& c, F&& func)
return mapTry(exec, c.begin(), c.end(), std::forward<F>(func));
}
/// Carry out the computation contained in the given future if
/// the predicate holds.
///
/// thunk behaves like std::function<Future<T2>(void)> or
/// std::function<SemiFuture<T2>(void)>
template <class F>
auto when(bool p, F&& thunk)
-> decltype(std::declval<invoke_result_t<F>>().unit());
#if FOLLY_FUTURE_USING_FIBER
SemiFuture<Unit> wait(std::unique_ptr<fibers::Baton> baton);
......
......@@ -30,6 +30,15 @@ TEST(When, predicateFalse) {
auto f1 = folly::when(false, thunk);
f1.wait();
EXPECT_EQ(0, i);
auto sfThunk = [&] {
return makeSemiFuture().deferValue([&](auto&&) { i += 1; });
};
// false
auto f1s = folly::when(false, sfThunk);
f1s.wait();
EXPECT_EQ(0, i);
}
TEST(When, predicateTrue) {
......@@ -40,4 +49,13 @@ TEST(When, predicateTrue) {
auto f2 = folly::when(true, thunk);
f2.wait();
EXPECT_EQ(1, i);
auto sfThunk = [&] {
return makeSemiFuture().deferValue([&](auto&&) { i += 1; });
};
// true
auto f2s = folly::when(true, sfThunk);
f2s.wait();
EXPECT_EQ(2, i);
}
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