I am writing a POC for a binding to PHP's Zend Engine.
The library must have exactly one entry point that is responsible for creating all necessary structures.
Parts of those structures are *const ::std::os::raw::c_char
fields.
Right now to allocate each string I am using the following construct:
static mut BUILD_ID: Option<CString> = None;
// then in my function:
unsafe {
BUILD_ID = Some(CString::new(self.build_id).unwrap());
Box::new(zend_module_entry {
build_id: match BUILD_ID {
Some(ref x) => x.as_ptr(),
None => panic!(),
},
})
}
To deallocate the memory I need:
unsafe {
let _ = BUILD_ID.take().unwrap();
}
Is there a way to do this without static mut
?