ffi.rs 537 B

12345678910111213141516171819
  1. //! FFI functions, only to be called from C.
  2. //!
  3. //! This module provides the ability for C to free strings that have been
  4. //! allocated in Rust.
  5. extern crate libc;
  6. use libc::c_char;
  7. use std::ffi::CString;
  8. /// This allows strings allocated in Rust to be freed in Rust. Every string
  9. /// sent across the Rust/C FFI boundary should utilize this function for
  10. /// freeing strings allocated in Rust.
  11. #[no_mangle]
  12. pub extern "C" fn free_rust_str(ptr: *mut c_char) {
  13. if !ptr.is_null() {
  14. unsafe { CString::from_raw(ptr) };
  15. }
  16. }