1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::panic;
use std::path::PathBuf;
#[repr(C)]
#[derive(PartialEq, Debug)]
pub enum FCPResponseStatus {
FCPNoError = 0,
FCPUnclassifiedError = 1,
FCPCallerError = 2,
FCPReceiverError = 3,
}
pub trait CodeAndMessage {
fn set_error(&mut self, code_and_message: (FCPResponseStatus, *const libc::c_char));
}
#[macro_export]
macro_rules! code_and_message_impl {
{ $response:ty } => {
impl CodeAndMessage for $response {
fn set_error(&mut self, (code, message): (FCPResponseStatus, *const libc::c_char)) {
self.status_code = code;
self.error_msg = message;
}
}
}
}
pub fn rust_str_to_c_str<T: Into<String>>(s: T) -> *mut libc::c_char {
CString::new(s.into()).unwrap().into_raw()
}
pub unsafe fn free_c_str(ptr: *mut libc::c_char) {
if !ptr.is_null() {
let _ = CString::from_raw(ptr);
}
}
pub fn raw_ptr<T>(thing: T) -> *mut T {
Box::into_raw(Box::new(thing))
}
pub unsafe fn c_str_to_rust_str<'a>(x: *const libc::c_char) -> Cow<'a, str> {
if x.is_null() {
Cow::from("")
} else {
CStr::from_ptr(x).to_string_lossy()
}
}
pub unsafe fn cast_const<'a, T>(x: *mut T) -> &'a T {
assert!(!x.is_null(), "Object argument was null");
(&(*x))
}
pub unsafe fn c_str_to_pbuf(x: *const libc::c_char) -> PathBuf {
PathBuf::from(String::from(c_str_to_rust_str(x)))
}
pub fn catch_panic_response<F, T>(callback: F) -> *mut T
where
T: Default + CodeAndMessage,
F: FnOnce() -> *mut T,
{
let maybe_panic = panic::catch_unwind(panic::AssertUnwindSafe(callback));
match maybe_panic {
Ok(return_value) => return_value,
Err(panic) => {
let error_msg = match panic.downcast_ref::<&'static str>() {
Some(message) => message,
_ => "no unwind information",
};
let mut response = T::default();
let message = CString::new(format!("Rust panic: {}", error_msg))
.unwrap()
.into_raw();
response.set_error((FCPResponseStatus::FCPUnclassifiedError, message));
raw_ptr(response)
}
}
}