Skip to content

Add async::generator<T> #5

@kammce

Description

@kammce

Below is an underlying way to understand each of the coroutine types and what states they can be in.

class future {
  std::variant<std::coroutine_handle, T> m_state;
};

class task {
  std::coroutine_handle m_handle;
};

For future, its either a coroutine or some value T. Once a future becomes a T, it is finished and no more work or resumption can occur.
For task, its expected to last as long as it needs to and if it completes, then its handle will becomes std::noop_coroutine_handle.
For generator, the generator can last for every or complete. Completion is determined when the handle becomes a std::noop_coroutine_handle. Generator provides APIs for accessing the value when its yielded. When a value is yielded, the value is returned and the internal state removes that value such that asking for a value again will yield an empty optional.

template<typename T>
class generator {
  
  void resume();
  bool has_value();
  std::optional<T> get_value();
  std::optional<T> get_next_value();

  std::coroutine_handle m_handle;
  std::optional<T> m_data;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions