chore: comments

This commit is contained in:
2021-01-10 19:06:06 +08:00
parent 58ea520a63
commit e73575f62e
2 changed files with 12 additions and 1 deletions

View File

@@ -5,8 +5,17 @@
#include <stdlib.h> #include <stdlib.h>
/**
* Get const string from rust
*/
const char *get_str(void); const char *get_str(void);
/**
* Get mut-able string from rust
*/
char *get_str_mut(void); char *get_str_mut(void);
/**
* Print line
*/
void println(const char *str); void println(const char *str);

View File

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