Skip to content

Commit

Permalink
Add co_awaitTry support to Baton
Browse files Browse the repository at this point in the history
Summary:
Since `Baton` doesn't currently support `co_awaitTry`, I found myself writing this sort of thing in my "coro/safe" implementation:

```
Try<ResultT> res;
if constexpr (std::is_same_v<
    decltype(inner), ::folly::coro::Baton::WaitOperation>) {
  co_await std::move(t);
} else {
  res = co_await co_awaitTry(std::move(t));
}
```

After this diff, the above could be abbreviated to:

```
auto res = co_await co_awaitTry(std::move(t));
```

Since awaiting `Baton` doesn't throw, I see no downside to supporting `co_awaitTry` here, besides the minor header bloat.

Reviewed By: andriigrynenko

Differential Revision: D64274928

fbshipit-source-id: 201f8b0765f19a769bd80b34ff4c86792c5b6df4
  • Loading branch information
Alexey Spiridonov authored and facebook-github-bot committed Oct 16, 2024
1 parent f661bfb commit 6c0b1ec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions folly/coro/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ cpp_library(
"//folly/synchronization:atomic_util",
],
exported_deps = [
"//folly:try",
"//folly/experimental/coro:coroutine",
],
)
Expand Down
5 changes: 5 additions & 0 deletions folly/coro/Baton.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <atomic>

#include <folly/Try.h>
#include <folly/experimental/coro/Coroutine.h>

#if FOLLY_HAS_COROUTINES
Expand Down Expand Up @@ -107,6 +108,10 @@ class Baton {

void await_resume() noexcept {}

// Awaiting a baton doesn't throw, so supporting `co_awaitTry` here only
// serves to simplify generic code.
folly::Try<void> await_resume_try() noexcept { return {}; }

protected:
friend class Baton;

Expand Down
3 changes: 2 additions & 1 deletion folly/coro/test/BatonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ TEST_F(BatonTest, MultiAwaitBaton) {

auto makeTask2 = [&]() -> coro::Task<void> {
reachedBeforeAwait2 = true;
co_await baton;
// Equivalent to `co_await baton`, we just want it to compile.
co_await co_awaitTry(baton.operator co_await());
reachedAfterAwait2 = true;
};

Expand Down

0 comments on commit 6c0b1ec

Please sign in to comment.