如何使用其他模块的私有特征?

时间:2015-04-19 06:50:12

标签: rust

我正在尝试运行以下代码段:

use std::net::Ipv4Addr;
use std::ffi::CString;
use std::sys_common::AsInner;

fn main() {
    let ip: Ipv4Addr = Ipv4Addr::new(127,0,0,1);
    println!("{}", ip.as_inner().s_addr);
}

失败
test.rs:3:5: 3:29 error: trait `AsInner` is private
test.rs:3 use std::sys_common::AsInner;
              ^~~~~~~~~~~~~~~~~~~~~~~~
test.rs:7:20: 7:33 error: source trait is private
test.rs:7     println!("{}", ip.as_inner().s_addr);
                             ^~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
test.rs:7:5: 7:42 note: expansion site
error: aborting due to 2 previous errors

我确实在http://doc.rust-lang.org/nightly/src/std/net/ip.rs.html#238中看到AsInner特征是私密的 我该如何使用这个特性?

编辑: 我试图将Ipv4Addr转换为int,我以为我可以读取底层的in_addr。但似乎使用八位字节是一个更好的主意。

1 个答案:

答案 0 :(得分:5)

Vladimir Matveev说得最好:

  

你做不到。私有事物的整个概念是它们不能在它们被定义的模块之外使用。