1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
extern crate typeable;
extern crate traitobject;
use std::fmt::Debug;
use std::any::TypeId;
use std::error::Error as StdError;
use std::mem;
use typeable::Typeable;
pub trait Error: Debug + Send + Typeable + StdError { }
impl<S: StdError + Debug + Send + Typeable> Error for S { }
impl Error {
pub fn is<E: Error>(&self) -> bool { self.get_type() == TypeId::of::<E>() }
pub fn downcast<E: Error>(&self) -> Option<&E> {
if self.is::<E>() {
unsafe { Some(mem::transmute(traitobject::data(self))) }
} else {
None
}
}
}
impl Error + Send {
pub fn is<E: Error + Send>(&self) -> bool { self.get_type() == TypeId::of::<E>() }
pub fn downcast<E: Error + Send>(&self) -> Option<&E> {
if self.is::<E>() {
unsafe { Some(mem::transmute(traitobject::data(self))) }
} else {
None
}
}
}
impl<E: Error> From<E> for Box<Error> {
fn from(e: E) -> Box<Error> { Box::new(e) }
}
#[macro_export]
macro_rules! match_error {
($m:expr, $i1:pat => $t1:ty: $e1:expr) => {{
let tmp = $m;
match tmp.downcast::<$t1>() {
Some($i1) => Some($e1),
None => None,
}
}};
($m:expr, $i1:pat => $t1:ty: $e1:expr, $($i:pat => $t:ty: $e:expr),+) => {{
let tmp = $m;
match tmp.downcast::<$t1>() {
Some($i1) => Some($e1),
None => match_error!(tmp, $($i: $t => $e),*),
}
}};
}
#[cfg(test)]
mod test {
use super::Error;
use std::error::Error as StdError;
use std::fmt::Error as FmtError;
use std::fmt::Display;
use std::fmt::Formatter;
#[derive(Debug, PartialEq)]
pub struct ParseError {
location: usize,
}
impl StdError for ParseError {
fn description(&self) -> &str { "Parse Error" }
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
self.description().fmt(f)
}
}
#[test] fn test_generic() {
fn produce_parse_error() -> Box<Error> {
Box::new(ParseError { location: 7 })
}
fn generic_handler(raw: Box<Error>) {
(match_error! { raw,
parse => ParseError: {
assert_eq!(*parse, ParseError { location: 7 })
}
}).unwrap()
}
generic_handler(produce_parse_error())
}
}