1use libc::{self, c_char, c_void};
17
18use std::ffi::{CStr, CString};
19use std::path::Path;
20use std::ptr;
21
22use crate::Error;
23
24pub fn error_message(ptr: *const c_char) -> String {
25 let cstr = unsafe { CStr::from_ptr(ptr as *const _) };
26 let s = String::from_utf8_lossy(cstr.to_bytes()).into_owned();
27 unsafe {
28 ffi::rocksdb_free(ptr as *mut c_void);
29 }
30 s
31}
32
33pub fn opt_bytes_to_ptr<T: AsRef<[u8]>>(opt: Option<T>) -> *const c_char {
34 match opt {
35 Some(v) => v.as_ref().as_ptr() as *const c_char,
36 None => ptr::null(),
37 }
38}
39
40pub fn to_cpath<P, E>(path: P, error_message: E) -> Result<CString, Error>
41where
42 P: AsRef<Path>,
43 E: AsRef<str>,
44{
45 match CString::new(path.as_ref().to_string_lossy().as_bytes()) {
46 Ok(c) => Ok(c),
47 Err(_) => Err(Error::new(error_message.as_ref().to_string())),
48 }
49}
50
51pub fn to_cstring<S, E>(string: S, error_message: E) -> Result<CString, Error>
52where
53 S: AsRef<str>,
54 E: AsRef<str>,
55{
56 match CString::new(string.as_ref().as_bytes()) {
57 Ok(c) => Ok(c),
58 Err(_) => Err(Error::new(error_message.as_ref().to_string())),
59 }
60}
61
62macro_rules! ffi_try {
63 ( $($function:ident)::*() ) => {
64 ffi_try_impl!($($function)::*())
65 };
66
67 ( $($function:ident)::*( $arg1:expr $(, $arg:expr)* $(,)? ) ) => {
68 ffi_try_impl!($($function)::*($arg1 $(, $arg)* ,))
69 };
70}
71
72macro_rules! ffi_try_impl {
73 ( $($function:ident)::*( $($arg:expr,)*) ) => {{
74 let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();
75 let result = $($function)::*($($arg,)* &mut err);
76 if !err.is_null() {
77 return Err(Error::new($crate::ffi_util::error_message(err)));
78 }
79 result
80 }};
81}