Commit 6a469b3f authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Add unit() method to SemiFuture

Reviewed By: yfeldblum

Differential Revision: D17086382

fbshipit-source-id: 318834a1d82025bc0c34bcf7a95fedfffdb02ddb
parent b277c2bb
......@@ -868,6 +868,11 @@ SemiFuture<T> SemiFuture<T>::deferError(F&& func) && {
});
}
template <class T>
SemiFuture<Unit> SemiFuture<T>::unit() && {
return std::move(*this).deferValue([](T&&) {});
}
template <typename T>
SemiFuture<T> SemiFuture<T>::delayed(Duration dur, Timekeeper* tk) && {
return collectAllSemiFuture(*this, futures::sleep(dur, tk))
......
......@@ -791,6 +791,20 @@ class SemiFuture : private futures::detail::FutureBase<T> {
return std::move(*this).deferError(&func);
}
/// Convenience method for ignoring the value and creating a Future<Unit>.
/// Exceptions still propagate.
///
/// Preconditions:
///
/// - `valid() == true` (else throws FutureInvalid)
///
/// Postconditions:
///
/// - Calling code should act as if `valid() == false`,
/// i.e., as if `*this` was moved into RESULT.
/// - `RESULT.valid() == true`
SemiFuture<Unit> unit() &&;
SemiFuture<T> within(Duration dur, Timekeeper* tk = nullptr) && {
return std::move(*this).within(dur, FutureTimeout(), tk);
}
......
......@@ -50,6 +50,12 @@ TEST(SemiFuture, futureDefaultCtor) {
SemiFuture<Unit>();
}
TEST(SemiFuture, semiFutureToUnit) {
SemiFuture<Unit> fu = makeSemiFuture(42).unit();
std::move(fu).get();
EXPECT_THROW(makeSemiFuture<int>(eggs).unit().get(), eggs_t);
}
TEST(SemiFuture, makeSemiFutureWithUnit) {
int count = 0;
SemiFuture<Unit> fu = makeSemiFutureWith([&] { count++; });
......
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