]> git.lizzy.rs Git - rust.git/blob - src/error.rs
65dd187fa0b600fdf5ed5fbda8d7051b52ae61f3
[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     InvalidDiscriminant,
9     PointerOutOfBounds,
10     ReadPointerAsBytes,
11     ReadBytesAsPointer,
12     InvalidPointerMath,
13     ReadUndefBytes,
14 }
15
16 pub type EvalResult<T> = Result<T, EvalError>;
17
18 impl Error for EvalError {
19     fn description(&self) -> &str {
20         match *self {
21             EvalError::DanglingPointerDeref =>
22                 "dangling pointer was dereferenced",
23             EvalError::InvalidBool =>
24                 "invalid boolean value read",
25             EvalError::InvalidDiscriminant =>
26                 "invalid enum discriminant value read",
27             EvalError::PointerOutOfBounds =>
28                 "pointer offset outside bounds of allocation",
29             EvalError::ReadPointerAsBytes =>
30                 "a raw memory access tried to access part of a pointer value as raw bytes",
31             EvalError::ReadBytesAsPointer =>
32                 "attempted to interpret some raw bytes as a pointer address",
33             EvalError::InvalidPointerMath =>
34                 "attempted to do math or a comparison on pointers into different allocations",
35             EvalError::ReadUndefBytes =>
36                 "attempted to read undefined bytes",
37         }
38     }
39
40     fn cause(&self) -> Option<&Error> { None }
41 }
42
43 impl fmt::Display for EvalError {
44     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45         write!(f, "{}", self.description())
46     }
47 }