#![cfg(feature = "ffi")]
use super::*;
use libc::{c_char, c_int, c_uchar, size_t};
use std::ffi::CStr;
use std::mem;
#[no_mangle]
#[repr(C)]
pub struct returned {
val: *mut c_uchar,
len: c_int,
cap: size_t,
}
impl Drop for returned {
fn drop(&mut self) {
unsafe { Vec::<c_uchar>::from_raw_parts(self.val, self.len as usize, self.cap) };
}
}
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn google(query: *const c_char) -> *mut returned {
match unsafe {
CStr::from_ptr(query)
.to_str()
.map_err(Box::from)
.and_then(native::google)
} {
Ok(x) => {
let mut v = to_vec_named(&x).unwrap();
let ret = Box::into_raw(
returned {
val: v.as_mut_ptr(),
len: v.len() as c_int,
cap: v.capacity(),
}
.into(),
);
mem::forget(v); ret
}
Err(x) => {
let mut v = to_vec(&x.to_string()).unwrap();
let ret = Box::into_raw(
returned {
val: v.as_mut_ptr(),
len: v.len() as c_int,
cap: v.capacity(),
}
.into(),
);
mem::forget(v);
ret
}
}
}
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn freeGResults(ret: *mut returned) {
unsafe { Box::from_raw(ret) };
}