• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::ffi::CStr;
2 use std::os::raw::c_char;
3 
4 #[allow(clippy::not_unsafe_ptr_arg_deref)]
c_string_to_string(cstr: *const c_char) -> String5 pub fn c_string_to_string(cstr: *const c_char) -> String {
6     if cstr.is_null() {
7         return String::from("");
8     }
9 
10     let res = unsafe { CStr::from_ptr(cstr).to_str() };
11     assert!(res.is_ok());
12     String::from(res.unwrap_or(""))
13 }
14 
15 /// # Safety
16 ///
17 /// Same as [`CStr::from_ptr`]
char_arr_to_cstr(c_str: &[c_char]) -> &CStr18 pub unsafe fn char_arr_to_cstr(c_str: &[c_char]) -> &CStr {
19     unsafe { CStr::from_ptr(c_str.as_ptr()) }
20 }
21