From 3c5902881d1b9216b3337334d0451f7930c1230f Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Thu, 6 Sep 2018 04:20:24 +0900 Subject: [PATCH] Replace to failure --- Cargo.toml | 3 +- src/error.rs | 98 ++++++++-------------------------------- src/lapack_traits/mod.rs | 6 +-- src/layout.rs | 20 ++++++-- src/lib.rs | 6 +-- src/solve.rs | 4 +- src/solveh.rs | 4 +- 7 files changed, 44 insertions(+), 97 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b99d1871..89b2e15e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,11 +20,10 @@ openblas = ["lapack-src/openblas", "blas-src/openblas", "openblas-src"] serde-1 = ["ndarray/serde-1", "num-complex/serde"] [dependencies] -derive-new = "0.5" lapacke = "0.2" num-traits = "0.2" -procedurals = "0.3" rand = "0.5" +failure = "0.1" [dependencies.num-complex] version = "0.2" diff --git a/src/error.rs b/src/error.rs index 8dbe0f21..e64ed4c8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,95 +1,35 @@ //! 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(Debug, EnumError)] +#[derive(Fail, Debug)] pub enum LinalgError { - NotSquare(NotSquareError), - Lapack(LapackError), - Stride(StrideError), - MemoryCont(MemoryContError), - Shape(ShapeError), -} - -/// Error from LAPACK -#[derive(Debug, new)] -pub struct LapackError { - pub return_code: i32, -} - -impl fmt::Display for LapackError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "LAPACK: return_code = {}", self.return_code) - } -} - -impl error::Error for LapackError { - fn description(&self) -> &str { - "LAPACK subroutine returns non-zero code" - } -} + /// Matrix is not square + #[fail(display = "Not square: rows({}) != cols({})", rows, cols)] + NotSquare { rows: i32, cols: i32 }, -impl From for LapackError { - fn from(code: i32) -> LapackError { - LapackError { return_code: code } - } -} + /// LAPACK subroutine returns non-zero code + #[fail(display = "LAPACK: return_code = {}", return_code)] + LapackFailure { return_code: i32 }, -/// Error that matrix is not square -#[derive(Debug, new)] -pub struct NotSquareError { - pub rows: i32, - pub cols: i32, -} + /// Strides of the array is not supported + #[fail(display = "invalid stride: s0={}, s1={}", s0, s1)] + InvalidStride { s0: Ixs, s1: Ixs }, -impl fmt::Display for NotSquareError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Not square: rows({}) != cols({})", self.rows, self.cols) - } -} + /// Memory is not aligned continously + #[fail(display = "Memory is not contiguous")] + MemoryNotCont {}, -impl error::Error for NotSquareError { - fn description(&self) -> &str { - "Matrix is not square" - } -} - -/// Error that strides of the array is not supported -#[derive(Debug, new)] -pub struct StrideError { - pub s0: Ixs, - pub s1: Ixs, -} - -impl fmt::Display for StrideError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "invalid stride: s0={}, s1={}", self.s0, self.s1) - } -} - -impl error::Error for StrideError { - fn description(&self) -> &str { - "invalid stride" - } -} - -/// Error that the memory is not aligned continously -#[derive(Debug, new)] -pub struct MemoryContError {} - -impl fmt::Display for MemoryContError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Memory is not contiguous") - } + /// Strides of the array is not supported + #[fail(display = "Shape Error: {}", error)] + ShapeFailure { error: ShapeError }, } -impl error::Error for MemoryContError { - fn description(&self) -> &str { - "Memory is not contiguous" +impl From for LinalgError { + fn from(error: ShapeError) -> LinalgError { + LinalgError::ShapeFailure { error } } } diff --git a/src/lapack_traits/mod.rs b/src/lapack_traits/mod.rs index 6c2f57b7..e2420333 100644 --- a/src/lapack_traits/mod.rs +++ b/src/lapack_traits/mod.rs @@ -30,11 +30,11 @@ impl LapackScalar for f64 {} impl LapackScalar for c32 {} impl LapackScalar for c64 {} -pub fn into_result(info: i32, val: T) -> Result { - if info == 0 { +pub fn into_result(return_code: i32, val: T) -> Result { + if return_code == 0 { Ok(val) } else { - Err(LapackError::new(info).into()) + Err(LinalgError::LapackFailure { return_code }) } } diff --git a/src/layout.rs b/src/layout.rs index ac6203c7..600796d8 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -98,7 +98,10 @@ where if shape[1] == strides[0] as usize { return Ok(MatrixLayout::C((self.rows() as i32, self.cols() as i32))); } - Err(StrideError::new(strides[0], strides[1]).into()) + Err(LinalgError::InvalidStride { + s0: strides[0], + s1: strides[1], + }) } fn square_layout(&self) -> Result { @@ -107,7 +110,7 @@ where if n == m { Ok(l) } else { - Err(NotSquareError::new(n, m).into()) + Err(LinalgError::NotSquare { rows: n, cols: m }) } } @@ -115,12 +118,17 @@ where if self.is_square() { Ok(()) } else { - Err(NotSquareError::new(self.rows() as i32, self.cols() as i32).into()) + Err(LinalgError::NotSquare { + rows: self.rows() as i32, + cols: self.cols() as i32, + }) } } fn as_allocated(&self) -> Result<&[A]> { - Ok(self.as_slice_memory_order().ok_or_else(MemoryContError::new)?) + Ok(self + .as_slice_memory_order() + .ok_or_else(|| LinalgError::MemoryNotCont {})?) } } @@ -129,6 +137,8 @@ where S: DataMut, { fn as_allocated_mut(&mut self) -> Result<&mut [A]> { - Ok(self.as_slice_memory_order_mut().ok_or_else(MemoryContError::new)?) + Ok(self + .as_slice_memory_order_mut() + .ok_or_else(|| LinalgError::MemoryNotCont {})?) } } diff --git a/src/lib.rs b/src/lib.rs index 6b03ef80..23109ec8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,12 +21,10 @@ 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; -#[macro_use] -extern crate procedurals; -#[macro_use] -extern crate derive_new; pub mod assert; pub mod cholesky; diff --git a/src/solve.rs b/src/solve.rs index b1ac5833..e654974c 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::Lapack(LapackError { return_code })) if return_code > 0 => { + Err(LinalgError::LapackFailure { 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::Lapack(LapackError { return_code })) if return_code > 0 => { + Err(LinalgError::LapackFailure { 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 a4dc3fe8..abeeb05d 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::Lapack(LapackError { return_code })) if return_code > 0 => { + Err(LinalgError::LapackFailure { 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::Lapack(LapackError { return_code })) if return_code > 0 => { + Err(LinalgError::LapackFailure { return_code }) if return_code > 0 => { // Determinant is zero. Ok((A::Real::zero(), A::Real::neg_infinity())) }