Skip to content

Commit 97b6018

Browse files
committed
Use enum-error-derive
1 parent f37e155 commit 97b6018

File tree

4 files changed

+17
-59
lines changed

4 files changed

+17
-59
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ openblas = ["blas/openblas", "lapack/openblas"]
1515
netlib = ["blas/netlib", "lapack/netlib"]
1616

1717
[dependencies]
18+
enum-error-derive = "0.1"
1819
num-traits = "0.1"
1920
num-complex = "0.1"
2021
ndarray = { version = "0.9", default-features = false, features = ["blas"] }

src/error.rs

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ use std::error;
44
use std::fmt;
55
use ndarray::{Ixs, ShapeError};
66

7+
#[derive(Debug, EnumError)]
8+
pub enum LinalgError {
9+
NotSquare(NotSquareError),
10+
Lapack(LapackError),
11+
Stride(StrideError),
12+
Shape(ShapeError),
13+
}
14+
715
#[derive(Debug)]
816
pub struct LapackError {
917
pub return_code: i32,
@@ -62,56 +70,3 @@ impl error::Error for StrideError {
6270
"invalid stride"
6371
}
6472
}
65-
66-
#[derive(Debug)]
67-
pub enum LinalgError {
68-
NotSquare(NotSquareError),
69-
Lapack(LapackError),
70-
Stride(StrideError),
71-
Shape(ShapeError),
72-
}
73-
74-
impl fmt::Display for LinalgError {
75-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76-
match *self {
77-
LinalgError::NotSquare(ref err) => err.fmt(f),
78-
LinalgError::Lapack(ref err) => err.fmt(f),
79-
LinalgError::Stride(ref err) => err.fmt(f),
80-
LinalgError::Shape(ref err) => err.fmt(f),
81-
}
82-
}
83-
}
84-
85-
impl error::Error for LinalgError {
86-
fn description(&self) -> &str {
87-
match *self {
88-
LinalgError::NotSquare(ref err) => err.description(),
89-
LinalgError::Lapack(ref err) => err.description(),
90-
LinalgError::Stride(ref err) => err.description(),
91-
LinalgError::Shape(ref err) => err.description(),
92-
}
93-
}
94-
}
95-
96-
impl From<NotSquareError> for LinalgError {
97-
fn from(err: NotSquareError) -> LinalgError {
98-
LinalgError::NotSquare(err)
99-
}
100-
}
101-
102-
impl From<LapackError> for LinalgError {
103-
fn from(err: LapackError) -> LinalgError {
104-
LinalgError::Lapack(err)
105-
}
106-
}
107-
108-
impl From<StrideError> for LinalgError {
109-
fn from(err: StrideError) -> LinalgError {
110-
LinalgError::Stride(err)
111-
}
112-
}
113-
impl From<ShapeError> for LinalgError {
114-
fn from(err: ShapeError) -> LinalgError {
115-
LinalgError::Shape(err)
116-
}
117-
}

src/impls/opnorm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
use lapack::c::*;
44

55
pub trait ImplOpNorm: Sized {
6-
fn opnorm_1(m: usize, n: usize, a: Vec<Self>) -> Self;
7-
fn opnorm_i(m: usize, n: usize, a: Vec<Self>) -> Self;
8-
fn opnorm_f(m: usize, n: usize, a: Vec<Self>) -> Self;
6+
fn opnorm_1(m: usize, n: usize, a: &[Self]) -> Self;
7+
fn opnorm_i(m: usize, n: usize, a: &[Self]) -> Self;
8+
fn opnorm_f(m: usize, n: usize, a: &[Self]) -> Self;
99
}
1010

1111
macro_rules! impl_opnorm {
1212
($scalar:ty, $lange:path) => {
1313
impl ImplOpNorm for $scalar {
14-
fn opnorm_1(m: usize, n: usize, mut a: Vec<Self>) -> Self {
14+
fn opnorm_1(m: usize, n: usize, mut a: &[Self]) -> Self {
1515
$lange(Layout::ColumnMajor, b'o', m as i32, n as i32, &mut a, m as i32)
1616
}
17-
fn opnorm_i(m: usize, n: usize, mut a: Vec<Self>) -> Self {
17+
fn opnorm_i(m: usize, n: usize, mut a: &[Self]) -> Self {
1818
$lange(Layout::ColumnMajor, b'i', m as i32, n as i32, &mut a, m as i32)
1919
}
20-
fn opnorm_f(m: usize, n: usize, mut a: Vec<Self>) -> Self {
20+
fn opnorm_f(m: usize, n: usize, mut a: &[Self]) -> Self {
2121
$lange(Layout::ColumnMajor, b'f', m as i32, n as i32, &mut a, m as i32)
2222
}
2323
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern crate num_traits;
3737
extern crate num_complex;
3838
#[macro_use(s)]
3939
extern crate ndarray;
40+
#[macro_use]
41+
extern crate enum_error_derive;
4042

4143
pub mod impls;
4244
pub mod error;

0 commit comments

Comments
 (0)