问题描述
Hyper的HttpConnector
和hyper_tls的HttpsConnector
显然实现了一个或多个共同特征。我已经研究了它们的实现,但是我还无法提出正确的类型(或别名),以致无法将任一struct的实例传递给下面的程序中的函数。看来Tower::Service<Url>
是正确的特质,但我未能给出正确的答案,确实可以使用帮助。
use hyper::{client::connect::HttpConnector,service::Service,Uri};
use hyper_tls::HttpsConnector;
// What type should param c have here for this program to compile?
fn with_connector(c: ????) {}
fn main() {
with_connector(HttpConnector::new());
with_connector(HttpsConnector::new());
}
有关的实现:
[dependencies]
hyper = "0.13"
hyper-tls = "0.4.3"
用例是我正在编写自己的连接器,该连接器将包含其中任一实例的代表。预先感谢。
解决方法
我能够通过复制两个连接器共有的通用类型定义来解决此问题。结果在我看来还是很丑陋:难以置信,没有人能像我一样提出/复制/粘贴这些内容。但是也许一个对所涉及的各种特征非常熟悉的人会没有问题吗?
use hyper::{client::connect::HttpConnector,service::Service,Uri};
use hyper_tls::HttpsConnector;
use tokio::io::{AsyncRead,AsyncWrite};
type BoxError = Box<dyn std::error::Error + Send + Sync>;
fn with_connector<T>(conn: T)
where
T: Service<Uri>,T::Response: AsyncRead + AsyncWrite + Send + Unpin,T::Future: Send + 'static,T::Error: Into<BoxError>,{
// do something with conn
}
fn main() {
let http = HttpConnector::new();
with_connector(http);
let https = HttpsConnector::new();
with_connector(https);
}
[dependencies]
hyper = "0.13"
hyper-tls = "0.4.3"
tokio = { version = "0.2",features = ["full"] }