1.1--- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2+++ b/gen.rs Sun May 07 18:06:13 2023 -0400
1.3@@ -0,0 +1,84 @@
1.4+//! demo
1.5+pub use fig::*;
1.6+pub use obj::*;
1.7+use std::ffi::{CStr, CString}; //OsStr,Path
1.8+ //use std::os::unix::ffi::OsStrExt;
1.9+use std::slice;
1.10+use libc::{c_char,size_t};
1.11+
1.12+#[macro_export]
1.13+macro_rules! cdefn {
1.14+ (free $t:tt $n:tt) => {
1.15+ #[no_mangle]
1.16+ pub unsafe extern "C" fn $n(ptr: *mut $t) {
1.17+ if ptr.is_null() {
1.18+ return;
1.19+ }
1.20+ let _ = Box::from_raw(ptr);
1.21+ }
1.22+ };
1.23+ (from_string $t:tt $n:tt) => {
1.24+ #[no_mangle]
1.25+ pub unsafe extern "C" fn $n(ptr: *const c_char) -> *mut $t {
1.26+ assert!(!ptr.is_null());
1.27+ let p = CStr::from_ptr(ptr).to_str().unwrap();
1.28+ Box::into_raw(Box::new(p.into()))
1.29+ }
1.30+ };
1.31+ (json_string $t:tt $r:tt $w:tt) => {
1.32+ #[no_mangle]
1.33+ pub unsafe extern "C" fn $r(ptr: *const c_char) -> *mut $t {
1.34+ assert!(!ptr.is_null());
1.35+ let s = CStr::from_ptr(ptr);
1.36+ Box::into_raw(Box::new($t::from_json_str(&s.to_str().unwrap()).unwrap()))
1.37+ }
1.38+
1.39+ #[no_mangle]
1.40+ pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char {
1.41+ let p = &*ptr;
1.42+ let x = p.to_json_string().unwrap();
1.43+ CString::new(x.as_str().as_bytes()).unwrap().into_raw()
1.44+ }
1.45+ };
1.46+ (ron_string $t:tt $r:tt $w:tt) => {
1.47+ #[no_mangle]
1.48+ pub unsafe extern "C" fn $r(ptr: *const c_char) -> *mut $t {
1.49+ assert!(!ptr.is_null());
1.50+ let s = CStr::from_ptr(ptr);
1.51+ Box::into_raw(Box::new($t::from_ron_str(&s.to_str().unwrap()).unwrap()))
1.52+ }
1.53+
1.54+ #[no_mangle]
1.55+ pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char {
1.56+ let p = &*ptr;
1.57+ let x = p.to_ron_string().unwrap();
1.58+ CString::new(x.as_str().as_bytes()).unwrap().into_raw()
1.59+ }
1.60+ };
1.61+ (bytes $t:tt $r:tt $w:tt) => {
1.62+ #[no_mangle]
1.63+ pub unsafe extern "C" fn $r(ptr: *const u8, len: size_t) -> *mut $t {
1.64+ Box::into_raw(Box::new($t::decode(slice::from_raw_parts(ptr,len)).unwrap()))
1.65+ }
1.66+
1.67+ #[no_mangle]
1.68+ pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut u8 {
1.69+ let p = &*ptr;
1.70+ let mut x = p.encode().unwrap();
1.71+ let r = x.as_mut_ptr();
1.72+ std::mem::forget(x);
1.73+ r
1.74+ }
1.75+ }
1.76+}
1.77+
1.78+cdefn!(free Service free_service);
1.79+cdefn!(from_string Service service_from_string);
1.80+cdefn!(json_string Service service_from_json_string service_to_json_string);
1.81+cdefn!(ron_string Service service_from_ron_string service_to_ron_string);
1.82+cdefn!(bytes Service service_decode service_encode);
1.83+cdefn!(free CustomService free_custom_service);
1.84+cdefn!(from_string CustomService custom_service_from_string);
1.85+cdefn!(json_string CustomService custom_service_from_json_string custom_service_to_json_string);
1.86+cdefn!(ron_string CustomService custom_service_from_ron_string custom_service_to_ron_string);
1.87+cdefn!(bytes CustomService custom_service_decode custom_service_encode);