Skip to content

Commit

Permalink
Add Any for Future<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Jul 21, 2024
1 parent 3e1274b commit 2fd46a6
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion coroio/corochain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,26 @@ inline TFuture<void> All(std::vector<TFuture<void>>&& futures) {
co_return;
}

template<typename T>
TFuture<T> Any(std::vector<TFuture<T>>&& futures) {
std::vector<TFuture<T>> all = std::move(futures);

auto it = std::find_if(all.begin(), all.end(), [](auto& f) { return f.done(); });
if (it != all.end()) {
co_return it->await_resume();
}

for (auto& f : all) {
f.await_suspend(co_await SelfId());
}
co_await std::suspend_always();
co_return std::find_if(all.begin(), all.end(), [](auto& f) { return f.done(); })->await_resume();
}

inline TFuture<void> Any(std::vector<TFuture<void>>&& futures) {
std::vector<TFuture<void>> all = std::move(futures);

if (std::all_of(all.begin(), all.end(), [](auto& f) { return f.done(); })) {
if (std::find_if(all.begin(), all.end(), [](auto& f) { return f.done(); }) != all.end()) {
co_return;
}

Expand Down

0 comments on commit 2fd46a6

Please sign in to comment.