: core_(new detail::Core<T>(Try<T>(std::forward<T2>(val)))) {}
template <class T>
-template <class T2,
- typename std::enable_if<
- folly::is_void_or_unit<T2>::value,
- int>::type>
+template <typename, typename>
Future<T>::Future()
: core_(new detail::Core<T>(Try<T>())) {}
-
template <class T>
Future<T>::~Future() {
detach();
return core_->ready();
}
+template <class T>
+bool Future<T>::hasValue() {
+ return getTry().hasValue();
+}
+
+template <class T>
+bool Future<T>::hasException() {
+ return getTry().hasException();
+}
+
template <class T>
void Future<T>::raise(exception_wrapper exception) {
core_->raise(std::move(exception));
!isFuture<typename std::decay<T2>::type>::value>::type>
/* implicit */ Future(T2&& val);
- template <class T2 = T,
+ template <class T2 = T, typename =
typename std::enable_if<
- folly::is_void_or_unit<T2>::value,
- int>::type = 0>
+ folly::is_void_or_unit<T2>::value>::type>
Future();
~Future();
/** True when the result (or exception) is ready. */
bool isReady() const;
+ /// sugar for getTry().hasValue()
+ bool hasValue();
+
+ /// sugar for getTry().hasException()
+ bool hasException();
+
/** A reference to the Try of the value */
Try<T>& getTry();
auto thenMultiWithExecutor(Executor* x, Callback&& fn)
-> decltype(this->then(std::forward<Callback>(fn)));
+ /// Discard a result, but propagate an exception.
+ Future<Unit> unit() {
+ return then([]{ return Unit{}; });
+ }
+
protected:
typedef detail::Core<T>* corePtr;
using namespace folly;
+std::runtime_error eggs("eggs");
+
TEST(Unit, futureDefaultCtor) {
Future<Unit>();
}
auto v = std::is_same<Unit, Lifted::type>::value;
EXPECT_TRUE(v);
}
+
+TEST(Unit, futureToUnit) {
+ Future<Unit> fu = makeFuture(42).unit();
+ fu.value();
+ EXPECT_TRUE(makeFuture<int>(eggs).unit().hasException());
+}
+
+TEST(Unit, voidFutureToUnit) {
+ Future<Unit> fu = makeFuture().unit();
+ fu.value();
+ EXPECT_TRUE(makeFuture<void>(eggs).unit().hasException());
+}
+
+TEST(Unit, unitFutureToUnitIdentity) {
+ Future<Unit> fu = makeFuture(Unit{}).unit();
+ fu.value();
+ EXPECT_TRUE(makeFuture<Unit>(eggs).unit().hasException());
+}
+
+TEST(Unit, toUnitWhileInProgress) {
+ Promise<int> p;
+ Future<Unit> fu = p.getFuture().unit();
+ EXPECT_FALSE(fu.isReady());
+ p.setValue(42);
+ EXPECT_TRUE(fu.isReady());
+}