rust-libxml icon indicating copy to clipboard operation
rust-libxml copied to clipboard

Declare function to xmllib2 using rust-libxml

Open hurricane-socrates opened this issue 2 months ago • 3 comments

This is a question also asked on rust-users

I'm working on an scxml interpreter. I want to declare a function to the xml path context. I'd appreciate some guidance.

It looks like I'll need this function signature:

unsafe extern "C" fn(ctxt: xmlXPathParserContextPtr, nargs: ::std::os::raw::c_int)

I'd like this function to be written in Rust. However, it looks like a function signature for that is

unsafe extern "C" fn(ctxt: xmlXPathParserContextPtr, nargs: ::std::os::raw::c_int) {}

I'm trying to use this as follows:

let funcname = CString::new("In").unwrap();
let func = Some(in_func);
let rslt = unsafe {
     xmlXPathRegisterFunc(context.as_ptr(), funcname.as_ptr() as *const u8, func)
};

which gives this error:

error[E0308]: mismatched types
      |
277   |                 xmlXPathRegisterFunc(context.as_ptr(), funcname.as_ptr() as *const u8, func)
      |                 -------------------- arguments to this function are incorrect          ^^^^ expected `Option<unsafe extern "C" fn(..., ...)>`, found `Option<unsafe extern "C" fn(..., ...) {in_func}>`
      |
      = note: expected enum `Option<unsafe extern "C" fn(_, _)>`
                 found enum `Option<unsafe extern "C" fn(_, _) {in_func}>`
note: function defined here
     --> libxml-0.3.3/src/bindings.rs:20561:10
      |
20561 |   pub fn xmlXPathRegisterFunc(
      |          ^^^^^^^^^^^^^^^^^^^^

hurricane-socrates avatar Apr 27 '24 13:04 hurricane-socrates