diff options
author | Chelsea Holland Komlo <me@chelseakomlo.com> | 2017-10-22 00:07:16 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-10-27 10:02:08 -0400 |
commit | 91bca5c31b9eefe4d07645f690f69914c89a5594 (patch) | |
tree | 919d0f8b657a1e7311395189cfd75ad0ce7d8794 /src/rust/tor_util/ffi.rs | |
parent | 76bbdfbfa9eca46b53d3ec5a44deafce51d2875a (diff) | |
download | tor-91bca5c31b9eefe4d07645f690f69914c89a5594.tar.gz tor-91bca5c31b9eefe4d07645f690f69914c89a5594.zip |
move to allocating c strings from rust
Diffstat (limited to 'src/rust/tor_util/ffi.rs')
-rw-r--r-- | src/rust/tor_util/ffi.rs | 57 |
1 files changed, 11 insertions, 46 deletions
diff --git a/src/rust/tor_util/ffi.rs b/src/rust/tor_util/ffi.rs index af4bfc41af..9a56309365 100644 --- a/src/rust/tor_util/ffi.rs +++ b/src/rust/tor_util/ffi.rs @@ -1,56 +1,21 @@ -//! FFI functions, only to be called from C. +//! FFI functions to announce Rust support during tor startup, 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 -} +use libc::c_char; +use tor_allocate::allocate_and_copy_string; /// 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); +/// char *rust_str = rust_welcome_string(); +/// printf("%s", rust_str); +/// tor_free(rust_str); /// ``` #[no_mangle] -pub extern "C" fn rust_welcome_string() -> RustString { - let s = CString::new("Tor is running with Rust integration. Please report \ - any bugs you encouter.") - .unwrap(); - RustString::from(s) +pub extern "C" fn rust_welcome_string() -> *mut c_char { + let rust_welcome = String::from("Tor is running with Rust integration. Please report \ + any bugs you encouter."); + allocate_and_copy_string(&rust_welcome) } |