Skip to content

Commit 5ad8866

Browse files
committed
Basic error handling in Array struct
1 parent c5c9ba7 commit 5ad8866

File tree

5 files changed

+158
-34
lines changed

5 files changed

+158
-34
lines changed

examples/helloworld.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
println!("Create 2-by-3 matrix from host data");
4141
let d_dims: Dim4 = Dim4::new(&[2, 3, 1, 1]);
4242
let d_input: [i32; 6] = [1, 2, 3, 4, 5, 6];
43-
let d: Array = Array::new(d_dims, &d_input, af::Aftype::S32);
43+
let d: Array = Array::new(d_dims, &d_input, af::Aftype::S32).ok().unwrap();
4444
af::print(&d);
4545

4646
// printf("Copy last column onto first\n");
@@ -56,5 +56,5 @@ fn main() {
5656
println!("u8 constant array");
5757
let u8_cnst = af::constant(1 as u8, dims);
5858
af::print(&u8_cnst);
59-
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single());
59+
println!("Is u8_cnst array float precision type ? {}", u8_cnst.is_single().ok().unwrap());
6060
}

src/arith/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ macro_rules! arith_scalar_func {
194194
type Output = Array;
195195

196196
fn $fn_name(self, rhs: $rust_type) -> Array {
197-
let cnst_arr = constant(rhs, self.dims());
197+
let cnst_arr = constant(rhs, self.dims().ok().unwrap()).ok().unwrap();
198198
unsafe {
199199
let mut temp: i64 = 0;
200200
$ffi_fn(&mut temp as MutAfArray,

src/array.rs

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate libc;
22

33
use dim4::Dim4;
4+
use defines::AfError;
45
use defines::Aftype;
56
use self::libc::{uint8_t, c_void, c_int, c_uint, c_longlong};
67

@@ -67,78 +68,106 @@ pub struct Array {
6768

6869
macro_rules! is_func {
6970
($fn_name: ident, $ffi_fn: ident) => (
70-
pub fn $fn_name(&self) -> bool {
71+
pub fn $fn_name(&self) -> Result<bool, AfError> {
7172
unsafe {
7273
let mut ret_val: i32 = 0;
73-
$ffi_fn(&mut ret_val as *mut c_int, self.handle as AfArray);
74-
ret_val > 0
74+
let err_val = $ffi_fn(&mut ret_val as *mut c_int, self.handle as AfArray);
75+
match err_val {
76+
0 => Ok(ret_val>0),
77+
_ => Err(AfError::from(err_val)),
78+
}
7579
}
7680
}
7781
)
7882
}
7983

8084
impl Array {
8185
#[allow(unused_mut)]
82-
pub fn new<T>(dims: Dim4, slice: &[T], aftype: Aftype) -> Array {
86+
pub fn new<T>(dims: Dim4, slice: &[T], aftype: Aftype) -> Result<Array, AfError> {
8387
unsafe {
8488
let mut temp: i64 = 0;
85-
af_create_array(&mut temp as MutAfArray, slice.as_ptr() as *const c_void,
86-
dims.ndims() as c_uint, dims.get().as_ptr() as * const c_longlong,
87-
aftype as uint8_t);
88-
Array {handle: temp}
89+
let err_val = af_create_array(&mut temp as MutAfArray,
90+
slice.as_ptr() as *const c_void,
91+
dims.ndims() as c_uint,
92+
dims.get().as_ptr() as * const c_longlong,
93+
aftype as uint8_t);
94+
match err_val {
95+
0 => Ok(Array {handle: temp}),
96+
_ => Err(AfError::from(err_val)),
97+
}
8998
}
9099
}
91100

92-
pub fn elements(&self) -> i64 {
101+
pub fn elements(&self) -> Result<i64, AfError> {
93102
unsafe {
94103
let mut ret_val: i64 = 0;
95-
af_get_elements(&mut ret_val as MutAfArray, self.handle as AfArray);
96-
ret_val
104+
let err_val = af_get_elements(&mut ret_val as MutAfArray, self.handle as AfArray);
105+
match err_val {
106+
0 => Ok(ret_val),
107+
_ => Err(AfError::from(err_val)),
108+
}
97109
}
98110
}
99111

100-
pub fn get_type(&self) -> Aftype {
112+
pub fn get_type(&self) -> Result<Aftype, AfError> {
101113
unsafe {
102114
let mut ret_val: u8 = 0;
103-
af_get_type(&mut ret_val as *mut uint8_t, self.handle as AfArray);
104-
Aftype::from(ret_val)
115+
let err_val = af_get_type(&mut ret_val as *mut uint8_t, self.handle as AfArray);
116+
match err_val {
117+
0 => Ok(Aftype::from(ret_val)),
118+
_ => Err(AfError::from(err_val)),
119+
}
105120
}
106121
}
107122

108-
pub fn dims(&self) -> Dim4 {
123+
pub fn dims(&self) -> Result<Dim4, AfError> {
109124
unsafe {
110125
let mut ret0: i64 = 0;
111126
let mut ret1: i64 = 0;
112127
let mut ret2: i64 = 0;
113128
let mut ret3: i64 = 0;
114-
af_get_dims(&mut ret0 as *mut c_longlong, &mut ret1 as *mut c_longlong,
115-
&mut ret2 as *mut c_longlong, &mut ret3 as *mut c_longlong,
116-
self.handle as AfArray);
117-
Dim4::new(&[ret0 as u64, ret1 as u64, ret2 as u64, ret3 as u64])
129+
let err_val = af_get_dims(&mut ret0 as *mut c_longlong, &mut ret1 as *mut c_longlong,
130+
&mut ret2 as *mut c_longlong, &mut ret3 as *mut c_longlong,
131+
self.handle as AfArray);
132+
match err_val {
133+
0 => Ok(Dim4::new(&[ret0 as u64, ret1 as u64, ret2 as u64, ret3 as u64])),
134+
_ => Err(AfError::from(err_val)),
135+
}
118136
}
119137
}
120138

121-
pub fn numdims(&self) -> u32 {
139+
pub fn numdims(&self) -> Result<u32, AfError> {
122140
unsafe {
123141
let mut ret_val: u32 = 0;
124-
af_get_numdims(&mut ret_val as *mut c_uint, self.handle as AfArray);
125-
ret_val
142+
let err_val = af_get_numdims(&mut ret_val as *mut c_uint, self.handle as AfArray);
143+
match err_val {
144+
0 => Ok(ret_val),
145+
_ => Err(AfError::from(err_val)),
146+
}
126147
}
127148
}
128149

129150
pub fn get(&self) -> i64 {
130151
self.handle
131152
}
132153

133-
pub fn host(&self, data:&mut [f64]) {
154+
pub fn host(&self, data:&mut [f64]) -> Result<(), AfError> {
134155
unsafe {
135-
af_get_data_ptr(data.as_mut_ptr() as *mut c_void, self.handle as AfArray);
156+
let ret_val = af_get_data_ptr(data.as_mut_ptr() as *mut c_void, self.handle as AfArray);
157+
match ret_val {
158+
0 => Ok(()),
159+
_ => Err(AfError::from(ret_val)),
160+
}
136161
}
137162
}
138163

139-
pub fn eval(&self) {
164+
pub fn eval(&self) -> Result<(), AfError> {
140165
unsafe {
141-
af_eval(self.handle as AfArray);
166+
let ret_val = af_eval(self.handle as AfArray);
167+
match ret_val {
168+
0 => Ok(()),
169+
_ => Err(AfError::from(ret_val)),
170+
}
142171
}
143172
}
144173

@@ -166,22 +195,33 @@ impl Clone for Array {
166195
fn clone(&self) -> Array {
167196
unsafe {
168197
let mut temp: i64 = 0;
169-
af_retain_array(&mut temp as MutAfArray, self.handle as AfArray);
170-
Array {handle: temp}
198+
let ret_val = af_retain_array(&mut temp as MutAfArray, self.handle as AfArray);
199+
match ret_val {
200+
0 => Array {handle: temp},
201+
_ => panic!("Weak copy of Array failed with error code: {}", ret_val),
202+
}
171203
}
172204
}
173205
}
174206

175207
impl Drop for Array {
176208
fn drop(&mut self) {
177209
unsafe {
178-
af_release_array(self.handle);
210+
let ret_val = af_release_array(self.handle);
211+
match ret_val {
212+
0 => (),
213+
_ => panic!("Weak copy of Array failed with error code: {}", ret_val),
214+
}
179215
}
180216
}
181217
}
182218

183-
pub fn print(input: &Array) {
219+
pub fn print(input: &Array) -> Result<(), AfError> {
184220
unsafe {
185-
af_print_array(input.get() as AfArray);
221+
let ret_val = af_print_array(input.get() as AfArray);
222+
match ret_val {
223+
0 => Ok(()),
224+
_ => Err(AfError::from(ret_val)),
225+
}
186226
}
187227
}

src/defines.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
1+
#[repr(C)]
2+
#[derive(Clone, Copy)]
3+
pub enum AfError {
4+
///
5+
/// The function returned successfully
6+
///
7+
SUCCESS = 0,
8+
// 100-199 Errors in environment
9+
///
10+
/// The system or device ran out of memory
11+
///
12+
ERR_NO_MEM = 101,
13+
///
14+
/// There was an error in the device driver
15+
///
16+
ERR_DRIVER = 102,
17+
///
18+
/// There was an error with the runtime environment
19+
///
20+
ERR_RUNTIME = 103,
21+
// 200-299 Errors in input parameters
22+
///
23+
/// The input array is not a valid af_array object
24+
///
25+
ERR_INVALID_ARRAY = 201,
26+
///
27+
/// One of the function arguments is incorrect
28+
///
29+
ERR_ARG = 202,
30+
///
31+
/// The size is incorrect
32+
///
33+
ERR_SIZE = 203,
34+
///
35+
/// The type is not suppported by this function
36+
///
37+
ERR_TYPE = 204,
38+
///
39+
/// The type of the input arrays are not compatible
40+
///
41+
ERR_DIFF_TYPE = 205,
42+
///
43+
/// Function does not support GFOR / batch mode
44+
///
45+
ERR_BATCH = 207,
46+
// 300-399 Errors for missing software features
47+
///
48+
/// The option is not supported
49+
///
50+
ERR_NOT_SUPPORTED = 301,
51+
///
52+
/// This build of ArrayFire does not support this feature
53+
///
54+
ERR_NOT_CONFIGURED = 302,
55+
// 400-499 Errors for missing hardware features
56+
///
57+
/// This device does not support double
58+
///
59+
ERR_NO_DBL = 401,
60+
///
61+
/// This build of ArrayFire was not built with graphics or this device does
62+
/// not support graphics
63+
///
64+
ERR_NO_GFX = 402,
65+
// 900-999 Errors from upstream libraries and runtimes
66+
///
67+
/// There was an internal error either in ArrayFire or in a project
68+
/// upstream
69+
///
70+
ERR_INTERNAL = 998,
71+
///
72+
/// Unknown Error
73+
///
74+
ERR_UNKNOWN = 999
75+
}
76+
177
#[derive(Copy, Clone)]
278
pub enum Aftype {
379
F32 = 0,

src/util.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use defines::AfError;
12
use defines::Aftype;
23
use defines::InterpType;
34
use defines::ConvMode;
@@ -6,6 +7,13 @@ use defines::MatProp;
67
use defines::MatchType;
78
use std::mem;
89

10+
impl From<i32> for AfError {
11+
fn from(t: i32) -> AfError {
12+
assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);
13+
unsafe { mem::transmute(t) }
14+
}
15+
}
16+
917
impl From<u8> for Aftype {
1018
fn from(t: u8) -> Aftype {
1119
assert!(Aftype::F32 as u8 <= t && t <= Aftype::U64 as u8);

0 commit comments

Comments
 (0)