From 34afc295915ca5fbaac49098a8da8b14677b6e8c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 28 Jan 2023 13:53:33 +0800 Subject: [PATCH] feat: dump String from `c_char` --- src/rust_ffi/src/main.rs | 41 ++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/rust_ffi/src/main.rs b/src/rust_ffi/src/main.rs index 77899e0..37b9842 100644 --- a/src/rust_ffi/src/main.rs +++ b/src/rust_ffi/src/main.rs @@ -4,34 +4,59 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); use std::env; -use std::ffi::CStr; +use std::ffi::{c_char, CStr}; -fn klotski_tmain() { - unsafe { - tmain(); +pub trait CStringDump { + fn dump(&self) -> String; +} + +impl CStringDump for *const c_char { + fn dump(&self) -> String { + let cstr: &CStr; + unsafe { + cstr = CStr::from_ptr(*self); + } + String::from(cstr.to_str().unwrap()) } } +// fn klotski_tmain() { +// unsafe { +// tmain(); +// } +// } + +// fn const_char_dump(i: *const c_char) { +// +// } + fn klotski_version() -> String { unsafe { // let v_major = get_version_major(); - let v = get_version(); - let csp: &CStr = CStr::from_ptr(v); - let s: &str = csp.to_str().unwrap(); + // let v = get_version(); + // let csp: &CStr = CStr::from_ptr(v); + // let s: &str = csp.to_str().unwrap(); // println!("klotski version -> {}", s); - String::from(s) + String::from(get_version().dump()) // String::new() } } +fn klotski_system() -> String { + unsafe { + String::from(get_system_info().dump()) + } +} + fn main() { println!("rust start"); // klotski_tmain(); println!("version -> {}", klotski_version()); + println!("system -> {}", klotski_system()); println!("rust exit"); }