add function to convert NULL-able c-string to Option<String>

This commit is contained in:
B. Petersen
2019-10-19 11:30:32 +02:00
committed by Floris Bruynooghe
parent bb08b39c71
commit 0108b4724e

View File

@@ -735,6 +735,14 @@ pub fn to_string_lossy(s: *const libc::c_char) -> String {
cstr.to_string_lossy().to_string()
}
pub fn to_opt_string_lossy(s: *const libc::c_char) -> Option<String> {
if s.is_null() {
return None;
}
Some(to_string_lossy(s))
}
pub fn as_str<'a>(s: *const libc::c_char) -> &'a str {
as_str_safe(s).unwrap_or_else(|err| panic!("{}", err))
}