From: Sven Over Date: Wed, 31 May 2017 16:43:09 +0000 (-0700) Subject: Future: some fixes re: handling of universal references X-Git-Tag: v2017.06.05.00~15 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=8401bbaffc06e05b14ea7057842b7ed2804375c0;p=folly.git Future: some fixes re: handling of universal references Summary: Callables that are passed as rvalues to makeFutureWith should be executed as rvalues. Generally callables that get captured by value should be executed as rvalues, to allow passing e.g. folly::Partial objects that contain move-only objects like unique_ptr or folly::Promise. `collect` would move out of parameters passed as lvalues. Reviewed By: fugalh Differential Revision: D5120420 fbshipit-source-id: 1633c6b7e3fbb562f4d31e21d28c98b053de9912 --- diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index c4f9ca2f..4089a676 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -324,7 +324,7 @@ template template Future Future::ensure(F&& func) { return this->then([funcw = std::forward(func)](Try && t) mutable { - funcw(); + std::move(funcw)(); return makeFuture(std::move(t)); }); } @@ -333,7 +333,7 @@ template template Future Future::onTimeout(Duration dur, F&& func, Timekeeper* tk) { return within(dur, tk).onError([funcw = std::forward(func)]( - TimedOut const&) { return funcw(); }); + TimedOut const&) { return std::move(funcw)(); }); } template @@ -457,8 +457,7 @@ inline Future Future::via(Executor* executor, int8_t priority) & { template auto via(Executor* x, Func&& func) - -> Future::Inner> -{ + -> Future()())>::Inner> { // TODO make this actually more performant. :-P #7260175 return via(x).then(std::forward(func)); } @@ -504,7 +503,7 @@ makeFutureWith(F&& func) { using InnerType = typename isFuture::type>::Inner; try { - return func(); + return std::forward(func)(); } catch (std::exception& e) { return makeFuture( exception_wrapper(std::current_exception(), e)); @@ -522,9 +521,8 @@ typename std::enable_if< makeFutureWith(F&& func) { using LiftedResult = typename Unit::Lift::type>::type; - return makeFuture(makeTryWith([&func]() mutable { - return func(); - })); + return makeFuture( + makeTryWith([&func]() mutable { return std::forward(func)(); })); } template @@ -574,7 +572,7 @@ collectAll(Fs&&... fs) { auto ctx = std::make_shared::type::value_type...>>(); detail::collectVariadicHelper( - ctx, std::forward::type>(fs)...); + ctx, std::forward(fs)...); return ctx->p.getFuture(); } @@ -677,7 +675,7 @@ collect(Fs&&... fs) { auto ctx = std::make_shared::type::value_type...>>(); detail::collectVariadicHelper( - ctx, std::forward::type>(fs)...); + ctx, std::forward(fs)...); return ctx->p.getFuture(); } diff --git a/folly/futures/detail/Core.h b/folly/futures/detail/Core.h index 3124cda3..b196b664 100644 --- a/folly/futures/detail/Core.h +++ b/folly/futures/detail/Core.h @@ -470,9 +470,11 @@ template