0

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Aleksander Wons
  • 3,611
  • 18
  • 29
  • *must have exactly one entry point that is responsible for creating all necessary structures* — that's known as a "singleton". – Shepmaster Aug 05 '17 at 19:42
  • Note that your current code is **not** thread safe, and is likely to have issues. That's why a `static mut` requires `unsafe`. – Shepmaster Aug 05 '17 at 19:48

0 Answers0