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

Mismatch in size of std::string #19

Closed
dtolnay opened this issue Oct 11, 2020 · 2 comments
Closed

Mismatch in size of std::string #19

dtolnay opened this issue Oct 11, 2020 · 2 comments

Comments

@dtolnay
Copy link
Contributor

dtolnay commented Oct 11, 2020

On my machine this input header results in the following generated Rust struct, which looks reasonable, except the Rust side and C++ side disagree about the size of std::string.

#pragma once
#include <cstdint>
#include <string>
struct S {
  explicit S(uint32_t i);
  std::string s;
  uint32_t i;
};
// generated by autocxx:

#[repr(C)]
pub struct S {
    pub s: ::cxx::CxxString,
    pub __bindgen_padding_0: [u32; 6usize],
    pub i: u32,
}

I'll share a PR to help reproduce. Possibly bindgen run by autocxx is finding a different set of system headers than the C++ build?

Here is the debug printing I used to confirm the issue:

S::S(uint32_t i) : i(i) {
  std::cout << "C++:" << (size_t(&this->i) - size_t(this)) << std::endl;
}
let s = ffi::cxxbridge::S_make_unique(1);
println!("Rust:{}", (&s.i as *const _ as usize) - (&*s as *const _ as usize));
println!("{}", s.i);

Expected output is that C++ and Rust print the same size and the third line is 1, but the actual output is:

C++:32
Rust:24
0
@adetaylor
Copy link
Collaborator

Per comments on #18 this is intentional because of this prelude without which bindgen can't cope with std::string at all (and, as you hint, different STLs could theoretically give different implementations so we probably can't make better assumptions - I must admit I have not checked the C++ standards though). I am going to treat this as a duplicate of #18, which remains in brief "field access to non-POD types is doomed"!!

@dtolnay
Copy link
Contributor Author

dtolnay commented Oct 11, 2020

Ah that's too bad. :( It would have been really slick to be able to support this without accessor methods. I have some ideas for how to make it work anyway without accessors and without requiring to know the field sizes at codegen time, which I will write up.

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

2 participants