dtolnay / cxx

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

How to pass binary string from Rust to C++? #1291

Closed beichen2012 closed 7 months ago

beichen2012 commented 7 months ago

How to pass binary string (not valid utf-8 characters) from Rust to C++?

moshimo2019 commented 7 months ago

Currently, it seems that there is no out-of-the-box API in cxx to pass OsString from Rust to C++. You need to convert it to String:

let os = OsString::new();
let s = unsafe { String::from_utf8_unchecked(os.into_encoded_bytes()) };

Anyway, it's fallible to convert an OsString (not valid utf-8) to String in Rust. So you can directly pass &[u8] to C++ and then construct a std::string.

beichen2012 commented 7 months ago

Thanks. Finally, Implemented by passing Vec[u8] from Rust to C++.