genet_abi/
error.rs

1use std::{error, fmt, str};
2use string::SafeString;
3
4/// An error object.
5#[repr(C)]
6#[derive(Clone, PartialEq)]
7pub struct Error {
8    desc: SafeString,
9}
10
11impl Error {
12    /// Creates a new Error.
13    pub fn new(desc: &str) -> Error {
14        Self {
15            desc: SafeString::from(desc),
16        }
17    }
18}
19
20impl fmt::Debug for Error {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        write!(f, "Error: {}", error::Error::description(self))
23    }
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        write!(f, "{}", error::Error::description(self))
29    }
30}
31
32impl error::Error for Error {
33    fn description(&self) -> &str {
34        &self.desc
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use std::error;
42
43    #[test]
44    fn description() {
45        let msg = "out of bounds";
46        let err = Error::new(msg);
47        assert_eq!(error::Error::description(&err), msg);
48    }
49}