]> git.lizzy.rs Git - rust.git/blob - src/error.rs
f16bdc3b6b3c8400bf638992cedf50ba47f70a4a
[rust.git] / src / error.rs
1 use std::error::Error;
2 use std::fmt;
3
4 #[derive(Clone, Debug)]
5 pub enum EvalError {
6     DanglingPointerDeref,
7     InvalidBool,
8     PointerOutOfBounds,
9     ReadPointerAsBytes,
10     ReadBytesAsPointer,
11     InvalidPointerMath,
12 }
13
14 pub type EvalResult<T> = Result<T, EvalError>;
15
16 impl Error for EvalError {
17     fn description(&self) -> &str {
18         match *self {
19             EvalError::DanglingPointerDeref => "dangling pointer was dereferenced",
20             EvalError::InvalidBool => "invalid boolean value read",
21             EvalError::PointerOutOfBounds => "pointer offset outside bounds of allocation",
22             EvalError::ReadPointerAsBytes =>
23                 "a raw memory access tried to access part of a pointer value as raw bytes",
24             EvalError::ReadBytesAsPointer =>
25                 "attempted to interpret some raw bytes as a pointer address",
26             EvalError::InvalidPointerMath =>
27                 "attempted to do math or a comparison on pointers into different allocations",
28         }
29     }
30
31     fn cause(&self) -> Option<&Error> { None }
32 }
33
34 impl fmt::Display for EvalError {
35     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36         write!(f, "{}", self.description())
37     }
38 }