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

Improve the sort-semantically for better idiomatic sort of generated Rust code #2300

Closed
amanjeev opened this issue Oct 7, 2022 · 1 comment
Assignees

Comments

@amanjeev
Copy link
Member

amanjeev commented Oct 7, 2022

#1743 has one implementation of sorting, to get the resulting Rust code in a certain order. However, it can be better.

Input C/C++ Header

class Foo {
public:
  int foo;
  int get_foo();
};

class Bar {
public:
  int bar;
  int get_bar();
};

Bindgen Invocation

$ bindgen input.h --no-layout-tests --sort-semantically

Actual Results

/* automatically generated by rust-bindgen 0.60.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
    pub foo: ::std::os::raw::c_int,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
    pub bar: ::std::os::raw::c_int,
}
impl Foo {
    #[inline]
    pub unsafe fn get_foo(&mut self) -> ::std::os::raw::c_int {
        Foo_get_foo(self)
    }
}
impl Bar {
    #[inline]
    pub unsafe fn get_bar(&mut self) -> ::std::os::raw::c_int {
        Bar_get_bar(self)
    }
}
extern "C" {
    #[link_name = "\u{1}_ZN3Foo7get_fooEv"]
    pub fn Foo_get_foo(this: *mut Foo) -> ::std::os::raw::c_int;
}
extern "C" {
    #[link_name = "\u{1}_ZN3Bar7get_barEv"]
    pub fn Bar_get_bar(this: *mut Bar) -> ::std::os::raw::c_int;
}

and/or

Insert compilation errors generated when compiling the bindings with rustc here

Expected or rather Better Results

The order is actually, type followed by the type's impls and extern fns

/* automatically generated by rust-bindgen 0.60.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
    pub foo: ::std::os::raw::c_int,
}
impl Foo {
    #[inline]
    pub unsafe fn get_foo(&mut self) -> ::std::os::raw::c_int {
        Foo_get_foo(self)
    }
}
extern "C" {
    #[link_name = "\u{1}_ZN3Foo7get_fooEv"]
    pub fn Foo_get_foo(this: *mut Foo) -> ::std::os::raw::c_int;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Bar {
    pub bar: ::std::os::raw::c_int,
}
impl Bar {
    #[inline]
    pub unsafe fn get_bar(&mut self) -> ::std::os::raw::c_int {
        Bar_get_bar(self)
    }
}
extern "C" {
    #[link_name = "\u{1}_ZN3Bar7get_barEv"]
    pub fn Bar_get_bar(this: *mut Bar) -> ::std::os::raw::c_int;
}
@amanjeev amanjeev self-assigned this Oct 7, 2022
@amanjeev
Copy link
Member Author

closing this because there is a decent implicit sorting of the elements already.

@amanjeev amanjeev closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
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

No branches or pull requests

1 participant