-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy patherror.rs
110 lines (97 loc) · 2.9 KB
/
error.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
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
extern crate libc;
use self::libc::{c_char, c_int};
use crate::defines::AfError;
use crate::util::{free_host, DimT, MutDimT};
use std::error::Error;
use std::ffi::CStr;
use std::ops::{Deref, DerefMut};
use std::sync::RwLock;
#[allow(dead_code)]
extern "C" {
fn af_get_last_error(str: *mut *mut c_char, len: *mut DimT) -> c_int;
}
/// Signature of error handling callback function
pub type ErrorCallback = fn(AfError);
/// Structure holding handle to callback function
pub struct Callback {
cb: ErrorCallback,
}
impl Callback {
/// Associated function to create a new Callback object
pub fn new(callback: ErrorCallback) -> Self {
Self { cb: callback }
}
/// call invokes the error callback with `error_code`.
pub fn call(&self, error_code: AfError) {
(self.cb)(error_code)
}
}
/// Default error handling callback provided by ArrayFire crate
pub fn handle_error_general(error_code: AfError) {
match error_code {
AfError::SUCCESS => {} /* No-op */
_ => panic!(
"Error message: {}\nLast error: {}",
error_code.description(),
get_last_error()
),
}
}
lazy_static! {
static ref ERROR_HANDLER_LOCK: RwLock<Callback> =
RwLock::new(Callback::new(handle_error_general));
}
/// Register user provided error handler
///
/// # Examples
/// ```
/// #[macro_use]
/// extern crate arrayfire;
///
/// use arrayfire::{AfError, Callback, info, register_error_handler};
/// use std::error::Error;
///
/// fn handleError(error_code: AfError) {
/// match error_code {
/// AfError::SUCCESS => {}, /* No-op */
/// _ => panic!("Error message: {}", error_code.description()),
/// }
/// }
///
/// fn main() {
/// //Registering the error handler should be the first call
/// //before any other functions are called if your version
/// //of error is to be used for subsequent function calls
/// register_error_handler(Callback::new(handleError));
///
/// info();
/// }
/// ```
#[allow(unused_must_use)]
pub fn register_error_handler(cb_value: Callback) {
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;
}
#[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"),
};
(*gaurd.deref()).call(error_code);
}
pub fn get_last_error() -> String {
let result: String;
unsafe {
let mut tmp: *mut c_char = ::std::ptr::null_mut();
let mut len: DimT = 0;
let err_val = af_get_last_error(&mut tmp, &mut len as MutDimT);
HANDLE_ERROR(AfError::from(err_val));
result = CStr::from_ptr(tmp).to_string_lossy().into_owned();
free_host(tmp);
}
result
}