Skip to content

Commit b57d518

Browse files
[fix] optional access err in with.
1 parent aa8ad06 commit b57d518

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

include/boost/cobalt/detail/with.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ struct with_promise_value
4141
result.emplace(std::move(*value));
4242
}
4343

44-
T get_result()
44+
std::optional<T> get_result()
4545
{
46-
return std::move(result).value();
46+
return std::move(result);
4747
}
4848
};
4949

@@ -117,12 +117,12 @@ template<typename T>
117117
T with_impl<T>::await_resume()
118118
{
119119
auto e = promise.e;
120-
auto res = std::move(promise.get_result());
120+
auto res = promise.get_result();
121121
std::coroutine_handle<promise_type>::from_promise(promise).destroy();
122122
if (e)
123123
std::rethrow_exception(e);
124124

125-
return std::move(res);
125+
return *std::move(res);
126126
}
127127

128128
template<>

test/with.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ CO_TEST_CASE(async)
9191
BOOST_CHECK_THROW(
9292
co_await boost::cobalt::with (
9393
&ft,
94-
[](finalizer_test * ) -> boost::cobalt::promise<void>
94+
[](finalizer_test * t) -> boost::cobalt::promise<void>
9595
{
96+
co_await asio::post(t->exec);
9697
throw std::runtime_error("42");
9798
co_return;
9899
},
@@ -146,4 +147,46 @@ CO_TEST_CASE(async_int)
146147
}
147148

148149

150+
CO_TEST_CASE(sync_int_throw)
151+
{
152+
finalizer_test ft{co_await boost::cobalt::this_coro::executor};
153+
154+
BOOST_CHECK_THROW(
155+
co_await boost::cobalt::with (
156+
&ft,
157+
[](finalizer_test *)
158+
{
159+
throw std::runtime_error("test");
160+
return 23;
161+
},
162+
[](finalizer_test * ft, std::exception_ptr &e)
163+
{
164+
return ft->exit(e);
165+
}), std::runtime_error);
166+
167+
168+
BOOST_CHECK(ft.e != nullptr);
169+
}
170+
171+
CO_TEST_CASE(async_int_throw)
172+
{
173+
finalizer_test ft{co_await boost::cobalt::this_coro::executor};
174+
175+
BOOST_CHECK_THROW(
176+
co_await boost::cobalt::with (
177+
&ft,
178+
[](finalizer_test * ) -> boost::cobalt::promise<int>
179+
{
180+
throw std::runtime_error("test");
181+
co_return 42;
182+
},
183+
[](finalizer_test * ft, std::exception_ptr &e)
184+
{
185+
return ft->exit(e);
186+
}), std::runtime_error);
187+
188+
BOOST_CHECK(ft.e != nullptr);
189+
}
190+
191+
149192
BOOST_AUTO_TEST_SUITE_END();

0 commit comments

Comments
 (0)