我正在为 C 库编写绑定,我想调用std::io::File::open它,因为它已经有了错误处理功能。然后我打算将其传递fd给 C 函数。
我看过std::io::fs,但该fd字段与我想象的完全不同。
经过更多挖掘后,我发现了native::io::file::FileDesc,它确实有fn fd(&self) -> fd_t,但似乎不是我可以从 的实例访问的东西std::io::File。
似乎有fs_from_raw_fd方法,它与我需要的完全相反。
小智 6
use std::os::unix::io::AsRawFd;
let path = Path::new("my.txt");
let display = path.display();
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that
// describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
println!("file descriptor: {}",file.as_raw_fd());
Run Code Online (Sandbox Code Playgroud)