1use std::{
2 ffi::{c_char, CString},
3 ptr,
4};
5
6pub mod logger;
7pub mod net;
8
9unsafe fn cstr_to_str<'a>(cstr: *const c_char) -> Option<&'a str> {
10 use std::ffi::CStr;
11 if cstr.is_null() {
12 return None;
13 }
14
15 let cstr = CStr::from_ptr(cstr);
16 return match cstr.to_str() {
17 Ok(str) => Some(str),
18 Err(_) => None,
19 };
20}
21
22unsafe fn disown_str_to_cstr(s: &str) -> *const c_char {
23 let Ok(c_str) = CString::new(s) else {
24 return ptr::null();
25 };
26
27 return c_str.into_raw() as *const c_char;
28}
29
30unsafe fn drop_cstr(s: *const c_char) {
31 if s.is_null() {
32 return;
33 }
34
35 let _ = CString::from_raw(s as *mut c_char);
36}
37
38unsafe fn move_to_heap<T>(value: T) -> *mut T {
39 return Box::into_raw(Box::new(value));
40}
41
42unsafe fn drop_from_heap<T>(value: *mut T) {
43 if !value.is_null() {
44 drop(Box::from_raw(value));
45 }
46}
47
48unsafe fn ptr_to_slice<'a, T>(ptr: *const T, size: usize) -> &'a [T] {
49 return std::slice::from_raw_parts(ptr, size);
50}