Skip to content

Commit 2b50ab4

Browse files
authored
Merge pull request #22 from sjinks/remove-sync-generator-adapter
fix: remove `sync_generator_adapter`
2 parents b249844 + 82ceffd commit 2b50ab4

10 files changed

+4
-480
lines changed

README.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ The documentation is available at [https://sjinks.github.io/coro-cpp/](https://s
8787

8888
### Eager Coroutine (`eager_task`)
8989

90-
An *eager coroutine* start execution immediately upon creation and does not suspend upon completion. From the caller's perspective,
91-
it runs synchoronously; the control is returned to the caller only after the coroutine finishes.
90+
An *eager coroutine* (or *hot-start coroutine*) starts execution immediately upon creation and keeps running until the first suspending `co_await`.
9291

9392
```cpp
9493
#include <wwa/coro/eager_task.h>
@@ -135,7 +134,7 @@ wwa::coro::task<> print()
135134
}
136135
```
137136

138-
It is possible to turn any task (or any awaitable) into a fire-anf-forget eager coroutine. For the example above,
137+
It is possible to turn any task (or any awaitable) into a fire-and-forget eager coroutine. For the example above,
139138

140139
```cpp
141140
#include <wwa/coro/eager_task.h>
@@ -226,28 +225,3 @@ while (it != end) {
226225
See [examples/async_generator.cpp](https://github.com/sjinks/coro-cpp/blob/master/examples/async_generator.cpp).
227226
228227
Unfortunately, it is impossible to use asynchronous iterators directly in range-based `for` loops.
229-
However, this limitation can be overcome with the help of eager coroutines.
230-
231-
There is a helper adapter, `sync_generator_adapter<T>`, that turns an asynchronous generator into a quasi-synchronous one:
232-
233-
```cpp
234-
#include <wwa/coro/async_generator.h>
235-
#include <wwa/coro/sync_generator_adapter.h>
236-
237-
wwa::coro::async_generator<int> async_iota(int start = 0)
238-
{
239-
for (int i = start;; ++i) {
240-
co_yield i;
241-
}
242-
}
243-
244-
// ...
245-
246-
wwa::coro::sync_generator_adapter<int> sync_iota(async_iota(10));
247-
248-
for (auto& n : sync_iota | std::views::take(5)) {
249-
std::cout << n << "\n";
250-
}
251-
```
252-
253-
See [examples/sync_generator_adapter.cpp](https://github.com/sjinks/coro-cpp/blob/master/examples/sync_generator_adapter.cpp).

examples/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ target_compile_features(eager_task PRIVATE cxx_std_20)
1212
add_executable(run_awaitable run_awaitable.cpp)
1313
target_compile_features(run_awaitable PRIVATE cxx_std_20)
1414

15-
add_executable(sync_generator_adapter sync_generator_adapter.cpp)
16-
target_compile_features(sync_generator_adapter PRIVATE cxx_std_20)
17-
1815
add_executable(async_generator async_generator.cpp)
1916
target_compile_features(async_generator PRIVATE cxx_std_20)
2017

examples/advance_with_begin.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "async_generator.h"
55
#include "eager_task.h"
66
#include "generator.h"
7-
#include "sync_generator_adapter.h"
87
#include "task.h"
98

109
namespace {
@@ -62,22 +61,6 @@ void iterate_over_sync()
6261
std::cout << "\n";
6362
}
6463

65-
void iterate_over_adapted_async()
66-
{
67-
//! [Iterate over adapted asynchronous generator with begin()]
68-
auto async = async_first_n(5);
69-
auto gen = wwa::coro::sync_generator_adapter(std::move(async));
70-
auto it = gen.begin();
71-
auto end = gen.end();
72-
73-
do {
74-
std::cout << *it << " ";
75-
} while (gen.begin() != end);
76-
//! [Iterate over adapted asynchronous generator with begin()]
77-
78-
std::cout << "\n";
79-
}
80-
8164
} // namespace
8265

8366
int main()
@@ -86,16 +69,12 @@ int main()
8669
iterate_over_async();
8770
std::cout << "Iterating over synchronous generator:\n";
8871
iterate_over_sync();
89-
std::cout << "Iterating over adapted asynchronous generator:\n";
90-
iterate_over_adapted_async();
9172

9273
// Expected output:
9374
// Iterating over asynchronous generator:
9475
// 0 1 2 3 4
9576
// Iterating over synchronous generator:
9677
// 0 1 2 3 4
97-
// Iterating over adapted asynchronous generator:
98-
// 0 1 2 3 4
9978

10079
return 0;
10180
}

examples/sync_generator_adapter.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ target_sources(
2828
eager_task.h
2929
exceptions.h
3030
generator.h
31-
sync_generator_adapter.h
3231
task.h
3332
)
3433

src/async_generator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
* Example usage:
1414
* @snippet async_generator.cpp asynchronous generator example
1515
* @snippet{trimleft} async_generator.cpp asynchronous generator usage
16-
*
17-
* With the help of `sync_generator_adapter`, you can adapt an asynchronous generator to use it in a synchronous context
18-
* (e.g., range-based for loops or views).
1916
*/
2017

2118
#include <coroutine>

src/eager_task.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ namespace wwa::coro {
2222
/**
2323
* @brief Eager coroutine.
2424
*
25-
* This class represents an eager coroutine, which starts execution immediately upon creation and does not suspend upon completion.
26-
* From the caller's perspective, it runs synchoronously; the control is returned to the caller only after the coroutine finishes.
25+
* This class represents an eager coroutine, which starts execution immediately upon creation and and keeps running until the first suspending `co_await`.
2726
*
2827
* Example:
2928
* @snippet eager_task.cpp eager_task example
@@ -134,7 +133,7 @@ eager_task run_awaitable(Awaitable&& f, Args&&... args)
134133

135134
/**
136135
* @example run_awaitable.cpp
137-
* Shows how to use `run_awaitable` to turn any awaitable into an eager coroutine that runs synchronously from the caller's perspective.
136+
* Shows how to use `run_awaitable` to turn any awaitable into an eager coroutine.
138137
*/
139138

140139
} // namespace wwa::coro

0 commit comments

Comments
 (0)