How to call a raw function pointer as a struct member wrapped by option

Hi guys! I am translating a C program into rust. I have a C struct

struct _sapi_module_struct {
 	char *name;
 	char *pretty_name;
 	int(*startup)(struct _sapi_module_struct *sapi_module);
 	int(*shutdown)(struct _sapi_module_struct *sapi_module);

And Bindgen translated it into:

#[derive(Debug, Copy, Clone)]
pub struct _sapi_module_struct {
    pub name: *mut ::std::os::raw::c_char,
    pub pretty_name: *mut ::std::os::raw::c_char,
    pub startup: ::std::option::Option<
          unsafe extern "C" fn(sapi_module: *mut _sapi_module_struct) -> ::std::os::raw::c_int,
     pub shutdown: ::std::option::Option<
          unsafe extern "C" fn(sapi_module: *mut _sapi_module_struct) -> ::std::os::raw::c_int,

You see, the startup and shutdown members of the struct are function pointers. But in rust they are wrapped by Option. I want to call startup. But I have a failed try. See my following figure.

Can someone help me? Thank you!

Hi! It's easier for us to read your code if you follow the formatting guidelines:

I may have thought of the solution myself: just use a simple match statement.


Your application looks very similar to the solution I proposed for another question:

struct Foo {
  destroy: Option<unsafe extern "C" fn(*mut Foo)>,

impl Drop for Foo {
  fn drop(&mut self) {
    if let Some(destructor) = self.destroy {
      unsafe { destructor(self); }

The idea is you can use an if-let statement to execute a block of code if the expression matches a single variant.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.