Debian Codegraph @

third_party/rust/warp/src/error.rs

raw
1 use std::convert::Infallible;
2 use std::error::Error as StdError;
3 use std::fmt;
4
5 type BoxError = Box<dyn std::error::Error + Send + Sync>;
6
7 /// Errors that can happen inside warp.
8 pub struct Error {
9 inner: BoxError,
10 }
11
12 impl Error {
13 pub(crate) fn new<E: Into<BoxError>>(err: E) -> Error {
14 Error { inner: err.into() }
15 }
16 }
17
18 impl fmt::Debug for Error {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 // Skip showing worthless `Error { .. }` wrapper.
21 fmt::Debug::fmt(&self.inner, f)
22 }
23 }
24
25 impl fmt::Display for Error {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 fmt::Display::fmt(&self.inner, f)
28 }
29 }
30
31 impl StdError for Error {
32 fn source(&self) -> Option<&(dyn StdError + 'static)> {
33 Some(self.inner.as_ref())
34 }
35 }
36
37 impl From<Infallible> for Error {
38 fn from(infallible: Infallible) -> Error {
39 match infallible {}
40 }
41 }
42
43 #[test]
44 fn error_size_of() {
45 assert_eq!(
46 ::std::mem::size_of::<Error>(),
47 ::std::mem::size_of::<usize>() * 2
48 );
49 }
50
51 #[test]
52 fn error_source() {
53 let e = Error::new(std::fmt::Error {});
54 assert!(e.source().unwrap().is::<std::fmt::Error>());
55 }
56
57 macro_rules! unit_error {
58 (
59 $(#[$docs:meta])*
60 $pub:vis $typ:ident: $display:literal
61 ) => (
62 $(#[$docs])*
63 $pub struct $typ { _p: (), }
64
65 impl ::std::fmt::Debug for $typ {
66 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
67 f.debug_struct(stringify!($typ)).finish()
68 }
69 }
70
71 impl ::std::fmt::Display for $typ {
72 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
73 f.write_str($display)
74 }
75 }
76
77 impl ::std::error::Error for $typ {}
78 )
79 }