dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.82k stars 330 forks source link

Pass a vector of boxed values from Rust to C++ #1273

Open alexasonya opened 1 year ago

alexasonya commented 1 year ago

I couldn't find a proper way of passing vector of boxed values from Rust to C++? The following code gives a seg fault:

Rust


mod ffi {
    extern "Rust" {
        type Temp;
        fn create_temp() -> rust::Vec<Temp>;
    }

      struct TempBox {
        value: Box<Temp>,
      }
}

pub struct Temp(String);

pub fn create_temp() -> Vec<TempBox> {
  let vec = vec![TempBox { value : Box::new(Temp("hello world!".to_string())) }];
  vec
}

C++:

rust::Vec<Temp> res = create_temp();
rust::Box<Temp> tmp = std::move(res[0].value);