// 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 pointer. fn ToCharPointer(self) -> *const c_char; /// Parse string from char pointer. fn FromCharPointer(pointer: *const c_char) -> String; } impl ForeignString for String { fn ToCharPointer(self) -> *const c_char { CString::new(self).unwrap().into_raw() } fn FromCharPointer(pointer: *const c_char) -> String { let contents = unsafe { CStr::from_ptr(pointer) }; contents.to_str().unwrap().to_string() } } /// Convert Rust string to C string. pub fn FromString(string: String) -> *const c_char { string.ToCharPointer() } /// Convert C string to Rust string. pub fn ToString(pointer: *const c_char) -> String { String::FromCharPointer(pointer) }