dtolnay / cxx

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

Question: Convert uint8_t* in C++ to Vec<u8> in Rust #1311

Open helenzhangyc opened 8 months ago

helenzhangyc commented 8 months ago

Hi, I have a uint8_t* in C++ and I want to pass it into my rust code and use it as a Vec<u8>. The current way I am doing it is converting the uint8_t* to a vector<uint8_t> in C++ and then passing it to rust as a vec: &CxxVector<u8>. Then, I do let new_vec: Vec<u8> = vec.iter().cloned().collect(); to obtain a Vec<u8> in Rust.

This works fine but takes a long time since my vector is huge (~14GB) and I have to iterate through it and clone it. I wonder if there is a way to pass the pointer directly into rust and cast it to a Vec<u8> in rust.