rust-lang/rust-bindgen

Constructors depend on memmovable fields or RVO.

emilio opened this issue · 1 comments

As @jsgf points out in #556, the current code generated for constructors relies on RVO, or requires memmovable fields.

jsgf commented

This does seem to be a problem in practice.

This test fails:

// TestType.h
struct TestType {
  TestType *self;

  TestType();
  bool isvalid() const;
};
// TestType.cpp
TestType::TestType() {
  self = this;
}

bool TestType::isvalid() const {
  return self == this;
}
// test.rs
fn test() {
   let v = unsafe { mem::uninitialized() };
   v = unsafe { TestType::new() };
   assert!(unsafe { v.isvalid() }); // Fails
}

I think bindgen should be generating a placement new method:

impl TestType {
  unsafe fn new_ptr(this: *mut Self) { TestType_TestType(this) }
}