rustffirust-bindgen

How can I provide an alternate definition of an extern "C" FFI binding?


I'm using a 3rd party library which contains a binding to an extern "C" function:

extern "C" {
    pub fn PageAddItemExtended(
        page: Page,
        item: Item,
        size: Size,
        offsetNumber: OffsetNumber,
        flags: ::std::os::raw::c_int,
    ) -> OffsetNumber;
}

Item is defined as type Pointer which is defined as *mut ::std::os::raw::c_char. This particular function never mutates the data that Item points to, and so I'd like to provide a immutable value to it.

If I had control over the 3rd party library, I could just omit the mut from the definition of Item and all would be fine, but I don't.

I tried adding a second definition of this extern "C" function without the mut, but it generated this error:

error[E0255]: the name `PageAddItemExtended` is defined multiple times
  --> src/segment/page.rs:49:5
   |
3  |   use crate::pg_sys::PageAddItemExtended;
   |       ---------------------------------- previous import of the value `PageAddItemExtended` here
...
49 | /     pub fn PageAddItemExtended(
50 | |         page: Page,
51 | |         item: *const ::std::os::raw::c_char,
52 | |         size: Size,
53 | |         offsetNumber: OffsetNumber,
54 | |         flags: ::std::os::raw::c_int,
55 | |     ) -> OffsetNumber;
   | |______________________^ `PageAddItemExtended` redefined here
   |
   = note: `PageAddItemExtended` must be defined only once in the value namespace of this module
help: you can use `as` to change the binding name of the import
   |
3  | use crate::pg_sys::PageAddItemExtended as OtherPageAddItemExtended;
   |   

This makes sense; if my code calls this function, the compiler wouldn't know which definition to use.

Is there a workaround to this problem? Some way of tricking the compiler into using a different definition of the function?


Solution

  • As the compiler says, it fails because PageAddItemExtended is already defined. Omit the previous definition use crate::pg_sys::PageAddItemExtended; and it will compile successfully.