aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/util.rs b/src/util.rs
index f8ad9c9..64dde39 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -28,17 +28,18 @@ pub enum LogLevel {
DebugL2,
}
-pub fn owned_str_from_ptr(ptr: *const c_char) -> String {
+pub fn owned_str_from_ptr(ptr: *const c_char) -> Result<String, Error> {
unsafe { CStr::from_ptr(ptr) }
- .to_string_lossy()
- .into_owned()
+ .to_str()
+ .map(String::from)
+ .map_err(Error::from)
}
pub fn result_from_string(ptr: *const c_char) -> Result<String, Error> {
if ptr.is_null() {
return Err(Error::UnexpectedError);
}
- let s = owned_str_from_ptr(ptr);
+ let s = owned_str_from_ptr(ptr)?;
unsafe { free(ptr as *mut c_void) };
// An empty string can both indicate an error or be a valid return value. In this case, we
// have to check the last command status to decide what to return.