11use std:: ops:: { Deref , DerefMut } ;
22use defines:: AfError ;
33use std:: error:: Error ;
4- use std:: marker:: { Send , Sync } ;
54use std:: sync:: RwLock ;
65
7- /// Signature of callback function to be called to handle errors
8- pub type ErrorCallback = Fn ( AfError ) ;
6+ /// Signature of error handling callback function
7+ pub type ErrorCallback = fn ( AfError ) ;
98
10- /// Wrap ErrorCallback function pointer inside a structure
11- /// to enable implementing Send, Sync traits on it.
12- pub struct Callback < ' cblifetime > {
13- ///Reference to a valid error callback function
14- ///Make sure this callback stays relevant throughout the lifetime of application.
15- pub cb : & ' cblifetime ErrorCallback ,
9+ /// Structure holding handle to callback function
10+ pub struct Callback {
11+ cb : ErrorCallback ,
1612}
1713
18- // Implement Send, Sync traits for Callback structure to
19- // enable the user of Callback function pointer in conjunction
20- // with threads using a mutex.
21- unsafe impl < ' cblifetime > Send for Callback < ' cblifetime > { }
22- unsafe impl < ' cblifetime > Sync for Callback < ' cblifetime > { }
14+ impl Callback {
15+ /// Associated function to create a new Callback object
16+ pub fn new ( callback : ErrorCallback ) -> Self {
17+ Callback { cb : callback}
18+ }
19+
20+ /// call invokes the error callback with `error_code`.
21+ pub fn call ( & self , error_code : AfError ) {
22+ ( self . cb ) ( error_code)
23+ }
24+ }
2325
24- pub const DEFAULT_HANDLE_ERROR : Callback < ' static > = Callback { cb : & handle_error_general} ;
26+ /// Default error handling callback provided by ArrayFire crate
27+ pub fn handle_error_general ( error_code : AfError ) {
28+ match error_code {
29+ AfError :: SUCCESS => { } , /* No-op */
30+ _ => panic ! ( "Error message: {}" , error_code. description( ) ) ,
31+ }
32+ }
2533
2634lazy_static ! {
27- static ref ERROR_HANDLER_LOCK : RwLock < Callback < ' static > > =
28- RwLock :: new( DEFAULT_HANDLE_ERROR ) ;
35+ static ref ERROR_HANDLER_LOCK : RwLock < Callback > =
36+ RwLock :: new( Callback :: new ( handle_error_general ) ) ;
2937}
3038
3139/// Register user provided error handler
@@ -45,16 +53,17 @@ lazy_static! {
4553/// }
4654/// }
4755///
48- /// pub const ERR_HANDLE: Callback<'static> = Callback{ cb: &handleError};
49- ///
5056/// fn main() {
51- /// register_error_handler(ERR_HANDLE);
57+ /// //Registering the error handler should be the first call
58+ /// //before any other functions are called if your version
59+ /// //of error is to be used for subsequent function calls
60+ /// register_error_handler(Callback::new(handleError));
5261///
5362/// info();
5463/// }
5564/// ```
5665#[ allow( unused_must_use) ]
57- pub fn register_error_handler ( cb_value : Callback < ' static > ) {
66+ pub fn register_error_handler ( cb_value : Callback ) {
5867 let mut gaurd = match ERROR_HANDLER_LOCK . write ( ) {
5968 Ok ( g) => g,
6069 Err ( _) => panic ! ( "Failed to acquire lock to register error handler" ) ,
@@ -63,22 +72,12 @@ pub fn register_error_handler(cb_value: Callback<'static>) {
6372 * gaurd. deref_mut ( ) = cb_value;
6473}
6574
66- /// Default error handling callback provided by ArrayFire crate
67- pub fn handle_error_general ( error_code : AfError ) {
68- match error_code {
69- AfError :: SUCCESS => { } , /* No-op */
70- _ => panic ! ( "Error message: {}" , error_code. description( ) ) ,
71- }
72- }
73-
7475#[ allow( non_snake_case) ]
7576pub fn HANDLE_ERROR ( error_code : AfError ) {
7677 let gaurd = match ERROR_HANDLER_LOCK . read ( ) {
7778 Ok ( g) => g,
7879 Err ( _) => panic ! ( "Failed to acquire lock while handling FFI return value" ) ,
7980 } ;
8081
81- let func = gaurd. deref ( ) . cb ;
82-
83- func ( error_code) ;
82+ ( * gaurd. deref ( ) ) . call ( error_code) ;
8483}
0 commit comments