use std::ffi::{CString, CStr, NulError};
use std::path::Path;
use std::borrow::Cow;
use libc::c_char;
pub fn to_cstr(s: &str) -> CString {
CString::new(s.to_owned()).expect("Error: Unexpected null byte")
}
pub unsafe fn from_cstr(p: *const c_char) -> Option<String> {
if p.is_null() {
None
}else{
let cstr = CStr::from_ptr(p);
Some(cstr.to_string_lossy().into_owned())
}
}
pub unsafe fn from_cstr_ref<'a>(p: *const c_char) -> Option<Cow<'a, str>> {
if p.is_null() {
None
}else{
let cstr = CStr::from_ptr(p);
Some(cstr.to_string_lossy())
}
}
pub fn path_to_cstr(path: &Path) -> Result<CString, NulError> {
let path = try!(CString::new(path.to_string_lossy().into_owned()));
Ok(path)
}