changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / lib.rs

changeset 7: 315fedf35bc7
parent: 8f59e2f1b8c4
child: bebb76da449c
author: ellis <ellis@rwest.io>
date: Sun, 07 May 2023 18:06:13 -0400
permissions: -rw-r--r--
description: rust ffi macros, general stuff
1 //! demo/lib.rs --- generated by DEMO:RS-MACROEXPAND
2 extern crate obj;
3 extern crate libc;
4 //pub use fig::*;
5 use obj::{Objective,Service,CustomService};
6 use std::ffi::{CStr, CString};
7 use std::slice;
8 use libc::{c_char, size_t};
9 #[no_mangle]
10 pub unsafe extern "C" fn free_service(ptr: *mut Service) {
11  if ptr.is_null() {
12  return;
13  }
14  let _ = Box::from_raw(ptr);
15 }
16 #[no_mangle]
17 pub unsafe extern "C" fn service_from_string(ptr: *const c_char) -> *mut Service {
18  assert!(!ptr.is_null());
19  let p = CStr::from_ptr(ptr).to_str().unwrap();
20  Box::into_raw(Box::new(p.into()))
21 }
22 #[no_mangle]
23 pub unsafe extern "C" fn service_from_json_string(ptr: *const c_char) -> *mut Service {
24  assert!(!ptr.is_null());
25  let s = CStr::from_ptr(ptr);
26  Box::into_raw(Box::new(Service::from_json_str(&s.to_str().unwrap()).unwrap()))
27 }
28 #[no_mangle]
29 pub unsafe extern "C" fn service_to_json_string(ptr: *const Service) -> *mut c_char {
30  let p = &*ptr;
31  let x = p.to_json_string().unwrap();
32  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
33 }
34 #[no_mangle]
35 pub unsafe extern "C" fn service_from_ron_string(ptr: *const c_char) -> *mut Service {
36  assert!(!ptr.is_null());
37  let s = CStr::from_ptr(ptr);
38  Box::into_raw(Box::new(Service::from_ron_str(&s.to_str().unwrap()).unwrap()))
39 }
40 #[no_mangle]
41 pub unsafe extern "C" fn service_to_ron_string(ptr: *const Service) -> *mut c_char {
42  let p = &*ptr;
43  let x = p.to_ron_string().unwrap();
44  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
45 }
46 #[no_mangle]
47 pub unsafe extern "C" fn service_decode(ptr: *const u8, len: size_t) -> *mut Service {
48  Box::into_raw(Box::new(Service::decode(slice::from_raw_parts(ptr, len)).unwrap()))
49 }
50 #[no_mangle]
51 pub unsafe extern "C" fn service_encode(ptr: *const Service) -> *mut u8 {
52  let p = &*ptr;
53  let mut x = p.encode().unwrap();
54  let r = x.as_mut_ptr();
55  std::mem::forget(x);
56  r
57 }
58 #[no_mangle]
59 pub unsafe extern "C" fn free_custom_service(ptr: *mut CustomService) {
60  if ptr.is_null() {
61  return;
62  }
63  let _ = Box::from_raw(ptr);
64 }
65 #[no_mangle]
66 pub unsafe extern "C" fn custom_service_from_string(
67  ptr: *const c_char,
68 ) -> *mut CustomService {
69  assert!(!ptr.is_null());
70  let p = CStr::from_ptr(ptr).to_str().unwrap();
71  Box::into_raw(Box::new(p.into()))
72 }
73 #[no_mangle]
74 pub unsafe extern "C" fn custom_service_from_json_string(ptr: *const c_char) -> *mut CustomService {
75  assert!(!ptr.is_null());
76  let s = CStr::from_ptr(ptr);
77  Box::into_raw(Box::new(CustomService::from_json_str(&s.to_str().unwrap()).unwrap()))}
78 #[no_mangle]
79 pub unsafe extern "C" fn custom_service_to_json_string(
80  ptr: *const CustomService,
81 ) -> *mut c_char {
82  let p = &*ptr;
83  let x = p.to_json_string().unwrap();
84  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
85 }
86 #[no_mangle]
87 pub unsafe extern "C" fn custom_service_from_ron_string(
88  ptr: *const c_char,
89 ) -> *mut CustomService {
90  assert!(!ptr.is_null());
91  let s = CStr::from_ptr(ptr);
92  Box::into_raw(Box::new(CustomService::from_ron_str(&s.to_str().unwrap()).unwrap()))
93 }
94 #[no_mangle]
95 pub unsafe extern "C" fn custom_service_to_ron_string(
96  ptr: *const CustomService,
97 ) -> *mut c_char {
98  let p = &*ptr;
99  let x = p.to_ron_string().unwrap();
100  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
101 }
102 #[no_mangle]
103 pub unsafe extern "C" fn custom_service_decode(
104  ptr: *const u8,
105  len: size_t,
106 ) -> *mut CustomService {
107  Box::into_raw(
108  Box::new(CustomService::decode(slice::from_raw_parts(ptr, len)).unwrap()),
109  )
110 }
111 #[no_mangle]
112 pub unsafe extern "C" fn custom_service_encode(ptr: *const CustomService) -> *mut u8 {
113  let p = &*ptr;
114  let mut x = p.encode().unwrap();
115  let r = x.as_mut_ptr();
116  std::mem::forget(x);
117  r
118 }