Commit ef0137ce authored by Hans Fugal's avatar Hans Fugal Committed by Andrii Grynenko

Promise::setValue() for Unit

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
parent 0cfa7e4b
......@@ -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) {
......
......@@ -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>
......
......@@ -1779,3 +1779,8 @@ TEST(Map, Basic) {
EXPECT_TRUE(collect(fs2).isReady());
}
TEST(Promise, defaultConstructedUnit) {
Promise<Unit> p;
p.setValue();
}
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