aboutsummaryrefslogtreecommitdiff
path: root/src/rust/c_string/ffi.rs
blob: edce250829a59688c1e8990a2616c60537c73aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//! FFI functions, only to be called from C.
//!
//! This module provides the ability for C to free strings that have been
//! allocated in Rust.

extern crate libc;

use libc::c_char;
use std::ffi::CString;

/// This allows strings allocated in Rust to be freed in Rust. Every string
/// sent across the Rust/C FFI boundary should utilize this function for
/// freeing strings allocated in Rust.
#[no_mangle]
pub extern "C" fn free_rust_str(ptr: *mut c_char) {
    if !ptr.is_null() {
        unsafe { CString::from_raw(ptr) };
    }
}