-
-
Notifications
You must be signed in to change notification settings - Fork 6
generator: add async generator helper #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #pragma once | ||
|
|
||
| #include <async/basic.hpp> | ||
| #include <optional> | ||
| #include <utility> | ||
|
|
||
| namespace async { | ||
|
|
||
| template <typename T> | ||
| struct generator { | ||
| struct promise_type; | ||
| using handle_type = corons::coroutine_handle<promise_type>; | ||
|
|
||
| struct continuation { | ||
| virtual void complete() = 0; | ||
| }; | ||
|
|
||
| struct promise_type { | ||
| std::optional<T> current_value; | ||
| continuation *cont = nullptr; | ||
|
|
||
| generator get_return_object() { | ||
| return generator{corons::coroutine_handle<promise_type>::from_promise(*this)}; | ||
| } | ||
|
|
||
| corons::suspend_always initial_suspend() noexcept { return {}; } | ||
|
|
||
| struct yield_awaiter { | ||
| bool await_ready() noexcept { return false; } | ||
|
|
||
| void await_suspend(handle_type h) noexcept { | ||
| auto cont = h.promise().cont; | ||
| h.promise().cont = nullptr; | ||
| if (cont) { | ||
| cont->complete(); | ||
| return; | ||
| } | ||
| FRG_INTF(panic)("Generator yielded but no consumer is waiting"); | ||
| } | ||
|
|
||
| void await_resume() noexcept {} | ||
| }; | ||
|
|
||
| yield_awaiter yield_value(T value) noexcept { | ||
| current_value = std::move(value); | ||
| return yield_awaiter{}; | ||
| } | ||
|
|
||
| yield_awaiter final_suspend() noexcept { | ||
| return yield_awaiter{}; | ||
| } | ||
|
|
||
| void return_void() {} | ||
|
|
||
| void unhandled_exception() { | ||
| FRG_INTF(panic)("Unhandled exception in generator coroutine"); | ||
| } | ||
| }; | ||
|
|
||
| explicit generator(corons::coroutine_handle<promise_type> h) : h_(h) {} | ||
|
|
||
| generator(generator &&other) : h_(std::exchange(other.h_, nullptr)) {} | ||
|
|
||
| generator &operator=(generator &&other) { | ||
| auto h = std::exchange(other.h_, nullptr); | ||
| if (h_) | ||
| h_.destroy(); | ||
| h_ = h; | ||
| return *this; | ||
| } | ||
|
|
||
| ~generator() { | ||
| if (h_) | ||
| h_.destroy(); | ||
| } | ||
|
|
||
| template <typename Receiver> | ||
| struct next_operation : continuation { | ||
| next_operation(handle_type h, Receiver r) | ||
| : h_{h}, r_{std::move(r)} {} | ||
|
|
||
| void start() { | ||
| h_.promise().cont = this; | ||
| h_.resume(); | ||
| } | ||
|
|
||
| void complete() override { | ||
| auto val = std::move(h_.promise().current_value); | ||
| h_.promise().current_value = {}; | ||
| execution::set_value(std::move(r_), std::move(val)); | ||
| } | ||
|
|
||
| handle_type h_; | ||
| Receiver r_; | ||
| }; | ||
|
|
||
| struct next_sender { | ||
| handle_type h_; | ||
| using value_type = std::optional<T>; | ||
|
|
||
| friend sender_awaiter<next_sender, next_sender::value_type> operator co_await (next_sender s) { | ||
| return {s}; | ||
| } | ||
|
|
||
| template <typename Receiver> | ||
| next_operation<Receiver> connect(Receiver r) { | ||
| return {h_, std::move(r)}; | ||
| } | ||
| }; | ||
|
|
||
| next_sender next() { | ||
| return {h_}; | ||
| } | ||
|
|
||
| private: | ||
| corons::coroutine_handle<promise_type> h_; | ||
| }; | ||
|
|
||
| } // namespace async | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include <async/generator.hpp> | ||
| #include <async/basic.hpp> | ||
| #include <async/result.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <memory> | ||
|
|
||
| namespace { | ||
|
|
||
| async::generator<int> generate_nothing() { | ||
| co_return; | ||
| } | ||
|
|
||
| async::generator<int> generate_ints() { | ||
| co_yield 1; | ||
| co_yield 2; | ||
| co_yield 3; | ||
| } | ||
|
|
||
| async::generator<std::unique_ptr<int>> generate_unique_ptrs() { | ||
| co_yield std::make_unique<int>(1); | ||
| co_yield std::make_unique<int>(2); | ||
| co_yield std::make_unique<int>(3); | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| TEST(Generator, YieldNothing) { | ||
| async::run([]() -> async::result<void> { | ||
| auto gen = generate_nothing(); | ||
|
|
||
| auto v = co_await gen.next(); | ||
| EXPECT_FALSE(v.has_value()); | ||
| }()); | ||
| } | ||
|
|
||
| TEST(Generator, YieldInts) { | ||
| async::run([]() -> async::result<void> { | ||
| auto gen = generate_ints(); | ||
|
|
||
| auto v1 = co_await gen.next(); | ||
| EXPECT_TRUE(v1.has_value()); | ||
| EXPECT_EQ(*v1, 1); | ||
|
|
||
| auto v2 = co_await gen.next(); | ||
| EXPECT_TRUE(v2.has_value()); | ||
| EXPECT_EQ(*v2, 2); | ||
|
|
||
| auto v3 = co_await gen.next(); | ||
| EXPECT_TRUE(v3.has_value()); | ||
| EXPECT_EQ(*v3, 3); | ||
|
|
||
| auto v4 = co_await gen.next(); | ||
| EXPECT_FALSE(v4.has_value()); | ||
| }()); | ||
| } | ||
|
|
||
| TEST(Generator, YieldMoveOnly) { | ||
| async::run([]() -> async::result<void> { | ||
| auto gen = generate_unique_ptrs(); | ||
|
|
||
| auto v1 = co_await gen.next(); | ||
| EXPECT_TRUE(v1.has_value()); | ||
| EXPECT_EQ(**v1, 1); | ||
|
|
||
| auto v2 = co_await gen.next(); | ||
| EXPECT_TRUE(v2.has_value()); | ||
| EXPECT_EQ(**v2, 2); | ||
|
|
||
| auto v3 = co_await gen.next(); | ||
| EXPECT_TRUE(v3.has_value()); | ||
| EXPECT_EQ(**v3, 3); | ||
|
|
||
| auto v4 = co_await gen.next(); | ||
| EXPECT_FALSE(v4.has_value()); | ||
| }()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.