cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Skip export prefix on certain objects

Open Cryoris opened this issue 10 months ago • 3 comments

Problem

We'd like to expose a C API from Rust where all objects we define obtain a prefix, which can be very nicely done with

[export]
prefix = "MyPackage_" 

However, some function signatures use existing C objects, which should not obtain the prefix.

For example, say we have something like

use pyo3::ffi::PyObject;

#[repr(C)]
pub struct Thing { .. }

#[no_mangle]
#[cfg(feature = "cbinding")]
pub unsafe extern "C" fn thing_to_py(thing: *mut Thing) -> *mut PyObject { .. }

Then the resulting header looks like

typedef struct { .. } MyPackage_Thing;  // has prefix

MyPackage_PyObject* thing_to_py(MyPackage_Thing thing);  // PyObject should not have the prefix!

which is not quite what we want.

Workaround

This can be worked around by doing the prefix manual, e.g. do not set the export.prefix but instead do

[export.rename]
"Thing" = "MyPackage_Thing"
# repeat for every object, except for PyObject... 

Proposal

It would be great if there was a way to exclude objects from prefix. E.g. something like

[export]
prefix = "MyPackage_"
prefix_ignores = ["PyObject"]

But maybe I'm just missing an already existing solution (which would be even better).

Cryoris avatar Mar 14 '25 17:03 Cryoris