Skip to content

Commit

Permalink
return block
Browse files Browse the repository at this point in the history
  • Loading branch information
kelbon committed Sep 24, 2024
1 parent bb4e146 commit c165d78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/kelcoro/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ template <typename T>
struct return_block {
std::optional<T> storage = std::nullopt;

constexpr void return_value(T value) noexcept(std::is_nothrow_move_constructible_v<T>) {
storage.emplace(std::move(value));
template <typename U = T>
constexpr void return_value(U&& value) noexcept(std::is_nothrow_constructible_v<T, U&&>) {
storage.emplace(std::forward<U>(value));
}
constexpr T&& result() noexcept KELCORO_LIFETIMEBOUND {
assert(storage.has_value());
Expand Down
22 changes: 22 additions & 0 deletions tests/test_coroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,27 @@ TEST(contexted_task) {
return error_count;
}

// checks that 'return_value' may be used with non movable types
dd::task<std::tuple<int, std::string, std::vector<int>, std::unique_ptr<int>>> complex_ret(int i) {
switch (i) {
case 0:
co_return {5, std::string(""), std::vector<int>{5, 10}, std::unique_ptr<int>(nullptr)};
case 1: {
std::tuple<int, std::string, std::vector<int>, std::unique_ptr<int>> v{
5, std::string(""), std::vector<int>{5, 10}, std::unique_ptr<int>(nullptr)};
;
co_return v;
}
default:
co_return {};
}
}
TEST(complex_ret) {
complex_ret(0).start_and_detach();
complex_ret(1).start_and_detach();
return error_count;
}

int main() {
srand(time(0));
size_t ec = 0;
Expand All @@ -651,6 +672,7 @@ int main() {
ec += TESTtask_blocking_wait_noexcept();
ec += TESTtask_start_and_detach();
ec += TESTcontexted_task();
ec += TESTcomplex_ret();
return ec;
}
#else
Expand Down

0 comments on commit c165d78

Please sign in to comment.