]> git.proxmox.com Git - rustc.git/blob - vendor/sysinfo/src/apple/utils.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / sysinfo / src / apple / utils.rs
1 // Take a look at the license at the top of the repository in the LICENSE file.
2
3 use libc::c_char;
4
5 pub(crate) fn cstr_to_rust(c: *const c_char) -> Option<String> {
6 cstr_to_rust_with_size(c, None)
7 }
8
9 pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option<usize>) -> Option<String> {
10 if c.is_null() {
11 return None;
12 }
13 let mut s = match size {
14 Some(len) => Vec::with_capacity(len),
15 None => Vec::new(),
16 };
17 let mut i = 0;
18 unsafe {
19 loop {
20 let value = *c.offset(i) as u8;
21 if value == 0 {
22 break;
23 }
24 s.push(value);
25 i += 1;
26 }
27 String::from_utf8(s).ok()
28 }
29 }
30
31 #[cfg(target_os = "macos")]
32 pub(crate) fn vec_to_rust(buf: Vec<i8>) -> Option<String> {
33 String::from_utf8(
34 buf.into_iter()
35 .flat_map(|b| if b > 0 { Some(b as u8) } else { None })
36 .collect(),
37 )
38 .ok()
39 }