From c0244a9d6f657934313e03f67a4b9fa9d93d34b7 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Wed, 28 May 2025 09:11:31 +0800 Subject: . --- core_c/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'core_c/src') diff --git a/core_c/src/lib.rs b/core_c/src/lib.rs index e69de29..bdeb7ef 100644 --- a/core_c/src/lib.rs +++ b/core_c/src/lib.rs @@ -0,0 +1,41 @@ +use std::ffi::{c_char, CStr, CString}; + +pub mod data_c; +pub mod service_c; + +pub fn string_to_c_char_safe(s: String) -> (CString, *const c_char) { + let c_string = CString::new(s).expect(""); + let ptr = c_string.as_ptr(); + (c_string, ptr) +} + +pub fn c_char_to_string_safe(ptr: *const c_char) -> Option { + if ptr.is_null() { + return None; + } + unsafe { + CStr::from_ptr(ptr) + .to_str() + .ok() + .map(|s| s.to_owned()) + } +} + +#[macro_export] +macro_rules! box_into_raw { + ($expr:expr) => { + $crate::box_into_raw!(@inner $expr) + }; + + ($expr:expr => $t:ty) => { + $crate::box_into_raw!(@inner ($expr) as $t) + }; + + (@inner $expr:expr) => { + ::std::boxed::Box::into_raw(::std::boxed::Box::new($expr)) + }; + + (@inner ($expr:expr) as $t:ty) => { + ::std::boxed::Box::into_raw(::std::boxed::Box::new($expr) as ::std::boxed::Box<$t>) + }; +} \ No newline at end of file -- cgit