1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
pub extern crate hazy_derive;
pub use hazy_derive::*;

pub trait OpaqueDebug {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result;
}

macro_rules! debug_impls {
    ( $( $t:ty => $val:expr,)* ) => {
        $(
        impl OpaqueDebug for $t {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                write!(f, $val)
            }
        }
        )*
    };
    ( $( $t:ty,)* ) => {
        debug_impls! {$(
            $t => "_",
        )*}
    };
    ( tuple $($t:ident,)*) => {
        impl<$($t,)*> OpaqueDebug for ($($t,)*) {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                write!(f, "(")?;
                // Getting around the need to use `$t` in the repetition:
                #[allow(non_snake_case)]
                let ($(ref $t,)*) = *self;
                $(
                    let _ = $t;
                    write!(f, "_, ")?;
                )*
                write!(f, ")")
            }
        }
    };
}

debug_impls! {
    u8,
    u16,
    u32,
    u64,
    u128,
    usize,
    i8,
    i16,
    i32,
    i64,
    i128,
    isize,
    f32,
    f64,
    bool,
}

debug_impls!(tuple T0,);
debug_impls!(tuple T0, T1,);
debug_impls!(tuple T0, T1, T2,);
debug_impls!(tuple T0, T1, T2, T3,);
debug_impls!(tuple T0, T1, T2, T3, T4,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6, T7,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6, T7, T8,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,);
debug_impls!(tuple T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,);

impl OpaqueDebug for ::std::string::String {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        if self.is_empty() {
            write!(f, "\"\"")
        } else {
            write!(f, "\"...\"")
        }
    }
}

impl<'a> OpaqueDebug for &'a str {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        if self.is_empty() {
            write!(f, "\"\"")
        } else {
            write!(f, "\"...\"")
        }
    }
}

impl<T, E> OpaqueDebug for Result<T, E>
where
    T: OpaqueDebug,
    E: ::std::fmt::Debug,
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match self {
            Ok(ref x) => {
                write!(f, "Ok(")?;
                OpaqueDebug::fmt(x, f)?;
                write!(f, ")")
            }
            Err(ref e) => write!(f, "Err({:?})", e),
        }
    }
}

impl<T> OpaqueDebug for Option<T>
where
    T: OpaqueDebug,
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match self {
            Some(ref x) => {
                write!(f, "Some(")?;
                OpaqueDebug::fmt(x, f)?;
                write!(f, ")")
            }
            None => write!(f, "None"),
        }
    }
}

impl<T> OpaqueDebug for Box<T>
where
    T: OpaqueDebug + ?Sized,
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "Box {{ ")?;
        OpaqueDebug::fmt(&**self, f)?;
        write!(f, " }}")
    }
}

impl<T> OpaqueDebug for Vec<T>
where
    T: OpaqueDebug,
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match self.len() {
            x if x < 13 => {
                write!(f, "[")?;
                let mut it = self.iter().peekable();
                while let Some(ref next) = it.next() {
                    OpaqueDebug::fmt(next, f)?;
                    if it.peek().is_some() {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")?;
                Ok(())
            }
            _ => write!(f, "[...]"),
        }
    }
}

impl<T> OpaqueDebug for [T]
where
    T: OpaqueDebug,
{
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match self.len() {
            x if x < 13 => {
                write!(f, "[")?;
                let mut it = self.iter().peekable();
                while let Some(ref next) = it.next() {
                    OpaqueDebug::fmt(next, f)?;
                    if it.peek().is_some() {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")?;
                Ok(())
            }
            _ => write!(f, "[...]"),
        }
    }
}

impl<'a, T: ?Sized + OpaqueDebug> OpaqueDebug for &'a T {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        OpaqueDebug::fmt(&**self, f)
    }
}

impl<'a, T: ?Sized + OpaqueDebug> OpaqueDebug for &'a mut T {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        OpaqueDebug::fmt(&**self, f)
    }
}