changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / src/lib.rs

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