changeset 7: |
315fedf35bc7 |
author: |
ellis <ellis@rwest.io> |
date: |
Sun, 07 May 2023 18:06:13 -0400 |
permissions: |
-rw-r--r-- |
description: |
rust ffi macros, general stuff |
4 use std::ffi::{CStr, CString}; //OsStr,Path 5 //use std::os::unix::ffi::OsStrExt; 7 use libc::{c_char,size_t}; 11 (free $t:tt $n:tt) => { 13 pub unsafe extern "C" fn $n(ptr: *mut $t) { 17 let _ = Box::from_raw(ptr); 20 (from_string $t:tt $n:tt) => { 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())) 28 (json_string $t:tt $r:tt $w:tt) => { 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())) 37 pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char { 39 let x = p.to_json_string().unwrap(); 40 CString::new(x.as_str().as_bytes()).unwrap().into_raw() 43 (ron_string $t:tt $r:tt $w:tt) => { 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())) 52 pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut c_char { 54 let x = p.to_ron_string().unwrap(); 55 CString::new(x.as_str().as_bytes()).unwrap().into_raw() 58 (bytes $t:tt $r:tt $w:tt) => { 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())) 65 pub unsafe extern "C" fn $w(ptr: *const $t) -> *mut u8 { 67 let mut x = p.encode().unwrap(); 68 let r = x.as_mut_ptr(); 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);