【问题标题】:Defining a struct member with private module types使用私有模块类型定义结构成员
【发布时间】:2019-10-25 23:12:35
【问题描述】:

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

我遇到的当前示例是在创建超级客户端时。

// 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 的新手,我正在努力弄清楚我正在尝试做的事情的正确术语是什么,更不用说让它发挥作用了。任何有关此的文档或代码示例的链接将不胜感激。

谢谢

【问题讨论】:

    标签: rust


    【解决方案1】:

    我不确定你想做什么,但你可以使用公共再导出hyper_rustls::HttpsConnector 代替私人hyper_rustls::connector::HttpsConnector 和公共再导出hyper::client::HttpConnector 代替私人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

    【讨论】:

    • 谢谢,改变它的工作,我不认为要调查再出口。我使用 IDE 获取实例化返回的类型,它显示私有类型而不是公共别名。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多