Skip to content

Commit 170fcc8

Browse files
committed
Make the StackError an actual Error
This allows it to play nice with error-handling libraries like error-chain or failure.
1 parent ab68ff3 commit 170fcc8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/stack.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// http://opensource.org/licenses/MIT>, at your option. This file may not be
66
// copied, modified, or distributed except according to those terms.
77

8+
use std::error::Error;
9+
use std::fmt::{Display, Formatter, Result as FmtResult};
810
use std::io;
911
use std::ops::Deref;
1012
use std::os::raw::c_void;
@@ -21,6 +23,32 @@ pub enum StackError {
2123
IoError(io::Error),
2224
}
2325

26+
impl Display for StackError {
27+
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
28+
match *self {
29+
StackError::ExceedsMaximumSize(size) => {
30+
write!(fmt, "Requested more than max size of {} bytes for a stack", size)
31+
},
32+
StackError::IoError(ref e) => e.fmt(fmt),
33+
}
34+
}
35+
}
36+
37+
impl Error for StackError {
38+
fn description(&self) -> &str {
39+
match *self {
40+
StackError::ExceedsMaximumSize(_) => "exceeds maximum stack size",
41+
StackError::IoError(ref e) => e.description(),
42+
}
43+
}
44+
fn cause(&self) -> Option<&Error> {
45+
match *self {
46+
StackError::ExceedsMaximumSize(_) => None,
47+
StackError::IoError(ref e) => Some(e),
48+
}
49+
}
50+
}
51+
2452
/// Represents any kind of stack memory.
2553
///
2654
/// `FixedSizeStack` as well as `ProtectedFixedSizeStack`

0 commit comments

Comments
 (0)