1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! FFI functions, only to be called from C.
//!
//! Equivalent C versions of these live in `src/common/compat_rust.c`

use std::mem::forget;
use std::ffi::CString;

use libc;
use rust_string::RustString;

/// Free the passed `RustString` (`rust_str_t` in C), to be used in place of
/// `tor_free`().
///
/// # Examples
/// ```c
/// rust_str_t r_s = rust_welcome_string();
/// rust_str_free(r_s);
/// ```
#[no_mangle]
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub unsafe extern "C" fn rust_str_free(_str: RustString) {
    // Empty body: Just drop _str and we're done (Drop takes care of it).
}

/// Lends an immutable, NUL-terminated C String.
///
/// # Examples
/// ```c
/// rust_str_t r_s = rust_welcome_string();
/// const char *s = rust_str_get(r_s);
/// printf("%s", s);
/// rust_str_free(r_s);
/// ```
#[no_mangle]
pub unsafe extern "C" fn rust_str_get(str: RustString) -> *const libc::c_char {
    let res = str.as_ptr();
    forget(str);
    res
}

/// Returns a short string to announce Rust support during startup.
///
/// # Examples
/// ```c
/// rust_str_t r_s = rust_welcome_string();
/// const char *s = rust_str_get(r_s);
/// printf("%s", s);
/// rust_str_free(r_s);
/// ```
#[no_mangle]
pub extern "C" fn rust_welcome_string() -> RustString {
    let s = CString::new(" with Rust integration").unwrap();
    RustString::from(s)
}