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
use std::fmt::{Debug, Formatter, Result};

pub struct MaybeDebugWrapper<'a, T: ?Sized + DebugOnStable>(&'a T);

trait MaybeDebug {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}

#[cfg(feature = "nightly")]
/// This trait is equivalent to `Debug` on non-nightly compilers, and is implemented for all types
/// on nightly compilers.
pub trait DebugOnStable {}
#[cfg(feature = "nightly")]
impl<T> DebugOnStable for T {}

#[cfg(not(feature = "nightly"))]
/// This trait is equivalent to `Debug` on non-nightly compilers, and is implemented for all types
/// on nightly compilers.
pub use std::fmt::{Debug as DebugOnStable};

#[cfg(feature = "nightly")]
impl<T> MaybeDebug for T {
    default fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(f, "???")
    }
}

impl<T: Debug> MaybeDebug for T {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        self.fmt(f)
    }
}

impl<'t, T: ?Sized + DebugOnStable> Debug for MaybeDebugWrapper<'t, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        self.0.fmt(f)
    }
}

pub fn dbg<T: ?Sized + DebugOnStable>(t: &T) -> MaybeDebugWrapper<'_, T> {
    MaybeDebugWrapper(t)
}