这就是C API的外观
bin
void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int));
为我生成了这个
rust-bindgen
如何创建一个生锈回调函数以传递给上面 rust 绑定中的pub fn mosquitto_connect_callback_set(mosq: *mut Struct_mosquitto,
on_connect:
::std::option::Option<extern "C" fn(arg1:
*mut Struct_mosquitto,
arg2:
*mut ::libc::c_void,
arg3:
::libc::c_int) -> ()>)
参数?
答案 0 :(得分:8)
如何创建生锈回调函数?
您是否阅读了The Rust Programming Language,特别是关于FFI标题为Callbacks from C code to Rust functions的部分?
那里的例子是
extern fn callback(a: i32) {
println!("I'm called from C with value {0}", a);
}
#[link(name = "extlib")]
extern {
fn register_callback(cb: extern fn(i32)) -> i32;
fn trigger_callback();
}
fn main() {
unsafe {
register_callback(callback);
trigger_callback(); // Triggers the callback
}
}
针对您的具体情况,您已经了解了所需的特定功能类型:
extern "C" fn mycallback(arg1: *mut Struct_mosquitto,
arg2: *mut ::libc::c_void,
arg3: ::libc::c_int) -> ()
{
println!("I'm in Rust!");
}
然后像
一样使用它mosquitto_connect_callback_set(mosq, Some(mycallback));