Browse Source

feat: dump String from `c_char`

legacy
Dnomd343 2 years ago
parent
commit
34afc29591
  1. 41
      src/rust_ffi/src/main.rs

41
src/rust_ffi/src/main.rs

@ -4,34 +4,59 @@
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
use std::env; use std::env;
use std::ffi::CStr; use std::ffi::{c_char, CStr};
fn klotski_tmain() { pub trait CStringDump {
unsafe { fn dump(&self) -> String;
tmain(); }
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 { fn klotski_version() -> String {
unsafe { unsafe {
// let v_major = get_version_major(); // let v_major = get_version_major();
let v = get_version(); // let v = get_version();
let csp: &CStr = CStr::from_ptr(v); // let csp: &CStr = CStr::from_ptr(v);
let s: &str = csp.to_str().unwrap(); // let s: &str = csp.to_str().unwrap();
// println!("klotski version -> {}", s); // println!("klotski version -> {}", s);
String::from(s) String::from(get_version().dump())
// String::new() // String::new()
} }
} }
fn klotski_system() -> String {
unsafe {
String::from(get_system_info().dump())
}
}
fn main() { fn main() {
println!("rust start"); println!("rust start");
// klotski_tmain(); // klotski_tmain();
println!("version -> {}", klotski_version()); println!("version -> {}", klotski_version());
println!("system -> {}", klotski_system());
println!("rust exit"); println!("rust exit");
} }

Loading…
Cancel
Save