| 1 |
use crate::{spec::Wrapper, Cmp, CmpDisplay, CmpError}; |
| 2 |
use core::fmt; |
| 3 |
|
| 4 |
#[repr(transparent)] |
| 5 |
#[derive(Copy, Clone)] |
| 6 |
pub struct SizedWrapper<T>(pub T); |
| 7 |
#[repr(transparent)] |
| 8 |
pub struct NoSizedWrapper<'a, T: ?Sized>(pub &'a T); |
| 9 |
|
| 10 |
impl<T: ?Sized> Copy for NoSizedWrapper<'_, T> {} |
| 11 |
impl<T: ?Sized> Clone for NoSizedWrapper<'_, T> { |
| 12 |
#[inline(always)] |
| 13 |
fn clone(&self) -> Self { |
| 14 |
*self |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
impl<T: fmt::Debug> fmt::Debug for SizedWrapper<T> { |
| 19 |
#[inline(always)] |
| 20 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 21 |
self.0.fmt(f) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl<T: ?Sized + fmt::Debug> fmt::Debug for NoSizedWrapper<'_, T> { |
| 26 |
#[inline(always)] |
| 27 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 28 |
self.0.fmt(f) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
impl<T> SizedWrapper<T> { |
| 33 |
#[inline(always)] |
| 34 |
pub fn get(&self) -> &Self { |
| 35 |
self |
| 36 |
} |
| 37 |
} |
| 38 |
impl<T: ?Sized> NoSizedWrapper<'_, T> { |
| 39 |
#[inline(always)] |
| 40 |
pub fn get(&self) -> &Self { |
| 41 |
self |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl< |
| 46 |
Lhs: ?Sized + core::ops::Deref, |
| 47 |
Rhs: ?Sized + core::ops::Deref, |
| 48 |
C: Cmp<Wrapper<Lhs::Target>, Wrapper<Rhs::Target>>, |
| 49 |
> Cmp<Wrapper<Lhs>, Wrapper<Rhs>> for CmpSizedWrapper<C> |
| 50 |
{ |
| 51 |
#[inline(always)] |
| 52 |
fn test(&self, lhs: &Wrapper<Lhs>, rhs: &Wrapper<Rhs>) -> Result<(), Self::Error> { |
| 53 |
self.0 |
| 54 |
.test( |
| 55 |
unsafe { &*((&*lhs.0) as *const Lhs::Target as *const Wrapper<Lhs::Target>) }, |
| 56 |
unsafe { &*((&*rhs.0) as *const Rhs::Target as *const Wrapper<Rhs::Target>) }, |
| 57 |
) |
| 58 |
.map_err(CmpSizedWrapper) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl< |
| 63 |
Lhs: ?Sized + core::ops::Deref, |
| 64 |
Rhs: ?Sized + core::ops::Deref, |
| 65 |
C: CmpError<C, Wrapper<Lhs::Target>, Wrapper<Rhs::Target>>, |
| 66 |
> CmpError<CmpSizedWrapper<C>, Wrapper<Lhs>, Wrapper<Rhs>> for CmpSizedWrapper<C> |
| 67 |
{ |
| 68 |
type Error = CmpSizedWrapper<C::Error>; |
| 69 |
} |
| 70 |
|
| 71 |
impl< |
| 72 |
Lhs: ?Sized + core::ops::Deref, |
| 73 |
Rhs: ?Sized + core::ops::Deref, |
| 74 |
C, |
| 75 |
E: CmpDisplay<C, Wrapper<Lhs::Target>, Wrapper<Rhs::Target>>, |
| 76 |
> CmpDisplay<CmpSizedWrapper<C>, Wrapper<Lhs>, Wrapper<Rhs>> for CmpSizedWrapper<E> |
| 77 |
{ |
| 78 |
fn fmt( |
| 79 |
&self, |
| 80 |
cmp: &CmpSizedWrapper<C>, |
| 81 |
lhs: &Wrapper<Lhs>, |
| 82 |
lhs_source: &str, |
| 83 |
lhs_debug: &dyn fmt::Debug, |
| 84 |
rhs: &Wrapper<Rhs>, |
| 85 |
rhs_source: &str, |
| 86 |
rhs_debug: &dyn fmt::Debug, |
| 87 |
f: &mut fmt::Formatter, |
| 88 |
) -> fmt::Result { |
| 89 |
self.0.fmt( |
| 90 |
&cmp.0, |
| 91 |
unsafe { &*((&*lhs.0) as *const Lhs::Target as *const Wrapper<Lhs::Target>) }, |
| 92 |
lhs_source, |
| 93 |
lhs_debug, |
| 94 |
unsafe { &*((&*rhs.0) as *const Rhs::Target as *const Wrapper<Rhs::Target>) }, |
| 95 |
rhs_source, |
| 96 |
rhs_debug, |
| 97 |
f, |
| 98 |
) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl<T> core::ops::Deref for SizedWrapper<T> { |
| 103 |
type Target = T; |
| 104 |
|
| 105 |
#[inline] |
| 106 |
fn deref(&self) -> &Self::Target { |
| 107 |
&self.0 |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl<T: ?Sized> core::ops::Deref for NoSizedWrapper<'_, T> { |
| 112 |
type Target = T; |
| 113 |
|
| 114 |
#[inline] |
| 115 |
fn deref(&self) -> &Self::Target { |
| 116 |
self.0 |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl SizedWrap { |
| 121 |
#[inline(always)] |
| 122 |
pub fn do_wrap<T>(self, value: &T) -> &SizedWrapper<T> { |
| 123 |
unsafe { &*(value as *const T as *const _) } |
| 124 |
} |
| 125 |
} |
| 126 |
impl NoSizedWrap { |
| 127 |
#[inline(always)] |
| 128 |
pub fn do_wrap<T: ?Sized>(self, value: &T) -> NoSizedWrapper<'_, T> { |
| 129 |
NoSizedWrapper(value) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[repr(transparent)] |
| 134 |
#[derive(Copy, Clone)] |
| 135 |
pub struct CmpSizedWrapper<T>(pub T); |
| 136 |
|
| 137 |
pub struct SizedWrap; |
| 138 |
pub struct NoSizedWrap; |
| 139 |
|
| 140 |
pub trait TrySizedWrap { |
| 141 |
type Wrap; |
| 142 |
fn wrap_sized(&self) -> Self::Wrap; |
| 143 |
} |
| 144 |
|
| 145 |
impl<T: Sized> TrySizedWrap for &Wrapper<&T> { |
| 146 |
type Wrap = SizedWrap; |
| 147 |
|
| 148 |
#[inline] |
| 149 |
fn wrap_sized(&self) -> Self::Wrap { |
| 150 |
SizedWrap |
| 151 |
} |
| 152 |
} |
| 153 |
impl<T: ?Sized> TrySizedWrap for Wrapper<&T> { |
| 154 |
type Wrap = NoSizedWrap; |
| 155 |
|
| 156 |
#[inline] |
| 157 |
fn wrap_sized(&self) -> Self::Wrap { |
| 158 |
NoSizedWrap |
| 159 |
} |
| 160 |
} |