Skip to content

Commit

Permalink
fixed out of scope error for eager self-posts of generators.
Browse files Browse the repository at this point in the history
closes #142.
  • Loading branch information
klemens-morgenstern committed Dec 5, 2023
1 parent 7884039 commit e42f17e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions include/boost/cobalt/detail/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define BOOST_COBALT_DETAIL_GENERATOR_HPP

#include <boost/cobalt/concepts.hpp>
#include <boost/cobalt/error.hpp>
#include <boost/cobalt/result.hpp>
#include <boost/cobalt/detail/exception.hpp>
#include <boost/cobalt/detail/forward_cancellation.hpp>
Expand Down Expand Up @@ -269,8 +270,16 @@ struct generator_receiver : generator_receiver_base<Yield, Push>
if (self->yield_from != nullptr && !self->lazy)
{
auto exec = self->yield_from->get_executor();
auto alloc = asio::get_associated_allocator(self->yield_from);
asio::post(
std::move(exec), std::exchange(self->yield_from, nullptr));
std::move(exec),
asio::bind_allocator(
alloc,
[y = std::exchange(self->yield_from, nullptr)]() mutable
{
if (y->receiver) // make sure we only resume eagerly when attached to a generator object
std::move(y)();
}));
}

return {system::in_place_value, self->get_result()};
Expand Down Expand Up @@ -490,8 +499,9 @@ struct generator_yield_awaitable
return res;
}

Push await_resume()
Push await_resume(const boost::source_location & loc = BOOST_CURRENT_LOCATION)
{
BOOST_ASSERT(self->receiver);
BOOST_ASSERT(self->receiver->pushed_value);
return *std::exchange(self->receiver->pushed_value, std::nullopt);
}
Expand Down
15 changes: 15 additions & 0 deletions test/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,19 @@ CO_TEST_CASE(detached_)
co_return;
}

cobalt::generator<int, int> detached_push()
{
int i = co_yield 42;
while (true)
i = co_yield i;
co_return i;
}

CO_TEST_CASE(detached_push_)
{
auto g = detached_push();

co_await g(1);
}

BOOST_AUTO_TEST_SUITE_END();

0 comments on commit e42f17e

Please sign in to comment.