Commit f5744f58 authored by Lee Howes's avatar Lee Howes Committed by Facebook GitHub Bot

Modify Future::getTryVia to be r-value qualified and destructive

Summary: For consistency with get() make getTryVia r-value qualified and move the result out. This has the benefit of removing surprise copies in code that already operated on a temporary future but ended up with an l-value reference to copy from as a result of this operation.

Reviewed By: ispeters

Differential Revision: D23610051

fbshipit-source-id: a11344b9c8680f9498abc307ba1c7c793e3403e1
parent ba3db8b9
...@@ -2393,17 +2393,17 @@ T Future<T>::getVia(TimedDrivableExecutor* e, HighResDuration dur) { ...@@ -2393,17 +2393,17 @@ T Future<T>::getVia(TimedDrivableExecutor* e, HighResDuration dur) {
} }
template <class T> template <class T>
Try<T>& Future<T>::getTryVia(DrivableExecutor* e) { Try<T> Future<T>::getTryVia(DrivableExecutor* e) && {
return waitVia(e).result(); return std::move(waitVia(e).result());
} }
template <class T> template <class T>
Try<T>& Future<T>::getTryVia(TimedDrivableExecutor* e, HighResDuration dur) { Try<T> Future<T>::getTryVia(TimedDrivableExecutor* e, HighResDuration dur) && {
waitVia(e, dur); waitVia(e, dur);
if (!this->isReady()) { if (!this->isReady()) {
throw_exception<FutureTimeout>(); throw_exception<FutureTimeout>();
} }
return result(); return std::move(result());
} }
namespace futures { namespace futures {
......
...@@ -1143,11 +1143,11 @@ class Future : private futures::detail::FutureBase<T> { ...@@ -1143,11 +1143,11 @@ class Future : private futures::detail::FutureBase<T> {
/// Call e->drive() repeatedly until the future is fulfilled. Examples /// Call e->drive() repeatedly until the future is fulfilled. Examples
/// of DrivableExecutor include EventBase and ManualExecutor. Returns a /// of DrivableExecutor include EventBase and ManualExecutor. Returns a
/// reference to the Try of the value. /// reference to the Try of the value.
Try<T>& getTryVia(DrivableExecutor* e); Try<T> getTryVia(DrivableExecutor* e) &&;
/// getTryVia but will wait only until `dur` elapses. Returns the /// getTryVia but will wait only until `dur` elapses. Returns the
/// Try of the value (moved-out) or may throw a FutureTimeout exception. /// Try of the value (moved-out) or may throw a FutureTimeout exception.
Try<T>& getTryVia(TimedDrivableExecutor* e, HighResDuration dur); Try<T> getTryVia(TimedDrivableExecutor* e, HighResDuration dur) &&;
/// Unwraps the case of a Future<Future<T>> instance, and returns a simple /// Unwraps the case of a Future<Future<T>> instance, and returns a simple
/// Future<T> instance. /// Future<T> instance.
......
...@@ -465,7 +465,7 @@ TEST(Via, getTryVia) { ...@@ -465,7 +465,7 @@ TEST(Via, getTryVia) {
ManualExecutor x; ManualExecutor x;
auto f = via(&x).thenValue([](auto&&) { return 23; }); auto f = via(&x).thenValue([](auto&&) { return 23; });
EXPECT_FALSE(f.isReady()); EXPECT_FALSE(f.isReady());
EXPECT_EQ(23, f.getTryVia(&x).value()); EXPECT_EQ(23, std::move(f).getTryVia(&x).value());
} }
{ {
...@@ -473,14 +473,14 @@ TEST(Via, getTryVia) { ...@@ -473,14 +473,14 @@ TEST(Via, getTryVia) {
ManualExecutor x; ManualExecutor x;
auto f = via(&x).then(); auto f = via(&x).then();
EXPECT_FALSE(f.isReady()); EXPECT_FALSE(f.isReady());
auto t = f.getTryVia(&x); auto t = std::move(f).getTryVia(&x);
EXPECT_TRUE(t.hasValue()); EXPECT_TRUE(t.hasValue());
} }
{ {
DummyDrivableExecutor x; DummyDrivableExecutor x;
auto f = makeFuture(23); auto f = makeFuture(23);
EXPECT_EQ(23, f.getTryVia(&x).value()); EXPECT_EQ(23, std::move(f).getTryVia(&x).value());
EXPECT_FALSE(x.ran); EXPECT_FALSE(x.ran);
} }
} }
...@@ -489,7 +489,8 @@ TEST(Via, SimpleTimedGetTryVia) { ...@@ -489,7 +489,8 @@ TEST(Via, SimpleTimedGetTryVia) {
TimedDrivableExecutor e2; TimedDrivableExecutor e2;
Promise<folly::Unit> p; Promise<folly::Unit> p;
auto f = p.getFuture(); auto f = p.getFuture();
EXPECT_THROW(f.getTryVia(&e2, std::chrono::seconds(1)), FutureTimeout); EXPECT_THROW(
std::move(f).getTryVia(&e2, std::chrono::seconds(1)), FutureTimeout);
} }
TEST(Via, waitVia) { TEST(Via, waitVia) {
......
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