是否可以在不污染命名空间的情况下声明 extern "C" 函数?
Is it possible to declare extern "C" functions without polluting the namespace?
在 Rust 中,extern C
函数可以在外部作用域中声明:
#[link(name = "some_lib")]
extern "C" {
pub fn foo(path: *const c_char);
pub fn bar(path: *const c_char);
pub fn baz(path: *const c_char);
pub fn fez(path: *const c_char);
}
虽然可以接受,但每个函数都需要直接访问foo()
bar()
...等等
是否可以通过使用公共前缀访问它们的方式来声明它们?
封装如何工作的示例:
namespace some_lib {
#[link(name = "some_lib")]
extern "C" {
pub fn foo(path: *const c_char);
// ... etc ...
}
}
fn some_rust_function() {
unsafe {
some_lib::foo(); // <-- example usage
}
}
这样的事情可能吗?
只需使用一个模块。
mod ffi {
extern "C" {
pub fn exit(_: i32) -> !;
}
}
fn main() {
unsafe { ffi::exit(1); }
}
在 Rust 中,extern C
函数可以在外部作用域中声明:
#[link(name = "some_lib")]
extern "C" {
pub fn foo(path: *const c_char);
pub fn bar(path: *const c_char);
pub fn baz(path: *const c_char);
pub fn fez(path: *const c_char);
}
虽然可以接受,但每个函数都需要直接访问foo()
bar()
...等等
是否可以通过使用公共前缀访问它们的方式来声明它们?
封装如何工作的示例:
namespace some_lib {
#[link(name = "some_lib")]
extern "C" {
pub fn foo(path: *const c_char);
// ... etc ...
}
}
fn some_rust_function() {
unsafe {
some_lib::foo(); // <-- example usage
}
}
这样的事情可能吗?
只需使用一个模块。
mod ffi {
extern "C" {
pub fn exit(_: i32) -> !;
}
}
fn main() {
unsafe { ffi::exit(1); }
}