Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Feature Request ] Resolve trait associated types #106

Open
sphaerophoria opened this issue Dec 22, 2017 · 4 comments · May be fixed by #905
Open

[ Feature Request ] Resolve trait associated types #106

sphaerophoria opened this issue Dec 22, 2017 · 4 comments · May be fixed by #905

Comments

@sphaerophoria
Copy link

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 ::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

@sjeohp-zz
Copy link

sjeohp-zz commented Apr 1, 2020

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

@emilio
Copy link
Collaborator

emilio commented Apr 1, 2020

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

@mversic
Copy link
Contributor

mversic commented Mar 16, 2023

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?

@orangeng
Copy link

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

@orangeng orangeng linked a pull request Dec 7, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants