use libc::{self, c_char, c_void};
use std::ffi::CStr;
use std::ptr;
pub fn error_message(ptr: *const c_char) -> String {
let cstr = unsafe { CStr::from_ptr(ptr as *const _) };
let s = String::from_utf8_lossy(cstr.to_bytes()).into_owned();
unsafe {
libc::free(ptr as *mut c_void);
}
s
}
pub fn opt_bytes_to_ptr(opt: Option<&[u8]>) -> *const c_char {
match opt {
Some(v) => v.as_ptr() as *const c_char,
None => ptr::null(),
}
}
macro_rules! ffi_try {
( $($function:ident)::*( $( $arg:expr,)* ) ) => ({
let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();
let result = $($function)::*($($arg),*, &mut err);
if !err.is_null() {
return Err(Error::new($crate::ffi_util::error_message(err)));
}
result
})
}