| 1 |
use crate::{spec::Wrapper, Cmp, CmpDisplay, CmpError}; |
| 2 |
use core::fmt; |
| 3 |
|
| 4 |
#[repr(transparent)] |
| 5 |
#[derive(Copy, Clone)] |
| 6 |
pub struct DebugWrapper<T: ?Sized>(pub T); |
| 7 |
#[repr(transparent)] |
| 8 |
#[derive(Copy, Clone)] |
| 9 |
pub struct NoDebugWrapper<T: ?Sized>(pub T); |
| 10 |
|
| 11 |
impl< |
| 12 |
Lhs: ?Sized + core::ops::Deref, |
| 13 |
Rhs: ?Sized + core::ops::Deref, |
| 14 |
C: CmpError<C, Lhs::Target, Rhs::Target>, |
| 15 |
> CmpError<CmpDebugWrapper<C>, Wrapper<Lhs>, Wrapper<Rhs>> for CmpDebugWrapper<C> |
| 16 |
{ |
| 17 |
type Error = CmpDebugWrapper<C::Error>; |
| 18 |
} |
| 19 |
|
| 20 |
impl< |
| 21 |
Lhs: ?Sized + core::ops::Deref, |
| 22 |
Rhs: ?Sized + core::ops::Deref, |
| 23 |
C, |
| 24 |
E: CmpDisplay<C, Lhs::Target, Rhs::Target>, |
| 25 |
> CmpDisplay<CmpDebugWrapper<C>, Wrapper<Lhs>, Wrapper<Rhs>> for CmpDebugWrapper<E> |
| 26 |
{ |
| 27 |
fn fmt( |
| 28 |
&self, |
| 29 |
cmp: &CmpDebugWrapper<C>, |
| 30 |
lhs: &Wrapper<Lhs>, |
| 31 |
lhs_source: &str, |
| 32 |
lhs_debug: &dyn fmt::Debug, |
| 33 |
rhs: &Wrapper<Rhs>, |
| 34 |
rhs_source: &str, |
| 35 |
rhs_debug: &dyn fmt::Debug, |
| 36 |
f: &mut fmt::Formatter, |
| 37 |
) -> fmt::Result { |
| 38 |
self.0.fmt( |
| 39 |
&cmp.0, &*lhs.0, lhs_source, lhs_debug, &*rhs.0, rhs_source, rhs_debug, f, |
| 40 |
) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl< |
| 45 |
Lhs: ?Sized + core::ops::Deref, |
| 46 |
Rhs: ?Sized + core::ops::Deref, |
| 47 |
C: Cmp<Lhs::Target, Rhs::Target>, |
| 48 |
> Cmp<Wrapper<Lhs>, Wrapper<Rhs>> for CmpDebugWrapper<C> |
| 49 |
{ |
| 50 |
#[inline(always)] |
| 51 |
fn test(&self, lhs: &Wrapper<Lhs>, rhs: &Wrapper<Rhs>) -> Result<(), Self::Error> { |
| 52 |
self.0.test(&*lhs.0, &*rhs.0).map_err(CmpDebugWrapper) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl<T: ?Sized> core::ops::Deref for DebugWrapper<T> { |
| 57 |
type Target = T; |
| 58 |
|
| 59 |
#[inline] |
| 60 |
fn deref(&self) -> &Self::Target { |
| 61 |
&self.0 |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<T: ?Sized> core::ops::Deref for NoDebugWrapper<T> { |
| 66 |
type Target = T; |
| 67 |
|
| 68 |
#[inline] |
| 69 |
fn deref(&self) -> &Self::Target { |
| 70 |
&self.0 |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl<T: ?Sized + fmt::Debug> fmt::Debug for DebugWrapper<T> { |
| 75 |
#[inline(always)] |
| 76 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 |
self.0.fmt(f) |
| 78 |
} |
| 79 |
} |
| 80 |
impl<T: ?Sized> fmt::Debug for NoDebugWrapper<T> { |
| 81 |
#[inline(always)] |
| 82 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 83 |
write!( |
| 84 |
f, |
| 85 |
"<object of type \"{}\" at address {:?}>", |
| 86 |
core::any::type_name::<T>(), |
| 87 |
self as *const _ as *const () |
| 88 |
) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
pub struct DebugWrap; |
| 93 |
pub struct NoDebugWrap; |
| 94 |
|
| 95 |
impl DebugWrap { |
| 96 |
#[inline(always)] |
| 97 |
pub fn do_wrap<T: ?Sized>(self, value: &T) -> &DebugWrapper<T> { |
| 98 |
unsafe { &*(value as *const T as *const DebugWrapper<T>) } |
| 99 |
} |
| 100 |
} |
| 101 |
impl NoDebugWrap { |
| 102 |
#[inline(always)] |
| 103 |
pub fn do_wrap<T: ?Sized>(self, value: &T) -> &NoDebugWrapper<T> { |
| 104 |
unsafe { &*(value as *const T as *const NoDebugWrapper<T>) } |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[repr(transparent)] |
| 109 |
#[derive(Copy, Clone)] |
| 110 |
pub struct CmpDebugWrapper<T>(pub T); |
| 111 |
|
| 112 |
pub trait TryDebugWrap { |
| 113 |
type Wrap; |
| 114 |
fn wrap_debug(&self) -> Self::Wrap; |
| 115 |
} |
| 116 |
|
| 117 |
impl<T: fmt::Debug + ?Sized> TryDebugWrap for &Wrapper<T> { |
| 118 |
type Wrap = DebugWrap; |
| 119 |
|
| 120 |
#[inline] |
| 121 |
fn wrap_debug(&self) -> Self::Wrap { |
| 122 |
DebugWrap |
| 123 |
} |
| 124 |
} |
| 125 |
impl<T: ?Sized> TryDebugWrap for Wrapper<T> { |
| 126 |
type Wrap = NoDebugWrap; |
| 127 |
|
| 128 |
#[inline] |
| 129 |
fn wrap_debug(&self) -> Self::Wrap { |
| 130 |
NoDebugWrap |
| 131 |
} |
| 132 |
} |