fix cross platform C ffi

main
Ziyang Hu 2 years ago
parent ba5e36d7b6
commit f33bda3d98

@ -16,4 +16,5 @@ cp target/release/libcozo_c.a release/libcozo_c-${VERSION}-mac-${ARCH}.a
cp target/release/libcozo_c.dylib release/libcozo_c-${VERSION}-mac-${ARCH}.dylib
strip release/cozoserver-${VERSION}-mac-${ARCH}
gzip release/*
gzip release/*
cp cozo-lib-c/cozo_c.h release/cozo_c.h

@ -24,7 +24,7 @@ extern "C" {
* otherwise a pointer to a C-string containing the error message will be returned.
* The returned C-string must be freed with `cozo_free_str`.
*/
int8_t *cozo_open_db(const int8_t *path, int32_t *db_id);
char *cozo_open_db(const char *path, int32_t *db_id);
/**
* Close a database.
@ -39,7 +39,7 @@ bool cozo_close_db(int32_t id);
/**
* Run query against a database.
*
* `db_id`: the ID representing the database to run the query.
* `db_id`: the ID representing the database to run the query.
* `script_raw`: a UTF-8 encoded C-string for the CozoScript to execute.
* `params_raw`: a UTF-8 encoded C-string for the params of the query,
* in JSON format. You must always pass in a valid JSON map,
@ -52,10 +52,7 @@ bool cozo_close_db(int32_t id);
* If `*errored` is false, then the string contains the JSON return value of the query.
* If `*errored` is true, then the string contains the error message.
*/
int8_t *cozo_run_query(int32_t db_id,
const int8_t *script_raw,
const int8_t *params_raw,
bool *errored);
char *cozo_run_query(int32_t db_id, const char *script_raw, const char *params_raw, bool *errored);
/**
* Free any C-string returned from the Cozo C API.
@ -63,7 +60,7 @@ int8_t *cozo_run_query(int32_t db_id,
*
* `s`: the C-string to free.
*/
void cozo_free_str(int8_t *s);
void cozo_free_str(char *s);
#ifdef __cplusplus
} // extern "C"

@ -4,7 +4,7 @@
#![warn(rust_2018_idioms, future_incompatible)]
use std::collections::BTreeMap;
use std::ffi::{CStr, CString};
use std::ffi::{c_char, CStr, CString};
use std::ptr::null_mut;
use std::sync::atomic::{AtomicI32, Ordering};
use std::sync::Mutex;
@ -32,7 +32,7 @@ lazy_static! {
/// otherwise a pointer to a C-string containing the error message will be returned.
/// The returned C-string must be freed with `cozo_free_str`.
#[no_mangle]
pub unsafe extern "C" fn cozo_open_db(path: *const i8, db_id: &mut i32) -> *mut i8 {
pub unsafe extern "C" fn cozo_open_db(path: *const c_char, db_id: &mut i32) -> *mut c_char {
let path = match CStr::from_ptr(path).to_str() {
Ok(p) => p,
Err(err) => return CString::new(format!("{}", err)).unwrap().into_raw(),
@ -82,10 +82,10 @@ pub unsafe extern "C" fn cozo_close_db(id: i32) -> bool {
#[no_mangle]
pub unsafe extern "C" fn cozo_run_query(
db_id: i32,
script_raw: *const i8,
params_raw: *const i8,
script_raw: *const c_char,
params_raw: *const c_char,
errored: &mut bool,
) -> *mut i8 {
) -> *mut c_char {
let script = match CStr::from_ptr(script_raw).to_str() {
Ok(p) => p,
Err(err) => {
@ -152,6 +152,6 @@ pub unsafe extern "C" fn cozo_run_query(
///
/// `s`: the C-string to free.
#[no_mangle]
pub unsafe extern "C" fn cozo_free_str(s: *mut i8) {
pub unsafe extern "C" fn cozo_free_str(s: *mut c_char) {
let _ = CString::from_raw(s);
}

Loading…
Cancel
Save