Skip to content

Commit 4614d0e

Browse files
authored
Merge pull request #118 from termoshtt/failure
Replace to failure
2 parents 7c68d2e + 3c59028 commit 4614d0e

File tree

7 files changed

+44
-97
lines changed

7 files changed

+44
-97
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ openblas = ["lapack-src/openblas", "blas-src/openblas", "openblas-src"]
2020
serde-1 = ["ndarray/serde-1", "num-complex/serde"]
2121

2222
[dependencies]
23-
derive-new = "0.5"
2423
lapacke = "0.2"
2524
num-traits = "0.2"
26-
procedurals = "0.3"
2725
rand = "0.5"
26+
failure = "0.1"
2827

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

src/error.rs

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

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

97
/// Master Error type of this crate
10-
#[derive(Debug, EnumError)]
8+
#[derive(Fail, Debug)]
119
pub enum LinalgError {
12-
NotSquare(NotSquareError),
13-
Lapack(LapackError),
14-
Stride(StrideError),
15-
MemoryCont(MemoryContError),
16-
Shape(ShapeError),
17-
}
18-
19-
/// Error from LAPACK
20-
#[derive(Debug, new)]
21-
pub struct LapackError {
22-
pub return_code: i32,
23-
}
24-
25-
impl fmt::Display for LapackError {
26-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
write!(f, "LAPACK: return_code = {}", self.return_code)
28-
}
29-
}
30-
31-
impl error::Error for LapackError {
32-
fn description(&self) -> &str {
33-
"LAPACK subroutine returns non-zero code"
34-
}
35-
}
10+
/// Matrix is not square
11+
#[fail(display = "Not square: rows({}) != cols({})", rows, cols)]
12+
NotSquare { rows: i32, cols: i32 },
3613

37-
impl From<i32> for LapackError {
38-
fn from(code: i32) -> LapackError {
39-
LapackError { return_code: code }
40-
}
41-
}
14+
/// LAPACK subroutine returns non-zero code
15+
#[fail(display = "LAPACK: return_code = {}", return_code)]
16+
LapackFailure { return_code: i32 },
4217

43-
/// Error that matrix is not square
44-
#[derive(Debug, new)]
45-
pub struct NotSquareError {
46-
pub rows: i32,
47-
pub cols: i32,
48-
}
18+
/// Strides of the array is not supported
19+
#[fail(display = "invalid stride: s0={}, s1={}", s0, s1)]
20+
InvalidStride { s0: Ixs, s1: Ixs },
4921

50-
impl fmt::Display for NotSquareError {
51-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52-
write!(f, "Not square: rows({}) != cols({})", self.rows, self.cols)
53-
}
54-
}
22+
/// Memory is not aligned continously
23+
#[fail(display = "Memory is not contiguous")]
24+
MemoryNotCont {},
5525

56-
impl error::Error for NotSquareError {
57-
fn description(&self) -> &str {
58-
"Matrix is not square"
59-
}
60-
}
61-
62-
/// Error that strides of the array is not supported
63-
#[derive(Debug, new)]
64-
pub struct StrideError {
65-
pub s0: Ixs,
66-
pub s1: Ixs,
67-
}
68-
69-
impl fmt::Display for StrideError {
70-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71-
write!(f, "invalid stride: s0={}, s1={}", self.s0, self.s1)
72-
}
73-
}
74-
75-
impl error::Error for StrideError {
76-
fn description(&self) -> &str {
77-
"invalid stride"
78-
}
79-
}
80-
81-
/// Error that the memory is not aligned continously
82-
#[derive(Debug, new)]
83-
pub struct MemoryContError {}
84-
85-
impl fmt::Display for MemoryContError {
86-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87-
write!(f, "Memory is not contiguous")
88-
}
26+
/// Strides of the array is not supported
27+
#[fail(display = "Shape Error: {}", error)]
28+
ShapeFailure { error: ShapeError },
8929
}
9030

91-
impl error::Error for MemoryContError {
92-
fn description(&self) -> &str {
93-
"Memory is not contiguous"
31+
impl From<ShapeError> for LinalgError {
32+
fn from(error: ShapeError) -> LinalgError {
33+
LinalgError::ShapeFailure { error }
9434
}
9535
}

src/lapack_traits/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ impl LapackScalar for f64 {}
3030
impl LapackScalar for c32 {}
3131
impl LapackScalar for c64 {}
3232

33-
pub fn into_result<T>(info: i32, val: T) -> Result<T> {
34-
if info == 0 {
33+
pub fn into_result<T>(return_code: i32, val: T) -> Result<T> {
34+
if return_code == 0 {
3535
Ok(val)
3636
} else {
37-
Err(LapackError::new(info).into())
37+
Err(LinalgError::LapackFailure { return_code })
3838
}
3939
}
4040

src/layout.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ where
9898
if shape[1] == strides[0] as usize {
9999
return Ok(MatrixLayout::C((self.rows() as i32, self.cols() as i32)));
100100
}
101-
Err(StrideError::new(strides[0], strides[1]).into())
101+
Err(LinalgError::InvalidStride {
102+
s0: strides[0],
103+
s1: strides[1],
104+
})
102105
}
103106

104107
fn square_layout(&self) -> Result<MatrixLayout> {
@@ -107,20 +110,25 @@ where
107110
if n == m {
108111
Ok(l)
109112
} else {
110-
Err(NotSquareError::new(n, m).into())
113+
Err(LinalgError::NotSquare { rows: n, cols: m })
111114
}
112115
}
113116

114117
fn ensure_square(&self) -> Result<()> {
115118
if self.is_square() {
116119
Ok(())
117120
} else {
118-
Err(NotSquareError::new(self.rows() as i32, self.cols() as i32).into())
121+
Err(LinalgError::NotSquare {
122+
rows: self.rows() as i32,
123+
cols: self.cols() as i32,
124+
})
119125
}
120126
}
121127

122128
fn as_allocated(&self) -> Result<&[A]> {
123-
Ok(self.as_slice_memory_order().ok_or_else(MemoryContError::new)?)
129+
Ok(self
130+
.as_slice_memory_order()
131+
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
124132
}
125133
}
126134

@@ -129,6 +137,8 @@ where
129137
S: DataMut<Elem = A>,
130138
{
131139
fn as_allocated_mut(&mut self) -> Result<&mut [A]> {
132-
Ok(self.as_slice_memory_order_mut().ok_or_else(MemoryContError::new)?)
140+
Ok(self
141+
.as_slice_memory_order_mut()
142+
.ok_or_else(|| LinalgError::MemoryNotCont {})?)
133143
}
134144
}

src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ extern crate lapacke;
2121
extern crate num_complex;
2222
extern crate num_traits;
2323
extern crate rand;
24+
#[macro_use]
25+
extern crate failure;
2426
#[macro_use(s)]
2527
extern crate ndarray;
26-
#[macro_use]
27-
extern crate procedurals;
28-
#[macro_use]
29-
extern crate derive_new;
3028

3129
pub mod assert;
3230
pub mod cholesky;

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::Lapack(LapackError { return_code })) if return_code > 0 => {
430+
Err(LinalgError::LapackFailure { 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::Lapack(LapackError { return_code })) if return_code > 0 => {
448+
Err(LinalgError::LapackFailure { 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::Lapack(LapackError { return_code })) if return_code > 0 => {
424+
Err(LinalgError::LapackFailure { 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::Lapack(LapackError { return_code })) if return_code > 0 => {
448+
Err(LinalgError::LapackFailure { 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)