Skip to content
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

Need more return types #49

Closed
luncliff opened this issue Mar 12, 2020 · 0 comments · Fixed by #50
Closed

Need more return types #49

luncliff opened this issue Mar 12, 2020 · 0 comments · Fixed by #50
Assignees

Comments

@luncliff
Copy link
Owner

luncliff commented Mar 12, 2020

Considering combination of initial_suspend/final_suspend, we need at least 4 return types.

Idea

I'd like to reuse existing types for coroutines' return whenever possible.

  • How about using *_frame_t for controlled destruction?
    (final suspend returns suspend_always)
  • ...?

std::nullptr_t/void

Something like null_frame_t. (previously no_return)

  • initial: suspend_never
  • final: suspend_never
#include <coroutine/return.h>

auto impl() -> std::nullptr_t {
    co_await std::suspend_never{};
}
void usage(){
    impl();
}

Or if compiler allows, void can be an option. (GCC-10 requires the type must be class)

passive_frame_t

Usually for manual start(initiate) / cleanup(destroy) of the frame.

  • initial: suspend_always
  • final: suspend_always
#include <coroutine/return.h>

[[nodiscard]] auto impl() -> coro::passive_frame_t {
    co_await std::suspend_never{};
}
void usage(){
    std::coroutine_handle<void> rh = impl();
    rh.resume(); // resume at the initial suspend point
    // ...
    if(rh.done()) // reserved after final suspend point
        rh.destroy();
}

std::packaged_task<R(P, ...)>

Lazy execution of the coroutine, especially to allow control of execution cost. (For future support of Concurrency TS or Executor TS)

  • initial: suspend_always
  • final: suspend_never
#include <coroutine/return.h>

[[nodiscard]] auto impl(int a, int b) -> std::packaged_task<int(void)> {
    co_await std::suspend_never{};
    co_return a + b;
}
void usage(){
    auto task = impl(1, 1);
    auto f = task.get_future();
    task();
    assert(f.get() == 2);
}
Concerns

std::packaged_task<R(P, ...)> requires std::future<R> support.
It is highly possible to be heavy more than necessary.

frame_t

Controlled destruction of the coroutine frame.

  • initial: suspend_never
  • final: suspend_always
#include <coroutine/return.h>

[[nodiscard]] auto impl() -> coro::frame_t {
    co_await std::suspend_never{};
}
void usage(){
    std::coroutine_handle<void> rh = impl();
    if(rh.done())
        rh.destroy();
}
@luncliff luncliff added this to the 1.5 milestone Mar 12, 2020
@luncliff luncliff self-assigned this Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant