diff options
| author | 1992414357@qq.com <1992414357@qq.com> | 2025-05-28 09:11:31 +0800 |
|---|---|---|
| committer | 1992414357@qq.com <1992414357@qq.com> | 2025-05-28 09:11:31 +0800 |
| commit | c0244a9d6f657934313e03f67a4b9fa9d93d34b7 (patch) | |
| tree | 2da30c3984f5182e1bd3369de148abaa05afcbf8 /core_c | |
| parent | adc42fdf2050e77de50952bc8629684f1b3cf0ce (diff) | |
.
Diffstat (limited to 'core_c')
| -rw-r--r-- | core_c/src/lib.rs | 41 |
1 files changed, 41 insertions, 0 deletions
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<String> { + 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 |
