generics - 如何缩短长的where子句?
问题描述
我有这个声明:
impl<T: JobContext, R> TaskProcessor<T, R>
where
R: hyper::service::Service<hyper::client::connect::dns::Name> + Clone + Send + Sync + 'static,
R::Response: std::iter::Iterator<Item = std::net::SocketAddr>,
R::Future: Send,
R::Error: std::error::Error + Send + Sync,
{
}
我想将条件转移where
到它自己的特征中,这样我就可以写如下内容:
impl<T: JobContext, R: DnsResolve> TaskProcessor<T, R> { }
做这个的最好方式是什么?
解决方案
以下解决了我的问题
trait DnsResolve: hyper::service::Service<hyper::client::connect::dns::Name, Response: std::iter::Iterator<Item = std::net::SocketAddr>, Future: Send, Error: std::error::Error + Send + Sync> + Clone + Send + Sync + 'static
{
}
impl<T> DnsResolve for T where T: hyper::service::Service<hyper::client::connect::dns::Name, Response: std::iter::Iterator<Item = std::net::SocketAddr>, Future: Send, Error: std::error::Error + Send + Sync> + Clone + Send + Sync + 'static,
{
}
但这使用了不稳定的功能
https://rust-lang.github.io/rfcs/2289-associated-type-bounds.html
https://github.com/rust-lang/rust/issues/52662
而且我不得不使用 nightly rustc 编译我的代码......我不太喜欢
这个话题对我来说很复杂,有什么办法可以绕过稳定生锈的相关类型界限吗?
推荐阅读
- django - Django - 仅在表单更改时显示消息
- javascript - 样式化组件:在滚动时传递状态
- python - 在 PySimpleGUI 中集成 Pygame
- python - 如何根据 txt 文件中的 url 从多个页面中抓取文本正文
- ios - iOS:如何从我现有的项目中制作一个 pod?代码
- laravel - 如何在 Laravel 8 中使用 CKEditor 5 RTL
- android - 为什么 ?。尽管实例不可为空,但在调用方法时是不可避免的吗?
- .net-core - 如何从 VS Web 部署转换为命令行 MS 部署
- javascript - 在用户尝试登录之前发送 401 错误是否正确?
- javascript - 动态更改导航器媒体约束