@@ -20,19 +20,6 @@ struct promise_type;
2020
2121// =============== Task =============== //
2222
23- /* *
24- * @brief `std::unique_ptr` compatible deleter for coroutine promises.
25- */
26- struct promise_deleter {
27- template <typename T>
28- constexpr static void operator ()(T *ptr) noexcept {
29- std::coroutine_handle<T>::from_promise (*ptr).destroy ();
30- }
31- };
32-
33- template <typename T>
34- using unique_promise = std::unique_ptr<T, promise_deleter>;
35-
3623/* *
3724 * @brief The return type for libfork's async functions/coroutines.
3825 *
@@ -50,7 +37,9 @@ using unique_promise = std::unique_ptr<T, promise_deleter>;
5037 * \endrst
5138 */
5239export template <returnable T, alloc_mixin Stack>
53- struct task final : unique_promise<promise_type<T, Stack>> {};
40+ struct task {
41+ promise_type<T, Stack> *promise;
42+ };
5443
5544// =============== Frame-mixin =============== //
5645
@@ -61,13 +50,11 @@ constexpr auto final_suspend(frame_type *frame) -> std::coroutine_handle<> {
6150
6251 frame_type *parent_frame = frame->parent ;
6352
64- {
65- // Destroy the child frame
66- unique_promise<frame_type> _{frame};
67- }
53+ // Destroy the child frame
54+ frame->handle ().destroy ();
6855
6956 if (parent_frame != nullptr ) {
70- return std::coroutine_handle<frame_type>:: from_promise (*parent_frame );
57+ return parent_frame-> handle ( );
7158 }
7259
7360 return std::noop_coroutine ();
@@ -81,22 +68,19 @@ struct final_awaitable : std::suspend_always {
8168 }
8269};
8370
84- // TODO: can we type-erase T/Policy here?
85-
86- template <typename T, alloc_mixin StackPolicy>
8771struct just_awaitable : std::suspend_always {
8872
89- task<T, StackPolicy> child;
73+ frame_type * child;
9074
9175 template <typename ... Us>
9276 auto await_suspend (std::coroutine_handle<promise_type<Us...>> parent) noexcept -> std::coroutine_handle<> {
9377
9478 LF_ASSUME (child != nullptr );
95- LF_ASSUME (child->frame . parent == nullptr );
79+ LF_ASSUME (child->parent == nullptr );
9680
97- child->frame . parent = &parent.promise ().frame ;
81+ child->parent = &parent.promise ().frame ;
9882
99- return child. release () ->handle ();
83+ return child->handle ();
10084 }
10185};
10286
@@ -116,8 +100,8 @@ struct mixin_frame {
116100 // === Called by the compiler === //
117101
118102 template <alloc_mixin P>
119- static constexpr auto await_transform(task<void , P> child) -> just_awaitable<void, P> {
120- return {.child = std::move ( child) };
103+ constexpr static auto await_transform(task<void , P> child) noexcept -> just_awaitable {
104+ return {.child = & child. promise -> frame };
121105 }
122106
123107 constexpr static auto initial_suspend () noexcept -> std::suspend_always { return {}; }
@@ -136,7 +120,7 @@ struct promise_type<void, StackPolicy> : StackPolicy, mixin_frame {
136120
137121 frame_type frame;
138122
139- constexpr auto get_return_object () -> task<void, StackPolicy> { return {{ this , {}} }; }
123+ constexpr auto get_return_object () -> task<void, StackPolicy> { return {. promise = this }; }
140124
141125 constexpr static void return_void () {}
142126};
0 commit comments