From fdb7a74f63de79712d9f67e7f9b9356cc9248620 Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Sun, 11 Oct 2020 00:18:10 +0200 Subject: [PATCH] translate "for co_await" to regular for loops using "co_await" "for co_await" was in the coroutine-ts but did not make it into the C++20 standard. This patch translates the "for co_await" which were used to standard for loops using "co_await". This is necessary to compile on MSVC 1928 in with c++-latest. --- include/cppcoro/resume_on.hpp | 3 ++- test/async_generator_tests.cpp | 3 ++- test/scheduling_operator_tests.cpp | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/cppcoro/resume_on.hpp b/include/cppcoro/resume_on.hpp index 4bb1d7c..b26188b 100644 --- a/include/cppcoro/resume_on.hpp +++ b/include/cppcoro/resume_on.hpp @@ -117,8 +117,9 @@ namespace cppcoro template async_generator resume_on(SCHEDULER& scheduler, async_generator source) { - for co_await(auto& value : source) + for (auto iter = co_await source.begin(); iter != source.end(); co_await ++iter) { + auto& value = *iter; co_await scheduler.schedule(); co_yield value; } diff --git a/test/async_generator_tests.cpp b/test/async_generator_tests.cpp index 3f0c4af..6390555 100644 --- a/test/async_generator_tests.cpp +++ b/test/async_generator_tests.cpp @@ -271,8 +271,9 @@ TEST_CASE("large number of synchronous completions doesn't result in stack-overf auto consumer = [](cppcoro::async_generator sequence) -> cppcoro::task<> { std::uint32_t expected = 0; - for co_await(std::uint32_t i : sequence) + for (auto iter = co_await sequence.begin(); iter != sequence.end(); co_await ++iter) { + std::uint32_t i = *iter; CHECK(i == expected++); } diff --git a/test/scheduling_operator_tests.cpp b/test/scheduling_operator_tests.cpp index 238eb75..19d666f 100644 --- a/test/scheduling_operator_tests.cpp +++ b/test/scheduling_operator_tests.cpp @@ -87,8 +87,9 @@ TEST_CASE_FIXTURE(io_service_fixture, "schedule_on async_generator<> function") auto seq = schedule_on(io_service(), makeSequence()); int expected = 1; - for co_await(int value : seq) + for (auto iter = co_await seq.begin(); iter != seq.end(); co_await ++iter) { + int value = *iter; CHECK(value == expected++); // Transfer exection back to main thread before @@ -177,8 +178,9 @@ TEST_CASE_FIXTURE(io_service_fixture, "resume_on async_generator<> function" auto seq = resume_on(otherIoService, makeSequence()); int expected = 1; - for co_await(int value : seq) + for (auto iter = co_await seq.begin(); iter != seq.end(); co_await ++iter) { + int value = *iter; // Every time we receive a value it should be on our requested // scheduler (ie. main thread) CHECK(std::this_thread::get_id() == mainThreadId);