Promise::setValue() for Unit
authorHans Fugal <fugalh@fb.com>
Mon, 27 Apr 2015 21:40:09 +0000 (14:40 -0700)
committerAndrii Grynenko <andrii@fb.com>
Wed, 29 Apr 2015 22:56:59 +0000 (15:56 -0700)
Summary: Unit is a bit special because it's just something special to use instead of `Promise<void>`, so let's offer the same sugar that `Promise<void>` has (`p.setValue()` instead of `p.setValue(Unit())`)

Test Plan: New unit tests. Look, a pun!

Reviewed By: jsedgwick@fb.com

Subscribers: exa, folly-diffs@, jsedgwick, yfeldblum, chalfant, davejwatson, hannesr

FB internal diff: D2014139

Tasks: 6847876

Signature: t1:2014139:1430159950:1484ee420c6d7f0f794a546b78ef1601c2eec45c

folly/futures/Promise-inl.h
folly/futures/Promise.h
folly/futures/test/FutureTest.cpp

index 6f5a7d1a5a141f3645f44d6aa0b36d6cb5edbf6e..4792b57dfe56044cd39c42aaa15d14a8a1af4330 100644 (file)
@@ -123,14 +123,6 @@ void Promise<T>::setValue(M&& v) {
   setTry(Try<T>(std::forward<M>(v)));
 }
 
-template <class T>
-void Promise<T>::setValue() {
-  static_assert(std::is_same<T, void>::value,
-                "Use setValue(value) instead");
-
-  setTry(Try<void>());
-}
-
 template <class T>
 template <class F>
 void Promise<T>::setWith(F&& func) {
index e39ea57f0d2ff9e06ae11547ee9a7994666f109b..560f4dc7d418e087e303d2b96404ad55e90f7996 100644 (file)
@@ -70,8 +70,19 @@ public:
   /// handled.
   void setInterruptHandler(std::function<void(exception_wrapper const&)>);
 
-  /** Fulfill this Promise (only for Promise<void>) */
-  void setValue();
+  /// Fulfill this Promise<void>
+  template <class B = T>
+  typename std::enable_if<std::is_void<B>::value, void>::type
+  setValue() {
+    setTry(Try<T>());
+  }
+
+  /// Sugar to fulfill this Promise<Unit>
+  template <class B = T>
+  typename std::enable_if<std::is_same<Unit, B>::value, void>::type
+  setValue() {
+    setTry(Try<T>(T()));
+  }
 
   /** Set the value (use perfect forwarding for both move and copy) */
   template <class M>
index da3e1c5cd17d323e0a6af8696657354b732b2087..3ae0703d46f67d5a9c04113579d3ba09a25d4bd3 100644 (file)
@@ -1779,3 +1779,8 @@ TEST(Map, Basic) {
 
   EXPECT_TRUE(collect(fs2).isReady());
 }
+
+TEST(Promise, defaultConstructedUnit) {
+  Promise<Unit> p;
+  p.setValue();
+}