Rust bindings for C API with function pointer as argument


#1

This is how my 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 argument?


#2

I don’t know exactly, but I think there is a class called Fn for functions.

Maybe that helps.

Edit: Sorry, but this seems to be something different. Please ignore this post.


#3

Copy the part inside Option<,> and put some name after fn.