Rust bindings for C API with function pointer as argument


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,
                                              ::std::option::Option<extern "C" fn(arg1:
                                                                                      *mut Struct_mosquitto,
                                                                                      *mut ::libc::c_void,
                                                                                      ::libc::c_int) -> ()>)

How do I create a rust callback function to pass to on_connect argument?


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.


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