Commit e22e5641 authored by Kenton Varda's avatar Kenton Varda

Fix joinPromises for Promise<void>.

parent 68ad3220
......@@ -547,6 +547,24 @@ private:
Array<ExceptionOr<T>> resultParts;
};
template <>
class ArrayJoinPromiseNode<void> final: public ArrayJoinPromiseNodeBase {
public:
ArrayJoinPromiseNode(Array<Own<PromiseNode>> promises,
Array<ExceptionOr<_::Void>> resultParts)
: ArrayJoinPromiseNodeBase(
kj::mv(promises), resultParts.begin(), sizeof(ExceptionOr<_::Void>)),
resultParts(kj::mv(resultParts)) {}
protected:
void getNoError(ExceptionOrValue& output) noexcept override {
output.as<_::Void>() = _::Void();
}
private:
Array<ExceptionOr<_::Void>> resultParts;
};
// -------------------------------------------------------------------
class EagerPromiseNodeBase: public PromiseNode, protected Event {
......@@ -758,6 +776,12 @@ Promise<Array<T>> joinPromises(Array<Promise<T>>&& promises) {
heapArray<_::ExceptionOr<T>>(promises.size())));
}
inline Promise<void> joinPromises(Array<Promise<void>>&& promises) {
return Promise<void>(false, kj::heap<_::ArrayJoinPromiseNode<void>>(
KJ_MAP(p, promises) { return kj::mv(p.node); },
heapArray<_::ExceptionOr<_::Void>>(promises.size())));
}
// =======================================================================================
namespace _ { // private
......
......@@ -40,6 +40,7 @@ class WaitScope;
template <typename T>
Promise<Array<T>> joinPromises(Array<Promise<T>>&& promises);
Promise<void> joinPromises(Array<Promise<void>>&& promises);
namespace _ { // private
......@@ -181,6 +182,7 @@ private:
friend class TaskSetImpl;
template <typename U>
friend Promise<Array<U>> kj::joinPromises(Array<Promise<U>>&& promises);
friend Promise<void> kj::joinPromises(Array<Promise<void>>&& promises);
};
void detach(kj::Promise<void>&& promise);
......
......@@ -531,6 +531,20 @@ TEST(Async, ArrayJoin) {
EXPECT_EQ(789, result[2]);
}
TEST(Async, ArrayJoinVoid) {
EventLoop loop;
WaitScope waitScope(loop);
auto builder = heapArrayBuilder<Promise<void>>(3);
builder.add(READY_NOW);
builder.add(READY_NOW);
builder.add(READY_NOW);
Promise<void> promise = joinPromises(builder.finish());
promise.wait(waitScope);
}
class ErrorHandlerImpl: public TaskSet::ErrorHandler {
public:
uint exceptionCount = 0;
......
......@@ -294,6 +294,7 @@ private:
friend class _::NeverDone;
template <typename U>
friend Promise<Array<U>> joinPromises(Array<Promise<U>>&& promises);
friend Promise<void> joinPromises(Array<Promise<void>>&& promises);
};
template <typename T>
......
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