Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from failure::Fail to std's Error trait #127

Merged
merged 1 commit into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 28 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
//! Define Errors

use ndarray::{Ixs, ShapeError};
use std::error;
use std::fmt;

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

/// 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<ShapeError> for LinalgError {
fn from(error: ShapeError) -> LinalgError {
LinalgError::ShapeFailure { error }
LinalgError::Shape(error)
}
}
2 changes: 1 addition & 1 deletion src/lapack_traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn into_result<T>(return_code: i32, val: T) -> Result<T> {
if return_code == 0 {
Ok(val)
} else {
Err(LinalgError::LapackFailure { return_code })
Err(LinalgError::Lapack { return_code })
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
}
}

Expand All @@ -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)?)
}
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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()))
}
Expand Down
4 changes: 2 additions & 2 deletions src/solveh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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()))
}
Expand Down