Adds writer test case for RCU
[folly.git] / folly / futures / Future-inl.h
index 25f8f464f0d1a2e5af1726cfa5bb5457366b4c39..fa7307bdd471a17d32af8a1fa3e3f556ed5ab5dd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 #pragma once
 
 #include <algorithm>
@@ -174,30 +173,50 @@ FutureBase<T>::~FutureBase() {
 
 template <class T>
 T& FutureBase<T>::value() & {
+  return result().value();
+}
+
+template <class T>
+T const& FutureBase<T>::value() const& {
+  return result().value();
+}
+
+template <class T>
+T&& FutureBase<T>::value() && {
+  return std::move(result().value());
+}
+
+template <class T>
+T const&& FutureBase<T>::value() const&& {
+  return std::move(result().value());
+}
+
+template <class T>
+Try<T>& FutureBase<T>::result() & {
   throwIfInvalid();
 
-  return core_->getTry().value();
+  return core_->getTry();
 }
 
 template <class T>
-T const& FutureBase<T>::value() const& {
+Try<T> const& FutureBase<T>::result() const& {
   throwIfInvalid();
 
-  return core_->getTry().value();
+  return core_->getTry();
 }
 
 template <class T>
-T&& FutureBase<T>::value() && {
+Try<T>&& FutureBase<T>::result() && {
   throwIfInvalid();
 
-  return std::move(core_->getTry().value());
+  return std::move(core_->getTry());
 }
 
 template <class T>
-T const&& FutureBase<T>::value() const&& {
+Try<T> const&& FutureBase<T>::result() const&& {
   throwIfInvalid();
 
-  return std::move(core_->getTry().value());
+  return std::move(core_->getTry());
 }
 
 template <class T>
@@ -934,12 +953,12 @@ struct CollectContext {
     Nothing,
     std::vector<Optional<T>>>::type;
 
-  explicit CollectContext(size_t n) : result(n) {}
+  explicit CollectContext(size_t n) : result(n) {
+    finalResult.reserve(n);
+  }
   ~CollectContext() {
     if (!threw.exchange(true)) {
       // map Optional<T> -> T
-      std::vector<T> finalResult;
-      finalResult.reserve(result.size());
       std::transform(result.begin(), result.end(),
                      std::back_inserter(finalResult),
                      [](Optional<T>& o) { return std::move(o.value()); });
@@ -951,6 +970,7 @@ struct CollectContext {
   }
   Promise<Result> p;
   InternalResult result;
+  Result finalResult;
   std::atomic<bool> threw {false};
 };
 
@@ -1443,7 +1463,7 @@ SemiFuture<T>&& SemiFuture<T>::waitVia(DrivableExecutor* e) && {
 
 template <class T>
 T SemiFuture<T>::get() && {
-  return std::move(wait().value());
+  return std::move(wait()).value();
 }
 
 template <class T>
@@ -1458,19 +1478,27 @@ T SemiFuture<T>::get(Duration dur) && {
 
 template <class T>
 Try<T> SemiFuture<T>::getTry() && {
-  wait();
-  return std::move(this->core_->getTry());
+  return std::move(wait()).result();
+}
+
+template <class T>
+Try<T> SemiFuture<T>::getTry(Duration dur) && {
+  wait(dur);
+  if (this->isReady()) {
+    return std::move(this->result());
+  } else {
+    throwTimedOut();
+  }
 }
 
 template <class T>
 T SemiFuture<T>::getVia(DrivableExecutor* e) && {
-  return std::move(waitVia(e).value());
+  return std::move(waitVia(e)).value();
 }
 
 template <class T>
 Try<T> SemiFuture<T>::getTryVia(DrivableExecutor* e) && {
-  waitVia(e);
-  return std::move(this->core_->getTry());
+  return std::move(waitVia(e)).result();
 }
 
 template <class T>
@@ -1526,9 +1554,7 @@ T Future<T>::get(Duration dur) {
 
 template <class T>
 Try<T>& Future<T>::getTry() {
-  throwIfInvalid();
-
-  return this->core_->getTry();
+  return result();
 }
 
 template <class T>