Skip to content

[ Feature Request ] Resolve trait associated types #106

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

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

[ Feature Request ] Resolve trait associated types #106

sphaerophoria opened this issue Dec 22, 2017 · 3 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.

@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.

4 participants