Open FireFragment opened 3 months ago
In case you're still trying to do this, I wanted to do something similar, but I don't think it's possible. What I ended up doing instead is used an out parameter.
#[cxx::bridge(namespace = "rs")]
mod ffi {
extern "Rust" {
fn ask(response: Pin<&mut CxxString>);
}
}
pub fn ask(response: Pin<&mut CxxString>) {
response.push_str("Hi!!!");
}
It would be nice to return stack-allocated C++ types, though.
Thanks @zambony! That's a fine option when returning a single string, but it doesn't work well for strings inside classes/structs.
Is it possible to construct
CxxString
on the Rust side and return it to the C++ side?Returning
Pin<&mut CxxString>
doesn't work, because it's just a reference to local variable, which is destroyed at the end of the function:Returning
UniquePtr<CxxString>
doesn't work, because I'm not able to construct it:How can I do it? I want my generated bindings to use standard C++ structs such as
std::string
without needing the C++ users to deal withrust::String
which they might not know and might not work with other C++ libraries.