changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / gen.rs

changeset 12: c520966de7fa
parent: 315fedf35bc7
author: ellis <ellis@rwest.io>
date: Mon, 15 May 2023 21:40:24 -0400
permissions: -rw-r--r--
description: added quiche.h and some ui work
1 //! demo
2 pub use fig::*;
3 pub use obj::*;
4 use std::ffi::{CStr, CString}; //OsStr,Path
5  //use std::os::unix::ffi::OsStrExt;
6 use std::slice;
7 use libc::{c_char,size_t};
8 
9 #[macro_export]
10 macro_rules! cdefn {
11  (free $t:tt $n:tt) => {
12  #[no_mangle]
13  pub unsafe extern "C" fn $n(ptr: *mut $t) {
14  if ptr.is_null() {
15  return;
16  }
17  let _ = Box::from_raw(ptr);
18  }
19  };
20  (from_string $t:tt $n:tt) => {
21  #[no_mangle]
22  pub unsafe extern "C" fn $n(ptr: *const c_char) -> *mut $t {
23  assert!(!ptr.is_null());
24  let p = CStr::from_ptr(ptr).to_str().unwrap();
25  Box::into_raw(Box::new(p.into()))
26  }
27  };
28  (json_string $t:tt $r:tt $w:tt) => {
29  #[no_mangle]
30  pub unsafe extern "C" fn $r(ptr: *const c_char) -> *mut $t {
31  assert!(!ptr.is_null());
32  let s = CStr::from_ptr(ptr);
33  Box::into_raw(Box::new($t::from_json_str(&s.to_str().unwrap()).unwrap()))
34  }
35 
36  #[no_mangle]
37  pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char {
38  let p = &*ptr;
39  let x = p.to_json_string().unwrap();
40  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
41  }
42  };
43  (ron_string $t:tt $r:tt $w:tt) => {
44  #[no_mangle]
45  pub unsafe extern "C" fn $r(ptr: *const c_char) -> *mut $t {
46  assert!(!ptr.is_null());
47  let s = CStr::from_ptr(ptr);
48  Box::into_raw(Box::new($t::from_ron_str(&s.to_str().unwrap()).unwrap()))
49  }
50 
51  #[no_mangle]
52  pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char {
53  let p = &*ptr;
54  let x = p.to_ron_string().unwrap();
55  CString::new(x.as_str().as_bytes()).unwrap().into_raw()
56  }
57  };
58  (bytes $t:tt $r:tt $w:tt) => {
59  #[no_mangle]
60  pub unsafe extern "C" fn $r(ptr: *const u8, len: size_t) -> *mut $t {
61  Box::into_raw(Box::new($t::decode(slice::from_raw_parts(ptr,len)).unwrap()))
62  }
63 
64  #[no_mangle]
65  pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut u8 {
66  let p = &*ptr;
67  let mut x = p.encode().unwrap();
68  let r = x.as_mut_ptr();
69  std::mem::forget(x);
70  r
71  }
72  }
73 }
74 
75 cdefn!(free Service free_service);
76 cdefn!(from_string Service service_from_string);
77 cdefn!(json_string Service service_from_json_string service_to_json_string);
78 cdefn!(ron_string Service service_from_ron_string service_to_ron_string);
79 cdefn!(bytes Service service_decode service_encode);
80 cdefn!(free CustomService free_custom_service);
81 cdefn!(from_string CustomService custom_service_from_string);
82 cdefn!(json_string CustomService custom_service_from_json_string custom_service_to_json_string);
83 cdefn!(ron_string CustomService custom_service_from_ron_string custom_service_to_ron_string);
84 cdefn!(bytes CustomService custom_service_decode custom_service_encode);