Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从 boost.cobalt 学的 awaitable<T> 模板特化方法 #17

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

microcai
Copy link
Member

@microcai microcai commented Oct 14, 2024

awaitable<T> 和 awaitable<void> 行为逻辑略有不同。

最初使用的是特化 awaitable_promise_type<void> 的方式

现在改为不特化 awaitable, 也不特化 awaitable_promise_type,
而是将针对 T 不同的部分,作为 awaitable_promise_value<T>
然后特化 awaitable_promise_value<void> 即可。

这样需要被特化的代码量有所减少。

@microcai microcai requested a review from Jackarain October 14, 2024 10:56
@microcai
Copy link
Member Author

看来编译出错了,我再改改!

@microcai microcai force-pushed the new_awaitable-template-handle branch 2 times, most recently from 4ba6fad to ff929de Compare October 14, 2024 11:58
@microcai microcai requested review from Jackarain and removed request for Jackarain October 14, 2024 11:59
@microcai
Copy link
Member Author

这次改对了。 ctest 成功

@microcai microcai force-pushed the new_awaitable-template-handle branch 2 times, most recently from f58ddc6 to a9283f4 Compare October 14, 2024 13:07
@microcai microcai changed the title 新的 awaitable<T> 模板特化方法 从 boost.cobalt 学的 awaitable<T> 模板特化方法 Oct 14, 2024
@microcai microcai force-pushed the new_awaitable-template-handle branch 2 times, most recently from 9c7054d to 599dfc2 Compare October 14, 2024 13:50
Copy link
Member

@Jackarain Jackarain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果能解释或修改这2个小问题,我觉得就可以合并了

include/ucoro/awaitable.hpp Show resolved Hide resolved
@@ -381,23 +354,24 @@ namespace ucoro
}
}

template <typename PromiseType>
auto await_suspend(std::coroutine_handle<PromiseType> continuation)
template <typename OT>
Copy link
Member

@Jackarain Jackarain Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OT 这个类型命名是有什么考虑,还是随意的命名?
如果是随意的,应该考虑用一个更有意义且看起来更具普遍性的类型命名。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是随意的命名。主要是 T 这种通用名被外面的模板类占用了。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有特别的原因,我的建议是继续使用之前的类型名称 PromiseType

Copy link
Member Author

@microcai microcai Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前的 PromiseType 还得在里面加约束,PromiseType 必须得是 awaitable<T> 的一种。不然并不能用这个代码。因为别人的 promise 类型可没 local_ 这个对象。

Copy link
Member Author

@microcai microcai Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果使用 std::coroutine_handle ,还得加 requires ( is_promise_type <PromiseType> )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不加约束是不行的。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你用 OT 的时候,我也没看到你有加约束啊

Copy link
Member Author

@microcai microcai Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要么非要用 std::coroutine_handle<PromiseType> continuation 这样的写法,然后给 PromiseType 上 concept 约束。
要么用 std::coroutine_handle<awaitable_type<AnyType>> continuation 这样只写法。让编译器自动约束。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你用 OT 的时候,我也没看到你有加约束啊

你没解释我这个问题

Copy link
Member Author

@microcai microcai Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不加约束编译测试没通过,重新 push 了个加约束的版本了。

原来的代码是用

if constexpr (std::is_base_of_v<awaitable_promise_base, PromiseType>)

这个加约束的。 因为 awaitable_promise_base 没了,所以这个检查就不行了。所以要换个方式写约束。
这里是必须要有约束的呀。

@microcai microcai force-pushed the new_awaitable-template-handle branch 5 times, most recently from ead41af to c4f25dc Compare October 14, 2024 15:08
awaitable<T> 和 awaitable<void> 行为逻辑略有不同。

最初使用的是特化 awaitable_promise_type<void> 的方式

现在改为不特化 awaitable, 也不特化 awaitable_promise_type,
而是将针对 T 不同的部分,作为 awaitable_promise_value<T>
然后特化 awaitable_promise_value<void> 即可。

这样需要被特化的代码量有所减少。
@microcai microcai force-pushed the new_awaitable-template-handle branch from c4f25dc to 75200a2 Compare October 14, 2024 15:17
@@ -381,23 +354,24 @@ namespace ucoro
}
}

template <typename PromiseType>
template <typename AnyType, typename PromiseType = awaitable_promise<AnyType>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template <typename AnyType, typename PromiseType = awaitable_promise<AnyType>>
这里是多此一举的,我的意思怎么还不能明白?
template <typename PromiseType>
这样就就可以了,而不是随意的名字 OT 或 AnyType

@@ -381,23 +354,24 @@ namespace ucoro
}
}

template <typename PromiseType>
auto await_suspend(std::coroutine_handle<PromiseType> continuation)
template <typename OT>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你的 OT 就不失常?为什么改成 PromiseType 就失常了?

@@ -381,23 +354,24 @@ namespace ucoro
}
}

template <typename PromiseType>
auto await_suspend(std::coroutine_handle<PromiseType> continuation)
template <typename OT>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你用 OT 的时候,我也没看到你有加约束啊

@@ -381,23 +354,24 @@ namespace ucoro
}
}

template <typename PromiseType>
auto await_suspend(std::coroutine_handle<PromiseType> continuation)
template <typename OT>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你用 OT 的时候,我也没看到你有加约束啊

你没解释我这个问题

@Jackarain Jackarain merged commit 405b1ef into master Oct 14, 2024
6 checks passed
@Jackarain Jackarain deleted the new_awaitable-template-handle branch October 14, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants