包含的源看不到随使用导入的类型

Included source does not see types imported with use

我很难收录!一些自动生成的代码到一个模块中。我的模块看起来像...

use libc::c_int;

mod nif_versions {
include!(concat!(env!("OUT_DIR"), "/nif_versions.snippet"));
}

... nif_versions.snippet 看起来像 ...

const NIF_MAJOR_VERSION: c_int = 2;
const NIF_MINOR_VERSION: c_int = 7;

但这给了我以下错误:

/home/goertzen/ruster/target/build/ruster-7f1b6b5473eea720/out/nif_versions.snippet:1:26: 1:31 error: use of undeclared type name `c_int`
/home/goertzen/ruster/target/build/ruster-7f1b6b5473eea720/out/nif_versions.snippet:1 const NIF_MAJOR_VERSION: c_int = 2;
                                                                                                               ^~~~~
note: in expansion of include!

如果我跳过包含!并手动粘贴这两行,一切正常。此外,我必须将 include 包装在一个模块中才能使其正常工作。如果可能的话,我想摆脱它。

您只需将 use libc::c_int 移动到内部 mod 声明,因为 use 仅在它们使用的模块内有效:

mod nif_versions {
    use libc::c_int;
    include!(concat!(env!("OUT_DIR"), "/nif_versions.snippet"));
}