Suppose I have a C++ allocated buffer uint8_t*
and I want to access it from Rust:
One way would be to have the C++ function:
uint8_t receive(uint8_t** data, size_t* size) {
//allocates the data, writes to it and then points *data to it
return 0;//on success
}
however this leaves Rust responsible for dealocating the data. Same for
uint8_t* receive(size_t* size) {
uit8_t* data = //allocates data
*size = data_size;
return data;
}
The idea I have is to allocate the data on Rust and then pass a pointer for C++ to fill:
uint8_t receive(uint8_t* data, size_t size) {
//fills data up to size
return 0;
}
However, we cannot simply allocate a buffer in Rust and expect it to be C/C++ compatible. Also it has the limitation of having to know a size in advance, or use a sufficiently big size for all buffers.
What would be the best solution for this?