1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::path::PathBuf;
pub fn rust_str_to_c_str<T: Into<String>>(s: T) -> *mut libc::c_char {
CString::new(s.into()).unwrap().into_raw()
}
pub unsafe fn free_c_str(ptr: *mut libc::c_char) {
if !ptr.is_null() {
let _ = CString::from_raw(ptr);
}
}
pub fn raw_ptr<T>(thing: T) -> *mut T {
Box::into_raw(Box::new(thing))
}
pub unsafe fn c_str_to_rust_str<'a>(x: *const libc::c_char) -> Cow<'a, str> {
if x.is_null() {
Cow::from("")
} else {
CStr::from_ptr(x).to_string_lossy()
}
}
pub unsafe fn cast_const<'a, T>(x: *mut T) -> &'a T {
assert!(!x.is_null(), "Object argument was null");
(&(*x))
}
pub unsafe fn c_str_to_pbuf(x: *const libc::c_char) -> PathBuf {
PathBuf::from(String::from(c_str_to_rust_str(x)))
}