cfasttext_sys/
lib.rs

1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4use std::ffi::CStr;
5use std::os::raw::c_char;
6
7mod binding;
8
9pub use self::binding::*;
10
11/// Convert error message to string
12///
13/// ## Safety
14/// Should only be called in `ffi_try` to ensure that `ptr` isn't null
15pub unsafe fn error_message(ptr: *mut c_char) -> String {
16    let c_str = unsafe { CStr::from_ptr(ptr) };
17    let s = format!("{}", c_str.to_string_lossy());
18    unsafe {
19        cft_str_free(ptr);
20    }
21    s
22}
23
24#[macro_export]
25macro_rules! ffi_try {
26    ($func:ident($($arg:expr),*)) => ({
27        use std::ptr;
28        let mut err = ptr::null_mut();
29        let res = $crate::$func($($arg),*, &mut err);
30        if !err.is_null() {
31            return Err(unsafe { $crate::error_message(err) });
32        }
33        res
34    })
35}