Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add set_error(std::error_code) into await_transform #523

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/unifex/await_transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <exception>
#include <optional>
#include <type_traits>
#include <system_error>

#include <unifex/detail/prologue.hpp>

Expand Down Expand Up @@ -110,6 +111,10 @@ struct _awaitable_base<Promise, Value>::type {
result_->state_ = _state::exception;
continuation_.resume();
}

void set_error(std::error_code code) && noexcept {
std::move(*this).set_error(std::make_exception_ptr(std::system_error{code}));
}

void set_done() && noexcept {
result_->state_ = _state::done;
Expand Down
17 changes: 17 additions & 0 deletions test/awaitable_senders_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#if !UNIFEX_NO_COROUTINES

#include <unifex/just.hpp>
#include <unifex/just_error.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/task.hpp>
#include <unifex/stop_when.hpp>
Expand All @@ -45,6 +46,22 @@ TEST(awaitable_senders, non_void) {
EXPECT_EQ(42, *answer);
}

#if !UNIFEX_NO_EXCEPTIONS
TEST(awaitable_senders, error_code) {
const std::error_code ec{42, std::system_category()};
auto makeTask = [&]() -> task<int> {
co_await just_error(ec);
co_return 0;
};
try {
sync_wait(makeTask());
FAIL();
} catch (const std::system_error& e) {
ASSERT_EQ(ec, e.code());
}
}
#endif // !UNIFEX_NO_EXCEPTIONS

TEST(awaitable_senders, void) {
// HACK: ideally would be task<void> once that specialisation has been added.
auto makeTask = [&]() -> task<unifex::unit> {
Expand Down