diff options
Diffstat (limited to 'bindings/c/src/lib.rs')
| -rw-r--r-- | bindings/c/src/lib.rs | 41 |
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 |
