cbindgen
cbindgen copied to clipboard
[ Feature Request ] Resolve trait associated types
I've written some derive code that generates the equivalent C api structs for me. After running cargo expand the structs look like
pub struct Notification {
pub color: Color,
pub message: String,
pub message_format: MessageFormat,
}
#[repr(C)]
pub struct CNotification {
pub color: <Color as CRepr>::Output,
pub message: <String as CRepr>::Output,
pub message_format: <MessageFormat as CRepr>::Output,
}
impl CRepr for Notification {
type Output = CNotification;
fn to_c_repr(&self) -> Self::Output {
CNotification{color: self.color.to_c_repr(),
message: self.message.to_c_repr(),
message_format: self.message_format.to_c_repr(),}
}
}
}
Unfortunately there is currently no way for me to automatically generate the equivalent C header bindings as no bindings generator can currently handle resolving what <String as CRepr>::Output means. Currently I've worked around this by generating type names for each associated type but it feels ugly to do it that way, and my generated C headers get a lot of extra typedefs.
It would be nice if the bindings generator could resolve associated types like it does regular types.
If you have an idea of where to start I don't mind taking a stab at it
I am also interested in this. Does anyone have an idea of how feasible it is?
e.g. I have various functions like
pub extern "C" fn some_func() -> <Option<bool> as Return<'static>>::Ext
which currently generate headers like
Ext some_func();
cbindgen doesn't hook into the rust compiler so it doesn't seem particularly easy to do it in the general case.
I require this feature badly. After having written most of this library for generating FFI out of existing rust structures I come here to find that generating C bindings would prove more of a challenge. My problem is twofold:
1. cbindgen
doesn't expand macros (my extern fn
s are macro generated):
I seem to be able to sidestep this issue with cargo expand
. As far as I was able to see cbindgen
didn't have any problem with parsing the generated file
2. cbindgen
doesn't resolve associated types (the library I mentioned relies heavily on associated types):
seems to me that I should be able to use RUSTFLAGS="--emit mir" cargo build
(which resolves associated types) and integrate that output with cbindgen
.
Do you find this approach feasible and do you think that 2. be integrated with cbindgen
?
Please assign me to this issue. I have already almost finished the implementation of the solution. Thank you