0

I have the following C function:

typedef void (*button_handler_fn_t)();

void set_button_handler(uint32_t button_no, button_handler_fn_t handler_fn);

The second parameter is a C function that takes no parameters and returns void.

I want to call this C function from Rust, passing in a Rust function. bindgen generates the following bindings which I then import in my Rust program:

pub type button_handler_fn_t = ::core::option::Option<unsafe extern "C" fn()>;

extern "C" {
    pub fn set_button_handler(button_no: u32, handler_fn: button_handler_fn_t);
}

How can I make a function call?

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

unsafe fn callback_button_0() {
    svc_leds_toggle(0);
}

fn main() -> ! {
    set_button_handler(0, callback_button_0);
                          ^^^^^^^^^^^^^^^^^ expected enum `core::option::Option`, found fn item

   = note: expected type `core::option::Option<unsafe extern "C" fn()>`
              found type `unsafe fn() {callback_button_0}`                          
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dazedviper
  • 992
  • 1
  • 9
  • 17
  • Your last code block appears to be a mix of Rust code and an error message. Please take the time to review your question before and after posting to make sure it accurately and effectively communicates what you intended. – Shepmaster Jul 23 '19 at 22:16
  • @Shepmaster I think thats the point, they are showing the code and what error it's saying and wanting to know how to do it correctly? – Keith Nicholas Jul 23 '19 at 22:18
  • @KeithNicholas my point is that the code they are attempting to compile and the error message should be in two separate code blocks. They make no sense to be smushed together. – Shepmaster Jul 23 '19 at 22:22
  • Applying the duplicate to this case: `unsafe extern "C" fn callback_button_0()` and `unsafe { set_button_handler(0, Some(callback_button_0)); }` – Shepmaster Jul 23 '19 at 22:24
  • Indeed, this is a duplicate. Thank you! – dazedviper Jul 23 '19 at 22:59

0 Answers0