Reads a possibly-null `*const c_char` as an `Option<&str>`. # Safety If `field` is non-null, it must point to a valid NUL-terminated C string whose bytes are valid UTF-8, and the backing memory must remain alive and unmodified for at least `'db`.
(field: *const c_char)
| 63 | /// whose bytes are valid UTF-8, and the backing memory must remain alive and |
| 64 | /// unmodified for at least `'db`. |
| 65 | const unsafe fn as_opt_str<'db>(field: *const c_char) -> Option<&'db str> { |
| 66 | if field.is_null() { |
| 67 | return None; |
| 68 | } |
| 69 | |
| 70 | // SAFETY: we just checked for null; caller guarantees validity otherwise. |
| 71 | let str = unsafe { CStr::from_ptr(field).to_str() }; |
| 72 | |
| 73 | match str { |
| 74 | Ok(str) => Some(str), |
| 75 | Err(_) => panic!("string is not utf-8"), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// CPU architecture reported by the PMC database. |
| 80 | /// |