serde_bench/error.rs
1use serde::{de, ser};
2use std::fmt::{self, Display};
3use std::{error, io, str, string};
4
5#[derive(Debug)]
6pub struct Error {
7 msg: String,
8}
9
10pub type Result<T> = std::result::Result<T, Error>;
11
12impl Error {
13 pub fn new<T: Display>(msg: T) -> Self {
14 Error {
15 msg: msg.to_string(),
16 }
17 }
18}
19
20impl error::Error for Error {}
21
22impl Display for Error {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 f.write_str(&self.msg)
25 }
26}
27
28impl ser::Error for Error {
29 fn custom<T: Display>(msg: T) -> Self {
30 Error::new(msg)
31 }
32}
33
34impl de::Error for Error {
35 fn custom<T: Display>(msg: T) -> Self {
36 Error::new(msg)
37 }
38}
39
40impl From<io::Error> for Error {
41 fn from(err: io::Error) -> Self {
42 Error::new(err)
43 }
44}
45
46impl From<str::Utf8Error> for Error {
47 fn from(err: str::Utf8Error) -> Self {
48 Error::new(err)
49 }
50}
51
52impl From<string::FromUtf8Error> for Error {
53 fn from(err: string::FromUtf8Error) -> Self {
54 Error::new(err)
55 }
56}