Skip to content

Commit 9114302

Browse files
authored
Merge pull request #127 from jturner314/error
Switch from failure::Fail to std's Error trait
2 parents 4614d0e + 6e63d07 commit 9114302

File tree

7 files changed

+35
-24
lines changed

7 files changed

+35
-24
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ serde-1 = ["ndarray/serde-1", "num-complex/serde"]
2323
lapacke = "0.2"
2424
num-traits = "0.2"
2525
rand = "0.5"
26-
failure = "0.1"
2726

2827
[dependencies.num-complex]
2928
version = "0.2"

src/error.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
11
//! Define Errors
22
33
use ndarray::{Ixs, ShapeError};
4+
use std::error;
5+
use std::fmt;
46

57
pub type Result<T> = ::std::result::Result<T, LinalgError>;
68

79
/// Master Error type of this crate
8-
#[derive(Fail, Debug)]
10+
#[derive(Debug)]
911
pub enum LinalgError {
1012
/// Matrix is not square
11-
#[fail(display = "Not square: rows({}) != cols({})", rows, cols)]
1213
NotSquare { rows: i32, cols: i32 },
13-
1414
/// LAPACK subroutine returns non-zero code
15-
#[fail(display = "LAPACK: return_code = {}", return_code)]
16-
LapackFailure { return_code: i32 },
17-
15+
Lapack { return_code: i32 },
1816
/// Strides of the array is not supported
19-
#[fail(display = "invalid stride: s0={}, s1={}", s0, s1)]
2017
InvalidStride { s0: Ixs, s1: Ixs },
21-
2218
/// Memory is not aligned continously
23-
#[fail(display = "Memory is not contiguous")]
24-
MemoryNotCont {},
25-
19+
MemoryNotCont,
2620
/// Strides of the array is not supported
27-
#[fail(display = "Shape Error: {}", error)]
28-
ShapeFailure { error: ShapeError },
21+
Shape(ShapeError),
22+
}
23+
24+
impl fmt::Display for LinalgError {
25+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26+
match self {
27+
LinalgError::NotSquare { rows, cols } => write!(f, "Not square: rows({}) != cols({})", rows, cols),
28+
LinalgError::Lapack { return_code } => write!(f, "LAPACK: return_code = {}", return_code),
29+
LinalgError::InvalidStride { s0, s1 } => write!(f, "invalid stride: s0={}, s1={}", s0, s1),
30+
LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"),
31+
LinalgError::Shape(err) => write!(f, "Shape Error: {}", err),
32+
}
33+
}
34+
}
35+
36+
impl error::Error for LinalgError {
37+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
38+
match self {
39+
LinalgError::Shape(err) => Some(err),
40+
_ => None,
41+
}
42+
}
2943
}
3044

3145
impl From<ShapeError> for LinalgError {
3246
fn from(error: ShapeError) -> LinalgError {
33-
LinalgError::ShapeFailure { error }
47+
LinalgError::Shape(error)
3448
}
3549
}

src/lapack_traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn into_result<T>(return_code: i32, val: T) -> Result<T> {
3434
if return_code == 0 {
3535
Ok(val)
3636
} else {
37-
Err(LinalgError::LapackFailure { return_code })
37+
Err(LinalgError::Lapack { return_code })
3838
}
3939
}
4040

src/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ where
128128
fn as_allocated(&self) -> Result<&[A]> {
129129
Ok(self
130130
.as_slice_memory_order()
131-
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
131+
.ok_or_else(|| LinalgError::MemoryNotCont)?)
132132
}
133133
}
134134

@@ -139,6 +139,6 @@ where
139139
fn as_allocated_mut(&mut self) -> Result<&mut [A]> {
140140
Ok(self
141141
.as_slice_memory_order_mut()
142-
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
142+
.ok_or_else(|| LinalgError::MemoryNotCont)?)
143143
}
144144
}

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ extern crate lapacke;
2121
extern crate num_complex;
2222
extern crate num_traits;
2323
extern crate rand;
24-
#[macro_use]
25-
extern crate failure;
2624
#[macro_use(s)]
2725
extern crate ndarray;
2826

src/solve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ where
427427
self.ensure_square()?;
428428
match self.factorize() {
429429
Ok(fac) => fac.sln_det(),
430-
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
430+
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
431431
// The determinant is zero.
432432
Ok((A::zero(), A::Real::neg_infinity()))
433433
}
@@ -445,7 +445,7 @@ where
445445
self.ensure_square()?;
446446
match self.factorize_into() {
447447
Ok(fac) => fac.sln_det_into(),
448-
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
448+
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
449449
// The determinant is zero.
450450
Ok((A::zero(), A::Real::neg_infinity()))
451451
}

src/solveh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ where
421421
fn sln_deth(&self) -> Result<(A::Real, A::Real)> {
422422
match self.factorizeh() {
423423
Ok(fac) => Ok(fac.sln_deth()),
424-
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
424+
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
425425
// Determinant is zero.
426426
Ok((A::Real::zero(), A::Real::neg_infinity()))
427427
}
@@ -445,7 +445,7 @@ where
445445
fn sln_deth_into(self) -> Result<(A::Real, A::Real)> {
446446
match self.factorizeh_into() {
447447
Ok(fac) => Ok(fac.sln_deth_into()),
448-
Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => {
448+
Err(LinalgError::Lapack { return_code }) if return_code > 0 => {
449449
// Determinant is zero.
450450
Ok((A::Real::zero(), A::Real::neg_infinity()))
451451
}

0 commit comments

Comments
 (0)