Future<typename>::getTryVia
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 1 Dec 2016 20:43:06 +0000 (12:43 -0800)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Thu, 1 Dec 2016 20:53:35 +0000 (12:53 -0800)
Summary:
[Folly] `Future<typename>::getTryVia`.

We have `waitVia` and `getVia` and `getTry`, but `getTryVia` was missing.

Reviewed By: fugalh

Differential Revision: D4254972

fbshipit-source-id: 403e1a3496ad5dfc93c4c55ab75a83bcc89e6c64

folly/futures/Future-inl.h
folly/futures/Future.h
folly/futures/test/ViaTest.cpp

index 1cd4035e676623f641e14e01e7d5b256ca020d29..61ea4ba86e7b8a3f5d87684b9f2e8c2ed75e2790 100644 (file)
@@ -415,6 +415,11 @@ Try<T>& Future<T>::getTry() {
   return core_->getTry();
 }
 
+template <class T>
+Try<T>& Future<T>::getTryVia(DrivableExecutor* e) {
+  return waitVia(e).getTry();
+}
+
 template <class T>
 Optional<Try<T>> Future<T>::poll() {
   Optional<Try<T>> o;
index 7563507d2bbc9b631a17a2a7688d850260f8d595..23cd5a96643c215eb4e13703d6690b95b8b09cad 100644 (file)
@@ -129,6 +129,11 @@ class Future {
   /** A reference to the Try of the value */
   Try<T>& getTry();
 
+  /// 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);
+
   /// If the promise has been fulfilled, return an Optional with the Try<T>.
   /// Otherwise return an empty Optional.
   /// Note that this moves the Try<T> out.
index 2914cb2a7f0bf852919544104ea71bac6f33e623..69bb96798599b5b72e2c9166f3ba2f1d5ec9f92e 100644 (file)
@@ -400,6 +400,32 @@ TEST(Via, getVia) {
   }
 }
 
+TEST(Via, getTryVia) {
+  {
+    // non-void
+    ManualExecutor x;
+    auto f = via(&x).then([] { return 23; });
+    EXPECT_FALSE(f.isReady());
+    EXPECT_EQ(23, f.getTryVia(&x).value());
+  }
+
+  {
+    // void
+    ManualExecutor x;
+    auto f = via(&x).then();
+    EXPECT_FALSE(f.isReady());
+    auto t = f.getTryVia(&x);
+    EXPECT_TRUE(t.hasValue());
+  }
+
+  {
+    DummyDrivableExecutor x;
+    auto f = makeFuture(23);
+    EXPECT_EQ(23, f.getTryVia(&x).value());
+    EXPECT_FALSE(x.ran);
+  }
+}
+
 TEST(Via, waitVia) {
   {
     ManualExecutor x;