I'm writing some code that interacts with a c ffi. I need to wrap a c function in the form of:
void c_function (char * res, int max_len);, where the function will fill res with up to max_len-1 bytes of data, plus a terminator. I would like the rust wrapper to return a Result<String, Error> .
I'm trying to create the empty buffer to pass in to the function but I'm having difficulty. I read the docs for std::ffi::CString, but there is nothing close to the with_capacity I was hoping for. My closest attempt is CString::from_vec_unchecked(vec![0; max_len]), but this is unsafe for reasons I do not understand.
What is the best way to allocate a buffer I can pass as a *mut c_char?