cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

[ Feature Request ] Resolve trait associated types

Open sphaerophoria opened this issue 7 years ago • 4 comments

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

sphaerophoria avatar Dec 22 '17 03:12 sphaerophoria

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();

sjeohp-zz avatar Apr 01 '20 14:04 sjeohp-zz

cbindgen doesn't hook into the rust compiler so it doesn't seem particularly easy to do it in the general case.

emilio avatar Apr 01 '20 15:04 emilio

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 fns 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?

mversic avatar Mar 16 '23 11:03 mversic

Please assign me to this issue. I have already almost finished the implementation of the solution. Thank you

orangeng avatar Nov 28 '23 05:11 orangeng