| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 5h ago | 1 | // A `Display` impl becomes a proc returning the string the formatter is |
| 2 | // written with, since the observable result of `{}` is exactly those bytes. |
| 3 | use core::fmt; |
| 4 | |
| 5 | #[derive(Debug, PartialEq)] |
| 6 | pub enum Error { |
| 7 | InvalidEncoding, |
| 8 | InvalidLength, |
| 9 | } |
| 10 | |
| 11 | impl fmt::Display for Error { |
| 12 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 13 | match self { |
| 14 | Error::InvalidEncoding => f.write_str("invalid Base16 encoding"), |
| 15 | Error::InvalidLength => f.write_str("invalid Base16 length"), |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // A marker trait with no items: we do not model trait resolution, so it |
| 21 | // generates nothing. |
| 22 | impl core::error::Error for Error {} |
| 23 | |
| 24 | struct Point { |
| 25 | x: i32, |
| 26 | y: i32, |
| 27 | } |
| 28 | |
| 29 | impl fmt::Display for Point { |
| 30 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 31 | write!(f, "({}, {})", self.x, self.y) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl Point { |
| 36 | fn sum(&self) -> i32 { |
| 37 | self.x + self.y |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn main() { |
| 42 | println!("{}", Error::InvalidEncoding); |
| 43 | println!("{}", Error::InvalidLength); |
| 44 | println!("{:?}", Error::InvalidLength); |
| 45 | let p = Point { x: 3, y: -4 }; |
| 46 | println!("{} {}", p, p.sum()); |
| 47 | } |