Browse Source

update: single ffi rs file

dev
Dnomd343 2 years ago
parent
commit
bf34eba338
  1. 18
      src/to-json/src/ffi.rs
  2. 16
      src/to-json/src/lib.rs

18
src/to-json/src/ffi.rs

@ -0,0 +1,18 @@
use std::ffi::{c_char, CStr, CString};
#[no_mangle]
pub unsafe extern "C" fn free_rust_string(string: *const c_char) {
let _ = CString::from_raw(string as *mut _);
}
#[no_mangle]
pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char {
let content: &str = CStr::from_ptr(content).to_str().unwrap();
// TODO: remove test code
println!("Raw content -> {}", content);
// TODO: convert to JSON format
CString::new(content).unwrap().into_raw()
}

16
src/to-json/src/lib.rs

@ -1,15 +1 @@
use std::ffi::{c_char, CStr, CString};
#[no_mangle]
pub unsafe extern "C" fn to_json_rust(content: *const c_char) -> *const c_char {
let content: &str = CStr::from_ptr(content).to_str().unwrap();
println!("Raw content -> {}", content);
CString::new(content).unwrap().into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn free_rust_string(string: *const c_char) {
let _ = CString::from_raw(string as *mut _);
}
mod ffi;

Loading…
Cancel
Save