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) {
}
template <class T>
Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
return waitVia(e).result();
Try<T> Future<T>::getTryVia(DrivableExecutor* e) && {
return std::move(waitVia(e).result());
}
template <class T>
Try<T>& Future<T>::getTryVia(TimedDrivableExecutor* e, HighResDuration dur) {
Try<T> Future<T>::getTryVia(TimedDrivableExecutor* e, HighResDuration dur) && {
waitVia(e, dur);
if (!this->isReady()) {
throw_exception<FutureTimeout>();
}
return result();
return std::move(result());
}
namespace futures {
......
......@@ -1143,11 +1143,11 @@ class Future : private futures::detail::FutureBase<T> {
/// Call e->drive() repeatedly until the future is fulfilled. Examples
/// of DrivableExecutor include EventBase and ManualExecutor. Returns a
/// 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
/// 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
/// Future<T> instance.
......
......@@ -465,7 +465,7 @@ TEST(Via, getTryVia) {
ManualExecutor x;
auto f = via(&x).thenValue([](auto&&) { return 23; });
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) {
ManualExecutor x;
auto f = via(&x).then();
EXPECT_FALSE(f.isReady());
auto t = f.getTryVia(&x);
auto t = std::move(f).getTryVia(&x);
EXPECT_TRUE(t.hasValue());
}
{
DummyDrivableExecutor x;
auto f = makeFuture(23);
EXPECT_EQ(23, f.getTryVia(&x).value());
EXPECT_EQ(23, std::move(f).getTryVia(&x).value());
EXPECT_FALSE(x.ran);
}
}
......@@ -489,7 +489,8 @@ TEST(Via, SimpleTimedGetTryVia) {
TimedDrivableExecutor e2;
Promise<folly::Unit> p;
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) {
......
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