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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#![warn(unsafe_op_in_unsafe_fn)]
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
use ffizz_passby::{PassByPointer, PassByValue};
mod hittr {
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Status {
Ready,
Running { count: u32 },
Failed,
}
pub struct System {
pub status: Status,
}
impl System {
pub fn new() -> System {
System {
status: Status::Ready,
}
}
pub fn new_network(_port: u16) -> Result<System, ()> {
Ok(System {
status: Status::Ready,
})
}
pub fn run(&mut self) {
if self.status != Status::Ready {
self.status = Status::Failed;
} else {
self.status = Status::Running { count: 0 };
}
}
pub fn count_hit(&mut self) {
if let Status::Running { count } = self.status {
if count >= 5 {
self.status = Status::Failed;
return;
}
self.status = Status::Running { count: count + 1 };
} else {
self.status = Status::Failed;
}
}
}
}
mod status {
use super::hittr::Status;
use ffizz_passby::PassByValue;
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct hittr_status_t {
pub status: u8,
pub count: u32,
}
pub const HITTR_STATUS_READY: u8 = 1;
pub const HITTR_STATUS_RUNNING: u8 = 2;
pub const HITTR_STATUS_FAILED: u8 = 3;
impl PassByValue for Status {
type CType = hittr_status_t;
unsafe fn from_ctype(cval: hittr_status_t) -> Self {
match cval.status {
HITTR_STATUS_READY => Status::Ready,
HITTR_STATUS_RUNNING => Status::Running { count: cval.count },
HITTR_STATUS_FAILED => Status::Failed,
_ => panic!("invalid status value"),
}
}
fn into_ctype(self) -> hittr_status_t {
match self {
Status::Ready => hittr_status_t {
status: HITTR_STATUS_READY,
count: 0,
},
Status::Running { count } => hittr_status_t {
status: HITTR_STATUS_RUNNING,
count,
},
Status::Failed => hittr_status_t {
status: HITTR_STATUS_FAILED,
count: 0,
},
}
}
}
}
mod system {
use super::hittr::System;
use ffizz_passby::PassByPointer;
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct hittr_system_t(pub System);
impl PassByPointer for hittr_system_t {}
}
use hittr::*;
use status::*;
use system::*;
#[no_mangle]
pub unsafe extern "C" fn hittr_system_new() -> *mut hittr_system_t {
let sys = System::new();
unsafe { hittr_system_t(sys).return_ptr() }
}
#[no_mangle]
pub unsafe extern "C" fn hittr_system_new_network(
system_out: *mut *mut hittr_system_t,
port: u16,
) -> bool {
if let Ok(sys) = System::new_network(port) {
unsafe { hittr_system_t(sys).ptr_to_arg_out(system_out) }
true
} else {
false
}
}
#[no_mangle]
pub unsafe extern "C" fn hittr_system_free(system: *mut hittr_system_t) {
let system = unsafe { hittr_system_t::take_from_ptr_arg(system) };
drop(system); }
#[no_mangle]
pub unsafe extern "C" fn hittr_system_run(system: *mut hittr_system_t) {
let system = &mut unsafe { hittr_system_t::from_ptr_arg_ref_mut(system) }.0;
system.run();
}
#[no_mangle]
pub unsafe extern "C" fn hittr_system_count_hit(system: *mut hittr_system_t) {
let system = &mut unsafe { hittr_system_t::from_ptr_arg_ref_mut(system) }.0;
system.count_hit();
}
#[no_mangle]
pub unsafe extern "C" fn hittr_system_status(system: *const hittr_system_t) -> hittr_status_t {
let system = &unsafe { hittr_system_t::from_ptr_arg_ref(system) }.0;
unsafe { system.status.return_val() }
}
fn main() {
let sys = unsafe { hittr_system_new() };
let st = unsafe { hittr_system_status(sys) };
assert_eq!(st.status, HITTR_STATUS_READY);
assert_eq!(st.count, 0);
unsafe { hittr_system_run(sys) };
let st = unsafe { hittr_system_status(sys) };
assert_eq!(st.status, HITTR_STATUS_RUNNING);
assert_eq!(st.count, 0);
for i in 1..=5 {
unsafe { hittr_system_count_hit(sys) };
let st = unsafe { hittr_system_status(sys) };
assert_eq!(st.status, HITTR_STATUS_RUNNING);
assert_eq!(st.count, i);
}
unsafe { hittr_system_count_hit(sys) }; let st = unsafe { hittr_system_status(sys) };
assert_eq!(st.status, HITTR_STATUS_FAILED);
assert_eq!(st.count, 0);
unsafe { hittr_system_free(sys) };
let mut sys: *mut hittr_system_t = std::ptr::null_mut();
assert!(unsafe { hittr_system_new_network(&mut sys as *mut *mut hittr_system_t, 1300) });
let st = unsafe { hittr_system_status(sys) };
assert_eq!(st.status, HITTR_STATUS_READY);
assert_eq!(st.count, 0);
}