diff --git a/include/asyncpp/task.hpp b/include/asyncpp/task.hpp index 77ff137..2e9021e 100644 --- a/include/asyncpp/task.hpp +++ b/include/asyncpp/task.hpp @@ -21,7 +21,7 @@ namespace impl_task { constexpr bool await_ready() const noexcept { return false; } void await_suspend(std::coroutine_handle handle) const noexcept { auto& owner = handle.promise(); - owner.m_event.set(owner.m_result); + owner.m_event.set(std::move(owner.m_result)); auto self = std::move(owner.m_self); // owner.m_self.reset() call method on owner after it's been deleted. self.reset(); } diff --git a/test/test_generator.cpp b/test/test_generator.cpp index c6636a5..a574255 100644 --- a/test/test_generator.cpp +++ b/test/test_generator.cpp @@ -33,4 +33,19 @@ TEST_CASE("Generator: sequence of references", "[Generator]") { values.push_back(item++); } REQUIRE(values == std::vector{ 0, 2, 4, 6 }); +} + + +TEST_CASE("Generator: sequence of mvoeables", "[Generator]") { + static const auto coro = [](int count) -> generator> { + for (int i = 0; i < count; ++i) { + co_yield std::make_unique(i); + } + }; + const auto g = coro(4); + std::vector values; + for (auto& item : g) { + values.push_back(*item); + } + REQUIRE(values == std::vector{ 0, 1, 2, 3 }); } \ No newline at end of file diff --git a/test/test_stream.cpp b/test/test_stream.cpp index 2b146c4..ef525d9 100644 --- a/test/test_stream.cpp +++ b/test/test_stream.cpp @@ -71,6 +71,17 @@ TEST_CASE("Stream: data types", "[Stream]") { }(); REQUIRE(r.get_counters().done); } + SECTION("moveable") { + static const auto coro = []() -> stream> { + co_yield std::make_unique(42); + }; + auto r = []() -> monitor_task { + auto s = coro(); + auto item = co_await s; + REQUIRE(**item == 42); + }(); + REQUIRE(r.get_counters().done); + } SECTION("exception") { static const auto coro = []() -> stream { throw std::runtime_error("test"); diff --git a/test/test_task.cpp b/test/test_task.cpp index c6907fa..4dff269 100644 --- a/test/test_task.cpp +++ b/test/test_task.cpp @@ -149,7 +149,6 @@ TEMPLATE_TEST_CASE("Task: co_await ref", "[Task]", task, shared_task TEMPLATE_TEST_CASE("Task: co_await void", "[Task]", task, shared_task) { - static int value = 42; static const auto coro = []() -> TestType { co_return; }; @@ -161,6 +160,32 @@ TEMPLATE_TEST_CASE("Task: co_await void", "[Task]", task, shared_task>; + static const auto coro = [](std::unique_ptr value) -> TaskType { + co_return value; + }; + static const auto enclosing = [](std::unique_ptr value) -> TaskType { + co_return co_await coro(std::move(value)); + }; + auto tsk = enclosing(std::make_unique(42)); + REQUIRE((*join(tsk)) == 42); +} + + +TEST_CASE("Task: co_await moveable -- shared_task", "[Task]") { + using TaskType = task>; + static const auto coro = [](std::unique_ptr value) -> TaskType { + co_return value; + }; + static const auto enclosing = [](std::unique_ptr value) -> TaskType { + co_return std::move(co_await coro(std::move(value))); + }; + auto tsk = enclosing(std::make_unique(42)); + REQUIRE((*join(tsk)) == 42); +} + + TEMPLATE_TEST_CASE("Task: co_await exception", "[Task]", task, shared_task) { static int value = 42; static const auto coro = []() -> TestType {