Files
simple-rust-tests/__ffi/dotalib/src/lib.rs
2021-01-10 19:06:06 +08:00

24 lines
598 B
Rust

use std::{ffi::{CStr, CString}, os::raw::c_char};
/// Print line
#[no_mangle]
pub unsafe extern "C" fn println(str: *const c_char) {
let c_str = CStr::from_ptr(str).to_str().unwrap();
println!("{}", c_str);
}
/// Get mut-able string from rust
#[no_mangle]
pub unsafe extern "C" fn get_str_mut() -> *mut c_char {
let c_string = CString::new("hello world!").expect("Error!");
c_string.into_raw()
}
/// Get const string from rust
#[no_mangle]
pub unsafe extern "C" fn get_str() -> *const c_char {
let c_string = CString::new("hello world!").expect("Error!");
c_string.as_ptr()
}