Skip to content

Commit 2c6a121

Browse files
authored
Merge pull request #208 from rust-ndarray/thiserror
Use thiserror crate for implement Error type
2 parents 1642d42 + 2c6ced5 commit 2c6a121

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ num-traits = "0.2.11"
2828
cauchy = "0.2.2"
2929
num-complex = "0.2.4"
3030
rand = "0.5"
31+
thiserror = "1.0.20"
3132

3233
[dependencies.ndarray]
3334
version = "0.13.0"

src/error.rs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,38 @@
11
//! Define Errors
22
33
use ndarray::{Ixs, ShapeError};
4-
use std::error;
5-
use std::fmt;
4+
use thiserror::Error;
65

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

98
/// Master Error type of this crate
10-
#[derive(Debug)]
9+
#[derive(Debug, Error)]
1110
pub enum LinalgError {
1211
/// Matrix is not square
12+
#[error("Not square: rows({}) != cols({})", rows, cols)]
1313
NotSquare { rows: i32, cols: i32 },
14+
1415
/// LAPACK subroutine returns non-zero code
16+
#[error("LAPACK: return_code = {}", return_code)]
1517
Lapack { return_code: i32 },
18+
1619
/// Strides of the array is not supported
20+
#[error("invalid stride: s0={}, s1={}", s0, s1)]
1721
InvalidStride { s0: Ixs, s1: Ixs },
22+
1823
/// Memory is not aligned continously
24+
#[error("Memroy is not continously")]
1925
MemoryNotCont,
26+
2027
/// Obj cannot be made from a (rows, cols) matrix
28+
#[error("{} cannot be made from a ({}, {}) matrix", obj, rows, cols)]
2129
NotStandardShape {
2230
obj: &'static str,
2331
rows: i32,
2432
cols: i32,
2533
},
26-
/// Strides of the array is not supported
27-
Shape(ShapeError),
28-
}
2934

30-
impl fmt::Display for LinalgError {
31-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32-
match self {
33-
LinalgError::NotSquare { rows, cols } => {
34-
write!(f, "Not square: rows({}) != cols({})", rows, cols)
35-
}
36-
LinalgError::Lapack { return_code } => {
37-
write!(f, "LAPACK: return_code = {}", return_code)
38-
}
39-
LinalgError::InvalidStride { s0, s1 } => {
40-
write!(f, "invalid stride: s0={}, s1={}", s0, s1)
41-
}
42-
LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"),
43-
LinalgError::NotStandardShape { obj, rows, cols } => write!(
44-
f,
45-
"{} cannot be made from a ({}, {}) matrix",
46-
obj, rows, cols
47-
),
48-
LinalgError::Shape(err) => write!(f, "Shape Error: {}", err),
49-
}
50-
}
51-
}
52-
53-
impl error::Error for LinalgError {
54-
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
55-
match self {
56-
LinalgError::Shape(err) => Some(err),
57-
_ => None,
58-
}
59-
}
60-
}
61-
62-
impl From<ShapeError> for LinalgError {
63-
fn from(error: ShapeError) -> LinalgError {
64-
LinalgError::Shape(error)
65-
}
35+
/// Strides of the array is not supported
36+
#[error(transparent)]
37+
Shape(#[from] ShapeError),
6638
}

0 commit comments

Comments
 (0)