changelog shortlog graph tags branches changeset file revisions annotate raw help

Mercurial > demo / lib.rs

revision 7: 315fedf35bc7
parent 3: 8f59e2f1b8c4
child 8: bebb76da449c
     1.1--- a/lib.rs	Sun May 07 01:32:31 2023 -0400
     1.2+++ b/lib.rs	Sun May 07 18:06:13 2023 -0400
     1.3@@ -1,3 +1,118 @@
     1.4-//! demo_ffi
     1.5-pub use obj::Service;
     1.6-pub use fig;
     1.7+//! demo/lib.rs --- generated by DEMO:RS-MACROEXPAND
     1.8+extern crate obj;
     1.9+extern crate libc;
    1.10+//pub use fig::*;
    1.11+use obj::{Objective,Service,CustomService};
    1.12+use std::ffi::{CStr, CString};
    1.13+use std::slice;
    1.14+use libc::{c_char, size_t};
    1.15+#[no_mangle]
    1.16+pub unsafe extern "C" fn free_service(ptr: *mut Service) {
    1.17+    if ptr.is_null() {
    1.18+        return;
    1.19+    }
    1.20+    let _ = Box::from_raw(ptr);
    1.21+}
    1.22+#[no_mangle]
    1.23+pub unsafe extern "C" fn service_from_string(ptr: *const c_char) -> *mut Service {
    1.24+  assert!(!ptr.is_null());
    1.25+  let p = CStr::from_ptr(ptr).to_str().unwrap();
    1.26+  Box::into_raw(Box::new(p.into()))
    1.27+}
    1.28+#[no_mangle]
    1.29+pub unsafe extern "C" fn service_from_json_string(ptr: *const c_char) -> *mut Service {
    1.30+  assert!(!ptr.is_null());
    1.31+  let s = CStr::from_ptr(ptr);
    1.32+  Box::into_raw(Box::new(Service::from_json_str(&s.to_str().unwrap()).unwrap()))
    1.33+}
    1.34+#[no_mangle]
    1.35+pub unsafe extern "C" fn service_to_json_string(ptr: *const Service) -> *mut c_char {
    1.36+    let p = &*ptr;
    1.37+    let x = p.to_json_string().unwrap();
    1.38+    CString::new(x.as_str().as_bytes()).unwrap().into_raw()
    1.39+}
    1.40+#[no_mangle]
    1.41+pub unsafe extern "C" fn service_from_ron_string(ptr: *const c_char) -> *mut Service {
    1.42+  assert!(!ptr.is_null());
    1.43+  let s = CStr::from_ptr(ptr);
    1.44+  Box::into_raw(Box::new(Service::from_ron_str(&s.to_str().unwrap()).unwrap()))
    1.45+}
    1.46+#[no_mangle]
    1.47+pub unsafe extern "C" fn service_to_ron_string(ptr: *const Service) -> *mut c_char {
    1.48+    let p = &*ptr;
    1.49+    let x = p.to_ron_string().unwrap();
    1.50+    CString::new(x.as_str().as_bytes()).unwrap().into_raw()
    1.51+}
    1.52+#[no_mangle]
    1.53+pub unsafe extern "C" fn service_decode(ptr: *const u8, len: size_t) -> *mut Service {
    1.54+    Box::into_raw(Box::new(Service::decode(slice::from_raw_parts(ptr, len)).unwrap()))
    1.55+}
    1.56+#[no_mangle]
    1.57+pub unsafe extern "C" fn service_encode(ptr: *const Service) -> *mut u8 {
    1.58+    let p = &*ptr;
    1.59+    let mut x = p.encode().unwrap();
    1.60+    let r = x.as_mut_ptr();
    1.61+    std::mem::forget(x);
    1.62+    r
    1.63+}
    1.64+#[no_mangle]
    1.65+pub unsafe extern "C" fn free_custom_service(ptr: *mut CustomService) {
    1.66+    if ptr.is_null() {
    1.67+        return;
    1.68+    }
    1.69+    let _ = Box::from_raw(ptr);
    1.70+}
    1.71+#[no_mangle]
    1.72+pub unsafe extern "C" fn custom_service_from_string(
    1.73+    ptr: *const c_char,
    1.74+) -> *mut CustomService {
    1.75+  assert!(!ptr.is_null());
    1.76+  let p = CStr::from_ptr(ptr).to_str().unwrap();
    1.77+  Box::into_raw(Box::new(p.into()))
    1.78+}
    1.79+#[no_mangle]
    1.80+pub unsafe extern "C" fn custom_service_from_json_string(ptr: *const c_char) -> *mut CustomService {
    1.81+  assert!(!ptr.is_null());
    1.82+  let s = CStr::from_ptr(ptr);
    1.83+  Box::into_raw(Box::new(CustomService::from_json_str(&s.to_str().unwrap()).unwrap()))}
    1.84+#[no_mangle]
    1.85+pub unsafe extern "C" fn custom_service_to_json_string(
    1.86+    ptr: *const CustomService,
    1.87+) -> *mut c_char {
    1.88+    let p = &*ptr;
    1.89+    let x = p.to_json_string().unwrap();
    1.90+    CString::new(x.as_str().as_bytes()).unwrap().into_raw()
    1.91+}
    1.92+#[no_mangle]
    1.93+pub unsafe extern "C" fn custom_service_from_ron_string(
    1.94+    ptr: *const c_char,
    1.95+) -> *mut CustomService {
    1.96+  assert!(!ptr.is_null());
    1.97+  let s = CStr::from_ptr(ptr);
    1.98+  Box::into_raw(Box::new(CustomService::from_ron_str(&s.to_str().unwrap()).unwrap()))
    1.99+}
   1.100+#[no_mangle]
   1.101+pub unsafe extern "C" fn custom_service_to_ron_string(
   1.102+    ptr: *const CustomService,
   1.103+) -> *mut c_char {
   1.104+    let p = &*ptr;
   1.105+    let x = p.to_ron_string().unwrap();
   1.106+    CString::new(x.as_str().as_bytes()).unwrap().into_raw()
   1.107+}
   1.108+#[no_mangle]
   1.109+pub unsafe extern "C" fn custom_service_decode(
   1.110+    ptr: *const u8,
   1.111+    len: size_t,
   1.112+) -> *mut CustomService {
   1.113+    Box::into_raw(
   1.114+        Box::new(CustomService::decode(slice::from_raw_parts(ptr, len)).unwrap()),
   1.115+    )
   1.116+}
   1.117+#[no_mangle]
   1.118+pub unsafe extern "C" fn custom_service_encode(ptr: *const CustomService) -> *mut u8 {
   1.119+    let p = &*ptr;
   1.120+    let mut x = p.encode().unwrap();
   1.121+    let r = x.as_mut_ptr();
   1.122+    std::mem::forget(x);
   1.123+    r
   1.124+}