Commit 242ddb11 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Consistency with folly::SemiFuture continuations 2/n: thenTry

Summary:
Step 2 in adding full set of r-value-qualified unambiguous continuation methods to folly::Future for consistency with folly::SemiFuture.
 * Adds r-value qualified thenTry which directly forwards to then, but has compile-time restrictions on the continuations that can be passed to avoid ambiguity.

Reviewed By: yfeldblum

Differential Revision: D7944820

fbshipit-source-id: 55ee09455703bb73168694175e884b053caff426
parent f7e99444
......@@ -952,6 +952,13 @@ Future<T>::then(R(Caller::*func)(Args...), Caller *instance) {
});
}
template <class T>
template <typename F>
Future<typename futures::detail::tryCallableResult<T, F>::value_type>
Future<T>::thenTry(F&& func) && {
return std::move(*this).then(std::forward<F>(func));
}
template <class T>
template <typename F>
Future<typename futures::detail::valueCallableResult<T, F>::value_type>
......
......@@ -768,9 +768,33 @@ class Future : private futures::detail::FutureBase<T> {
.via(oldX);
}
/**
* Then for functions taking a T rather than a Try<T>.
*/
/** When this Future has completed, execute func which is a function that
takes one of:
(const) Try<T>&&
(const) Try<T>&
(const) Try<T>
Func shall return either another Future or a value.
A Future for the return type of func is returned.
Future<string> f2 = f1.then([](Try<T>&&) { return string("foo"); });
The Future given to the functor is ready, and the functor may call
value(), which may rethrow if this has captured an exception. If func
throws, the exception will be captured in the Future that is returned.
*/
template <typename F>
Future<typename futures::detail::tryCallableResult<T, F>::value_type> thenTry(
F&& func) &&;
/** When this Future has completed, execute func which is a function that
takes one of:
(const) T&&
(const) T&
(const) T
(void)
*/
template <typename F>
Future<typename futures::detail::valueCallableResult<T, F>::value_type>
thenValue(F&& func) &&;
......
......@@ -612,6 +612,40 @@ TEST(Future, then) {
EXPECT_EQ(f.value(), "1;2;3;4;5;6;7;8;9;10;11;12;13");
}
static folly::Future<std::string> doWorkStaticTry(Try<std::string>&& t) {
return makeFuture(t.value() + ";8");
}
TEST(Future, thenTrythenValue) {
auto f =
makeFuture<std::string>("0")
.thenTry([]() { return makeFuture<std::string>("1"); })
.thenTry(
[](Try<std::string>&& t) { return makeFuture(t.value() + ";2"); })
.thenTry([](const Try<std::string>&& t) {
return makeFuture(t.value() + ";3");
})
.thenTry(
[](Try<std::string>& t) { return makeFuture(t.value() + ";4"); })
.thenTry([](const Try<std::string>& t) {
return makeFuture(t.value() + ";5");
})
.thenTry(
[](Try<std::string> t) { return makeFuture(t.value() + ";6"); })
.thenTry([](const Try<std::string> t) {
return makeFuture(t.value() + ";7");
})
.thenTry(doWorkStaticTry)
.thenValue([](std::string&& s) { return makeFuture(s + ";9"); })
.thenValue(
[](const std::string&& s) { return makeFuture(s + ";10"); })
.thenValue([](std::string& s) { return makeFuture(s + ";11"); })
.thenValue([](const std::string& s) { return makeFuture(s + ";12"); })
.thenValue([](std::string s) { return makeFuture(s + ";13"); })
.thenValue([](const std::string s) { return makeFuture(s + ";14"); });
EXPECT_EQ(f.value(), "1;2;3;4;5;6;7;8;9;10;11;12;13;14");
}
TEST(Future, thenTry) {
bool flag = false;
......
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