use crate::ffi;
use crate::{Error, Result};
use std::{
ffi::{CStr, CString},
path::Path,
};
pub(crate) unsafe fn copy_c_str(s: ffi::c_str) -> String {
debug_assert!(!s.is_null());
CStr::from_ptr(s).to_string_lossy().into_owned() }
pub(crate) trait AsPtr {
type Ptr;
unsafe fn as_mut_ptr(&self) -> *mut Self::Ptr;
#[allow(unused)]
fn as_ptr(&self) -> *const Self::Ptr {
(unsafe { self.as_mut_ptr() }).cast_const()
}
}
pub(crate) fn path_to_cstring<P: AsRef<Path>>(p: P) -> Result<CString> {
let path = p.as_ref().to_string_lossy().into_owned().into_bytes();
CString::new(path).map_err(Error::NulError)
}
#[test]
fn conversion_must_succeed() {
use std::ffi::CString;
let s1 = "mip1.log";
let cs = CString::new(s1).unwrap();
let s2 = unsafe { copy_c_str(cs.as_ptr()) };
assert!(s1 == s2);
}