diff --git a/Cargo.toml b/Cargo.toml index 89b2e15e..20bae921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ serde-1 = ["ndarray/serde-1", "num-complex/serde"] lapacke = "0.2" num-traits = "0.2" rand = "0.5" -failure = "0.1" [dependencies.num-complex] version = "0.2" diff --git a/src/error.rs b/src/error.rs index e64ed4c8..6fea8673 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,35 +1,49 @@ //! Define Errors use ndarray::{Ixs, ShapeError}; +use std::error; +use std::fmt; pub type Result = ::std::result::Result; /// Master Error type of this crate -#[derive(Fail, Debug)] +#[derive(Debug)] pub enum LinalgError { /// Matrix is not square - #[fail(display = "Not square: rows({}) != cols({})", rows, cols)] NotSquare { rows: i32, cols: i32 }, - /// LAPACK subroutine returns non-zero code - #[fail(display = "LAPACK: return_code = {}", return_code)] - LapackFailure { return_code: i32 }, - + Lapack { return_code: i32 }, /// Strides of the array is not supported - #[fail(display = "invalid stride: s0={}, s1={}", s0, s1)] InvalidStride { s0: Ixs, s1: Ixs }, - /// Memory is not aligned continously - #[fail(display = "Memory is not contiguous")] - MemoryNotCont {}, - + MemoryNotCont, /// Strides of the array is not supported - #[fail(display = "Shape Error: {}", error)] - ShapeFailure { error: ShapeError }, + Shape(ShapeError), +} + +impl fmt::Display for LinalgError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + LinalgError::NotSquare { rows, cols } => write!(f, "Not square: rows({}) != cols({})", rows, cols), + LinalgError::Lapack { return_code } => write!(f, "LAPACK: return_code = {}", return_code), + LinalgError::InvalidStride { s0, s1 } => write!(f, "invalid stride: s0={}, s1={}", s0, s1), + LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"), + LinalgError::Shape(err) => write!(f, "Shape Error: {}", err), + } + } +} + +impl error::Error for LinalgError { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self { + LinalgError::Shape(err) => Some(err), + _ => None, + } + } } impl From for LinalgError { fn from(error: ShapeError) -> LinalgError { - LinalgError::ShapeFailure { error } + LinalgError::Shape(error) } } diff --git a/src/lapack_traits/mod.rs b/src/lapack_traits/mod.rs index e2420333..693cfc3d 100644 --- a/src/lapack_traits/mod.rs +++ b/src/lapack_traits/mod.rs @@ -34,7 +34,7 @@ pub fn into_result(return_code: i32, val: T) -> Result { if return_code == 0 { Ok(val) } else { - Err(LinalgError::LapackFailure { return_code }) + Err(LinalgError::Lapack { return_code }) } } diff --git a/src/layout.rs b/src/layout.rs index 600796d8..61d7d5d8 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -128,7 +128,7 @@ where fn as_allocated(&self) -> Result<&[A]> { Ok(self .as_slice_memory_order() - .ok_or_else(|| LinalgError::MemoryNotCont {})?) + .ok_or_else(|| LinalgError::MemoryNotCont)?) } } @@ -139,6 +139,6 @@ where fn as_allocated_mut(&mut self) -> Result<&mut [A]> { Ok(self .as_slice_memory_order_mut() - .ok_or_else(|| LinalgError::MemoryNotCont {})?) + .ok_or_else(|| LinalgError::MemoryNotCont)?) } } diff --git a/src/lib.rs b/src/lib.rs index 23109ec8..96d70cc7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,8 +21,6 @@ extern crate lapacke; extern crate num_complex; extern crate num_traits; extern crate rand; -#[macro_use] -extern crate failure; #[macro_use(s)] extern crate ndarray; diff --git a/src/solve.rs b/src/solve.rs index e654974c..566190fc 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -427,7 +427,7 @@ where self.ensure_square()?; match self.factorize() { Ok(fac) => fac.sln_det(), - Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => { + Err(LinalgError::Lapack { return_code }) if return_code > 0 => { // The determinant is zero. Ok((A::zero(), A::Real::neg_infinity())) } @@ -445,7 +445,7 @@ where self.ensure_square()?; match self.factorize_into() { Ok(fac) => fac.sln_det_into(), - Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => { + Err(LinalgError::Lapack { return_code }) if return_code > 0 => { // The determinant is zero. Ok((A::zero(), A::Real::neg_infinity())) } diff --git a/src/solveh.rs b/src/solveh.rs index abeeb05d..b4f83c6a 100644 --- a/src/solveh.rs +++ b/src/solveh.rs @@ -421,7 +421,7 @@ where fn sln_deth(&self) -> Result<(A::Real, A::Real)> { match self.factorizeh() { Ok(fac) => Ok(fac.sln_deth()), - Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => { + Err(LinalgError::Lapack { return_code }) if return_code > 0 => { // Determinant is zero. Ok((A::Real::zero(), A::Real::neg_infinity())) } @@ -445,7 +445,7 @@ where fn sln_deth_into(self) -> Result<(A::Real, A::Real)> { match self.factorizeh_into() { Ok(fac) => Ok(fac.sln_deth_into()), - Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => { + Err(LinalgError::Lapack { return_code }) if return_code > 0 => { // Determinant is zero. Ok((A::Real::zero(), A::Real::neg_infinity())) }