mockers/
dbg.rs

1use std::fmt::{Debug, Formatter, Result};
2
3pub struct MaybeDebugWrapper<'a, T: ?Sized + DebugOnStable>(&'a T);
4
5trait MaybeDebug {
6    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
7}
8
9#[cfg(feature = "nightly")]
10/// This trait is equivalent to `Debug` on non-nightly compilers, and is implemented for all types
11/// on nightly compilers.
12pub trait DebugOnStable {}
13#[cfg(feature = "nightly")]
14impl<T> DebugOnStable for T {}
15
16#[cfg(not(feature = "nightly"))]
17/// This trait is equivalent to `Debug` on non-nightly compilers, and is implemented for all types
18/// on nightly compilers.
19pub use std::fmt::{Debug as DebugOnStable};
20
21#[cfg(feature = "nightly")]
22impl<T> MaybeDebug for T {
23    default fn fmt(&self, f: &mut Formatter<'_>) -> Result {
24        write!(f, "???")
25    }
26}
27
28impl<T: Debug> MaybeDebug for T {
29    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
30        self.fmt(f)
31    }
32}
33
34impl<'t, T: ?Sized + DebugOnStable> Debug for MaybeDebugWrapper<'t, T> {
35    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
36        self.0.fmt(f)
37    }
38}
39
40pub fn dbg<T: ?Sized + DebugOnStable>(t: &T) -> MaybeDebugWrapper<'_, T> {
41    MaybeDebugWrapper(t)
42}