Skip to content

Commit d15eaf6

Browse files
committed
Added Error & Display traits for AfError type
1 parent 507c226 commit d15eaf6

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

examples/helloworld.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ fn main() {
1010
let dims = Dim4::new(&[5, 3, 1, 1]);
1111

1212
println!("Create a 5-by-3 matrix of random floats on the GPU");
13-
let a = randu(dims, Aftype::F32).unwrap();
13+
let a = match randu(dims, Aftype::F32) {
14+
Ok(value) => value,
15+
Err(error) => panic!("{}", error),
16+
};
1417
print(&a);
1518

1619
println!("Element-wise arithmetic");
@@ -38,8 +41,7 @@ fn main() {
3841
// af_print(B);
3942

4043
println!("Fourier transform the result");
41-
let c = &fft(&b, 1.0, 0).unwrap();
42-
print(&c);
44+
fft(&b, 1.0, 0).map(|x| print(&x));
4345

4446
// printf("Grab last row\n");
4547
// array c = C.row(end);
@@ -57,9 +59,11 @@ fn main() {
5759

5860
// // Sort A
5961
println!("Sort A and print sorted array and corresponding indices");
60-
let (vals, inds) = sort_index(&a, 0, true).unwrap();
61-
print(&vals);
62-
print(&inds);
62+
sort_index(&a, 0, true)
63+
.map(| x | {
64+
print(&x.0);
65+
print(&x.1);
66+
});
6367

6468
println!("u8 constant array");
6569
let u8_cnst = &constant(1 as u8, dims).unwrap();

examples/histogram.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ fn main() {
1515

1616
let img_wnd = match Window::new(480, 640, String::from("Input Image")) {
1717
Ok(v) => { v.set_position(100, 100).unwrap(); v },
18-
Err(e)=> panic!("Window creation failed, exiting: {:?}", e),
18+
Err(e)=> panic!("Window creation failed, exiting: {}", e),
1919
};
2020

2121
let hst_wnd = match Window::new(512, 512, String::from("Input Image Histogram")) {
2222
Ok(v) => { v.set_position(600, 100).unwrap(); v },
23-
Err(e)=> panic!("Window creation failed, exiting: {:?}", e),
23+
Err(e)=> panic!("Window creation failed, exiting: {}", e),
2424
};
2525

2626
let (man, hst) = match load_image(format!("{}/man.jpg", assets_dir.display()), false) {
2727
Ok(v) => match histogram(&v, 256, 0.0, 255.0) {
2828
Ok(h) => (v, h),
29-
Err(e)=> panic!("Histogram computation failed, exiting: {:?}", e),
29+
Err(e)=> panic!("Histogram computation failed, exiting: {}", e),
3030
},
31-
Err(e)=> panic!("Image loading failed, exiting: {:?}", e),
31+
Err(e)=> panic!("Image loading failed, exiting: {}", e),
3232
};
3333

3434
let disp_img = man.dims()

src/defines.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::error::Error;
2+
use std::fmt::{Display, Formatter};
3+
use std::fmt::Error as FmtError;
4+
15
#[repr(C)]
26
#[derive(Clone, Copy, Debug)]
37
pub enum AfError {
@@ -74,6 +78,35 @@ pub enum AfError {
7478
ERR_UNKNOWN = 999
7579
}
7680

81+
impl Display for AfError {
82+
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
83+
write!(f, "{}", self.description())
84+
}
85+
}
86+
87+
impl Error for AfError {
88+
fn description(&self) -> &str {
89+
match *self {
90+
AfError::SUCCESS => "Function returned successfully",
91+
AfError::ERR_NO_MEM => "The system or device ran out of memory",
92+
AfError::ERR_DRIVER => "Device driver error",
93+
AfError::ERR_RUNTIME => "Error in runtime environment",
94+
AfError::ERR_INVALID_ARRAY => "Input is not a valid Array Object",
95+
AfError::ERR_ARG => "One of the function arguments is incorrect",
96+
AfError::ERR_SIZE => "The size is incorrect",
97+
AfError::ERR_TYPE => "The type is not supported by this function",
98+
AfError::ERR_DIFF_TYPE => "The type of input arrays are not compatible",
99+
AfError::ERR_BATCH => "Function does not support GFOR / batch mode",
100+
AfError::ERR_NOT_SUPPORTED => "The option is not supported",
101+
AfError::ERR_NOT_CONFIGURED => "This build of ArrayFire does not support this feature",
102+
AfError::ERR_NO_DBL => "This device does not support double",
103+
AfError::ERR_NO_GFX => "This build of ArrayFire was not built with graphics or this device does not support graphics",
104+
AfError::ERR_INTERNAL => "There was an internal error in either ArrayFire or upstream project",
105+
AfError::ERR_UNKNOWN => "Unkown Error",
106+
}
107+
}
108+
}
109+
77110
#[derive(Copy, Clone)]
78111
pub enum Aftype {
79112
F32 = 0,

0 commit comments

Comments
 (0)