是否可以将某些包含标记为仅包含在相关操作系统中?
例如,你可以这样做:
#[cfg(unix)] {
use std::os::unix::io::IntoRawFd;
}
#[cfg(windows)] {
// https://doc.rust-lang.org/std/os/unix/io/trait.AsRawFd.html suggests this is equivalent?
use std::os::windows::io::AsRawHandle;
}
尝试编译上面的代码会出现语法错误(即error: expected item after attributes
).
我正在尝试修补我在 GitHub 上找到的 Rust 项目,以便在 Windows 上进行编译(同时仍然保留在现有目标(即 Unix 和 WASM)上进行编译的能力)。目前我遇到了一个问题,其中一些文件从以下位置导入特定于平台的部分std::os
(e.g. use std::os::unix::io::IntoRawFd;
),这最终破坏了 Windows 上的构建。
注意:我使用的是 Rust Stable (1.31.1),而不是 nightly。
您正在寻找的语法是:
#[cfg(target_os = "unix")]
use std::os::unix::io::IntoRawFd;
#[cfg(target_os = "windows")]
use std::os::windows::io::AsRawHandle;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)