Add co_awaitTry() support to AsyncGenerator
Summary:
Add the ability for an AsyncGenerator to yield an error without needing to throw an exception and for consumers to retrieve the error without rethrowing the exception.
Added a new `folly::coro::co_error` type that acts as a simple wrapper around a `folly::exception_wrapper` that can be used to indicate that this value is an error.
This allows `AsyncGenerator` bodies to now `co_yield` a `co_error` as a way of completing with an error without needing to throw the exception.
For example:
```
class SomeError : public std::exception { ... };
folly::coro::AsyncGenerator<int> example() {
co_yield 42;
co_yield folly::coro::co_error(SomeError{});
}
```
This diff also adds support for `folly::coro::co_awaitTry()` to the `AsyncGenerator::next()` method.
For example:
```
folly::coro::Task<void> consumer() {
folly::coro::AsyncGenerator<int> gen = example();
folly::Try<int> result =
co_await folly::coro::co_awaitTry(gen.next());
// use result
}
```
Added a benchmark to compare performance of the new interfaces vs the existing throwing-based error handling.
Reviewed By: yfeldblum
Differential Revision: D18015926
fbshipit-source-id: 27d2e16e7dde9b4c871846f5d17b40ec3737330a
Showing
Please register or sign in to comment