-
Notifications
You must be signed in to change notification settings - Fork 16
/
c_helpers.rs
69 lines (63 loc) · 1.09 KB
/
c_helpers.rs
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
#[doc(hidden)]
#[macro_export]
macro_rules! deref_ptr {
($cfg: ident, $ret: expr) => {{
if $cfg.is_null() {
return $ret;
} else {
&*$cfg
}
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! deref_ptr_mut {
($cfg: ident, $ret: expr) => {{
if $cfg.is_null() {
return $ret;
} else {
&mut *$cfg
}
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! get_slice {
($buff: expr, $buff_size: expr) => {{
std::slice::from_raw_parts($buff, $buff_size).to_owned()
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! get_slice_mut {
($buff: expr, $buff_size: expr) => {{
std::slice::from_raw_parts_mut($buff, $buff_size)
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! get_string {
($ptr: expr) => {{
String::from_utf8(CStr::from_ptr($ptr).to_bytes().to_vec()).unwrap()
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! get_value_or_errno {
($val: expr) => {{
match $val {
Ok(v) => v,
Err(errno) => return errno,
}
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! get_value_or_false {
($val: expr) => {{
match $val {
Ok(v) => v,
Err(_) => return 0,
}
}};
}