// Copyright (c) TribuFu. All Rights Reserved. use libc::c_char; use std::ffi::{CStr, CString}; #[allow(clippy::not_unsafe_ptr_arg_deref)] pub trait ForeignString where Self: Sized, { /// Convert this string to a char pt. fn to_char_pt(self) -> *const c_char; /// Parse string from char pt. fn from_char_pt(pt: *const c_char) -> String; } impl ForeignString for String { fn to_char_pt(self) -> *const c_char { CString::new(self).unwrap().into_raw() } fn from_char_pt(pt: *const c_char) -> String { let contents = unsafe { CStr::from_ptr(pt) }; contents.to_str().unwrap().to_string() } } /// Convert Rust string to C string. pub fn from_string(string: String) -> *const c_char { string.to_char_pt() } /// Convert C string to Rust string. pub fn to_string(pt: *const c_char) -> String { String::from_char_pt(pt) }