mirror of
https://github.com/guilhermewerner/rust-ffi
synced 2025-06-16 05:44:19 +00:00
Add String Conversion Function
This commit is contained in:
14
Source/Converter.rs
Normal file
14
Source/Converter.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use libc::c_char;
|
||||
use std::ffi::{CStr, CString};
|
||||
|
||||
/// Convert C string to Rust String.
|
||||
pub fn ToString(ptr: *mut c_char) -> String {
|
||||
let input = unsafe { CStr::from_ptr(ptr) };
|
||||
|
||||
return input.to_str().unwrap().to_string();
|
||||
}
|
||||
|
||||
/// Convert Rust String to C string.
|
||||
pub fn FromString(string: String) -> *mut c_char {
|
||||
return CString::new(string).unwrap().into_raw();
|
||||
}
|
Reference in New Issue
Block a user