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
41
42
use std::{
ffi::{c_char, CString},
string::FromUtf8Error,
};
pub unsafe fn to_os_ptrs(
strings: &[String],
) -> (Vec<CString>, Vec<*const c_char>) {
let cstrings = strings
.iter()
.cloned()
.map(|str| CString::new(str).unwrap())
.collect::<Vec<CString>>();
let ptrs = cstrings
.iter()
.map(|cstr| cstr.as_ptr())
.collect::<Vec<*const c_char>>();
(cstrings, ptrs)
}
pub fn string_from_i8(bytes: &[i8]) -> Result<String, FromUtf8Error> {
String::from_utf8(
bytes
.iter()
.filter(|&c| *c != 0)
.map(|c| *c as u8)
.collect(),
)
}