Commit 407e4380 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Let Future handle throwing FutureNotReady

Summary: [Folly] Let `Future` handle throwing `FutureNotReady`, since it is logically the thing that knows about specific exceptions in its contract.

Reviewed By: LeeHowes

Differential Revision: D7964855

fbshipit-source-id: 49f3220a6a41ebe87f577814c0940e00497092c0
parent 61a56337
......@@ -191,22 +191,22 @@ T const&& FutureBase<T>::value() const&& {
template <class T>
Try<T>& FutureBase<T>::result() & {
return getCore().getTry();
return getCoreTryChecked();
}
template <class T>
Try<T> const& FutureBase<T>::result() const& {
return getCore().getTry();
return getCoreTryChecked();
}
template <class T>
Try<T>&& FutureBase<T>::result() && {
return std::move(getCore().getTry());
return std::move(getCoreTryChecked());
}
template <class T>
Try<T> const&& FutureBase<T>::result() const&& {
return std::move(getCore().getTry());
return std::move(getCoreTryChecked());
}
template <class T>
......
......@@ -188,6 +188,22 @@ class FutureBase {
return *self.core_;
}
Try<T>& getCoreTryChecked() {
return getCoreTryChecked(*this);
}
Try<T> const& getCoreTryChecked() const {
return getCoreTryChecked(*this);
}
template <typename Self>
static decltype(auto) getCoreTryChecked(Self& self) {
auto& core = self.getCore();
if (!core.hasResult()) {
throwFutureNotReady();
}
return core.getTry();
}
// shared core state object
// usually you should use `getCore()` instead of directly accessing `core_`.
corePtr core_;
......
......@@ -28,7 +28,6 @@
#include <folly/ScopeGuard.h>
#include <folly/Try.h>
#include <folly/Utility.h>
#include <folly/futures/FutureException.h>
#include <folly/futures/detail/FSM.h>
#include <folly/lang/Assume.h>
#include <folly/lang/Exception.h>
......@@ -401,11 +400,8 @@ class Core final {
template <typename Self>
static decltype(auto) getTryImpl(Self& self) {
if (self.ready()) {
return *self.result_;
} else {
throwFutureNotReady();
}
assume(self.result_.has_value());
return self.result_.value();
}
folly::Function<void(Try<T>&&)> callback_;
......
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