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
use std::ops::{Deref, DerefMut};
use defines::AfError;
use std::error::Error;
use std::marker::{Send, Sync};
use std::sync::RwLock;
pub type ErrorCallback = Fn(AfError);
pub struct Callback<'cblifetime> {
pub cb: &'cblifetime ErrorCallback,
}
unsafe impl<'cblifetime> Send for Callback<'cblifetime> {}
unsafe impl<'cblifetime> Sync for Callback<'cblifetime> {}
pub const DEFAULT_HANDLE_ERROR: Callback<'static> = Callback{cb: &handle_error_general};
lazy_static! {
static ref ERROR_HANDLER_LOCK: RwLock< Callback<'static> > =
RwLock::new(DEFAULT_HANDLE_ERROR);
}
#[allow(unused_must_use)]
pub fn register_error_handler(cb_value: Callback<'static>) {
let mut gaurd = match ERROR_HANDLER_LOCK.write() {
Ok(g) => g,
Err(_)=> panic!("Failed to acquire lock to register error handler"),
};
*gaurd.deref_mut() = cb_value;
}
pub fn handle_error_general(error_code: AfError) {
match error_code {
AfError::SUCCESS => {},
_ => panic!("Error message: {}", error_code.description()),
}
}
#[allow(non_snake_case)]
pub fn HANDLE_ERROR(error_code: AfError) {
let gaurd = match ERROR_HANDLER_LOCK.read() {
Ok(g) => g,
Err(_)=> panic!("Failed to acquire lock while handling FFI return value"),
};
let func = gaurd.deref().cb;
func(error_code);
}