gpt4 book ai didi

rust - 定义具有私有(private)模块类型的结构成员

转载 作者:行者123 更新时间:2023-11-29 08:30:47 24 4
gpt4 key购买 nike

我一直在使用一堆具有返回结构的 build() 函数的模块。但是,当我尝试创建自己的“ super ”结构以将它们捆绑在一起时,我遇到了错误 module `xxx` is private rustc(E0603)。如果有特征,我可以将单个变量作为参数传递,但无法弄清楚如何为结构定义/封装它。

我正在使用的当前示例是在创建 super 客户端时。

// Error due to privacy and cannot use the trait to define the member type
// Both the "hyper_rustls::connector" and "hyper::client::connect::http" modules are private.
struct SecureClient {
client: hyper::client::Client<
hyper_rustls::connector::HttpsConnector<hyper::client::connect::http::HttpConnector>>
}

// Works, but passing the client everywhere as an individual variable is not realistic.
fn use_client(client: hyper::client::Client<impl hyper::client::connect::Connect>) -> () {
()
}

let https_conn = hyper_rustls::HttpsConnector::new(4);
let client: hyper::client::Client<_, hyper::Body> = hyper::Client::builder().build(https_conn);

作为 Rust 的新手,我正在努力寻找适合我要做的事情的行话,更不用说让它发挥作用了。与此相关的任何文档或代码示例的链接将不胜感激。

谢谢

最佳答案

我不确定你想做什么,但你可以使用公共(public)重新导出 hyper_ruSTLs::HttpsConnector 而不是私有(private) hyper_ruSTLs::connector::HttpsConnector 和公共(public)重新导出 hyper::client::HttpConnector 而不是私有(private) hyper::client::connect::http::HttpConnector。您可以在此处阅读有关转口的信息:https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#re-exporting-names-with-pub-use

关于rust - 定义具有私有(private)模块类型的结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58566737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com