use crate::{spec::Wrapper, Cmp, CmpDisplay, CmpError}; use core::fmt; #[repr(transparent)] #[derive(Copy, Clone)] pub struct SizedWrapper(pub T); #[repr(transparent)] pub struct NoSizedWrapper<'a, T: ?Sized>(pub &'a T); impl Copy for NoSizedWrapper<'_, T> {} impl Clone for NoSizedWrapper<'_, T> { #[inline(always)] fn clone(&self) -> Self { *self } } impl fmt::Debug for SizedWrapper { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl fmt::Debug for NoSizedWrapper<'_, T> { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl SizedWrapper { #[inline(always)] pub fn get(&self) -> &Self { self } } impl NoSizedWrapper<'_, T> { #[inline(always)] pub fn get(&self) -> &Self { self } } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C: Cmp, Wrapper>, > Cmp, Wrapper> for CmpSizedWrapper { #[inline(always)] fn test(&self, lhs: &Wrapper, rhs: &Wrapper) -> Result<(), Self::Error> { self.0 .test( unsafe { &*((&*lhs.0) as *const Lhs::Target as *const Wrapper) }, unsafe { &*((&*rhs.0) as *const Rhs::Target as *const Wrapper) }, ) .map_err(CmpSizedWrapper) } } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C: CmpError, Wrapper>, > CmpError, Wrapper, Wrapper> for CmpSizedWrapper { type Error = CmpSizedWrapper; } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C, E: CmpDisplay, Wrapper>, > CmpDisplay, Wrapper, Wrapper> for CmpSizedWrapper { fn fmt( &self, cmp: &CmpSizedWrapper, lhs: &Wrapper, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Wrapper, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { self.0.fmt( &cmp.0, unsafe { &*((&*lhs.0) as *const Lhs::Target as *const Wrapper) }, lhs_source, lhs_debug, unsafe { &*((&*rhs.0) as *const Rhs::Target as *const Wrapper) }, rhs_source, rhs_debug, f, ) } } impl core::ops::Deref for SizedWrapper { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl core::ops::Deref for NoSizedWrapper<'_, T> { type Target = T; #[inline] fn deref(&self) -> &Self::Target { self.0 } } impl SizedWrap { #[inline(always)] pub fn do_wrap(self, value: &T) -> &SizedWrapper { unsafe { &*(value as *const T as *const _) } } } impl NoSizedWrap { #[inline(always)] pub fn do_wrap(self, value: &T) -> NoSizedWrapper<'_, T> { NoSizedWrapper(value) } } #[repr(transparent)] #[derive(Copy, Clone)] pub struct CmpSizedWrapper(pub T); pub struct SizedWrap; pub struct NoSizedWrap; pub trait TrySizedWrap { type Wrap; fn wrap_sized(&self) -> Self::Wrap; } impl TrySizedWrap for &Wrapper<&T> { type Wrap = SizedWrap; #[inline] fn wrap_sized(&self) -> Self::Wrap { SizedWrap } } impl TrySizedWrap for Wrapper<&T> { type Wrap = NoSizedWrap; #[inline] fn wrap_sized(&self) -> Self::Wrap { NoSizedWrap } }