In Rust, asynchronous functions do not require allocating on the heap. Function async
returns a compiler-generated structure on which you call a compiler generated poll
method. The design of async looks logical and clear.
The design of coroutines in C++ is strange. It forces you to do allocation on the heap, because you have no other way to return something using co_return
.
(It is possible to make a custom allocator that will make allocations in a buffer on the stack, but this will unnecessarily complicate the code.)
Why was it decided in the design of C++ that an object returned by a coroutine must have a promise_type
?
Why is await_ready
, await_suspend
, await_resume
not enough?
(It looks strange, and this is what enforces you to do allocation; you can't just directly construct a SomeTask<T>
object (with three await_*
methods) and return it.)
How can we make a zero-alloc coroutines without a custom allocator?
co_await
Or am I missing something? If I'm right, I wanted to know if there is a way to avoid such inconveniences when writing code. – Sachiko