From: Hans Fugal Date: Mon, 27 Apr 2015 21:40:09 +0000 (-0700) Subject: Promise::setValue() for Unit X-Git-Tag: v0.37.0~8 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ef0137ceaf11b0dce2b732797c480ccb9adb20b6;p=folly.git Promise::setValue() for Unit Summary: Unit is a bit special because it's just something special to use instead of `Promise`, so let's offer the same sugar that `Promise` 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 --- diff --git a/folly/futures/Promise-inl.h b/folly/futures/Promise-inl.h index 6f5a7d1a..4792b57d 100644 --- a/folly/futures/Promise-inl.h +++ b/folly/futures/Promise-inl.h @@ -123,14 +123,6 @@ void Promise::setValue(M&& v) { setTry(Try(std::forward(v))); } -template -void Promise::setValue() { - static_assert(std::is_same::value, - "Use setValue(value) instead"); - - setTry(Try()); -} - template template void Promise::setWith(F&& func) { diff --git a/folly/futures/Promise.h b/folly/futures/Promise.h index e39ea57f..560f4dc7 100644 --- a/folly/futures/Promise.h +++ b/folly/futures/Promise.h @@ -70,8 +70,19 @@ public: /// handled. void setInterruptHandler(std::function); - /** Fulfill this Promise (only for Promise) */ - void setValue(); + /// Fulfill this Promise + template + typename std::enable_if::value, void>::type + setValue() { + setTry(Try()); + } + + /// Sugar to fulfill this Promise + template + typename std::enable_if::value, void>::type + setValue() { + setTry(Try(T())); + } /** Set the value (use perfect forwarding for both move and copy) */ template diff --git a/folly/futures/test/FutureTest.cpp b/folly/futures/test/FutureTest.cpp index da3e1c5c..3ae0703d 100644 --- a/folly/futures/test/FutureTest.cpp +++ b/folly/futures/test/FutureTest.cpp @@ -1779,3 +1779,8 @@ TEST(Map, Basic) { EXPECT_TRUE(collect(fs2).isReady()); } + +TEST(Promise, defaultConstructedUnit) { + Promise p; + p.setValue(); +}