aboutsummaryrefslogtreecommitdiff
path: root/bindings/c/src/lib.rs
diff options
context:
space:
mode:
author1992414357@qq.com <1992414357@qq.com>2025-05-29 14:20:04 +0800
committer1992414357@qq.com <1992414357@qq.com>2025-05-29 14:20:04 +0800
commit3d23632d7c6330e0ec98dad53656dfed5f5767b0 (patch)
treed8d59726ca5b046b55ba502f1b8d5ba3a561a569 /bindings/c/src/lib.rs
parent9d3d6d4195e58c51166811d8357a331d53c4cdd4 (diff)
1. 优化了服务器的消息处理方式
2. 将c和c#的绑定移动到了./bindings/文件夹
Diffstat (limited to 'bindings/c/src/lib.rs')
-rw-r--r--bindings/c/src/lib.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/bindings/c/src/lib.rs b/bindings/c/src/lib.rs
new file mode 100644
index 0000000..6efd0e9
--- /dev/null
+++ b/bindings/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 str_rs_to_c(s: String) -> (CString, *const c_char) {
+ let c_string = CString::new(s).expect("");
+ let ptr = c_string.as_ptr();
+ (c_string, ptr)
+}
+
+pub fn str_c_to_rs(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