How do I create a Rust callback function to pass t

2019-02-18 00:39发布

问题:

This is how C API looks

void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int));

rust-bindgen has generated this for me

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) -> ()>)

How do I create a rust callback function to pass to on_connect parameter in the above rust binding?

回答1:

How do I create a rust callback function?

Have you read The Rust Programming Language, specifically the section about FFI titled Callbacks from C code to Rust functions?

The example from there is

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
    }
}

For your specific case, you already know the specific type of function you need:

extern "C" fn mycallback(arg1: *mut Struct_mosquitto,
                         arg2: *mut ::libc::c_void,
                         arg3: ::libc::c_int) -> ()
{
    println!("I'm in Rust!");
}

And then use it like

mosquitto_connect_callback_set(mosq, Some(mycallback));


标签: rust ffi