MCPcopy Create free account
hub / github.com/hashintel/hash / as_opt_str

Function as_opt_str

libs/darwin-kperf/src/database.rs:65–77  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

63/// whose bytes are valid UTF-8, and the backing memory must remain alive and
64/// unmodified for at least `'db`.
65const 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///

Callers 4

descriptionMethod · 0.85
errataMethod · 0.85
aliasMethod · 0.85
fallbackMethod · 0.85

Calls 2

is_nullMethod · 0.80
to_strMethod · 0.80

Tested by

no test coverage detected