use crate::{ decompose::PtrToDeref, spec::{debug::CmpDebugWrapper, sized::CmpSizedWrapper, Wrapper}, Cmp, CmpDisplay, CmpError, }; use core::fmt; #[repr(transparent)] #[derive(Copy, Clone)] pub struct ByVal(pub T); #[repr(transparent)] #[derive(Copy, Clone)] pub struct ByRef(pub T); impl fmt::Debug for ByVal { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl fmt::Debug for ByRef { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } impl ByRef { #[inline(always)] pub fn get_ptr(&self) -> *const () { self as *const _ as *const () } } impl ByVal { const FIT_IN_PTR: bool = core::mem::size_of::() <= core::mem::size_of::<*const ()>() && core::mem::align_of::() <= core::mem::align_of::<*const ()>(); #[inline(always)] pub fn get_ptr(&self) -> *const () { if Self::FIT_IN_PTR { let mut out = core::ptr::null::<()>(); unsafe { *((&mut out) as *mut *const () as *mut T) = self.0; }; out } else { self as *const _ as *const () } } } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C: Cmp, Wrapper>, > Cmp, Wrapper> for CmpByValWrapper { #[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(CmpByValWrapper) } } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C: CmpError, Wrapper>, > CmpError, Wrapper, Wrapper> for CmpByValWrapper { type Error = CmpByValWrapper; } impl< Lhs: ?Sized + core::ops::Deref, Rhs: ?Sized + core::ops::Deref, C, E: CmpDisplay, Wrapper>, > CmpDisplay, Wrapper, Wrapper> for CmpByValWrapper { fn fmt( &self, cmp: &CmpByValWrapper, 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 ByVal { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl core::ops::Deref for ByRef { type Target = T; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl ByValWrap { #[inline(always)] pub fn do_wrap(self, value: &T) -> &ByVal { unsafe { &*(value as *const T as *const _) } } } impl ByRefWrap { #[inline(always)] pub fn do_wrap(self, value: &T) -> &ByRef { unsafe { &*(value as *const T as *const _) } } } impl<'a, C> CmpByValWrapper>> { #[inline(always)] pub fn __wrap_ref(self) -> &'a CmpByValWrapper>> { unsafe { &*(self.0 .0 .0 as *const C as *const CmpByValWrapper>>) } } } impl TryByValWrap for &Wrapper<&T> { type Wrap = ByValWrap; #[inline] fn wrap_by_val(&self) -> Self::Wrap { ByValWrap } } impl TryByValWrap for Wrapper { type Wrap = ByRefWrap; #[inline] fn wrap_by_val(&self) -> Self::Wrap { ByRefWrap } } pub struct ByValWrap; pub struct ByRefWrap; #[repr(transparent)] #[derive(Copy, Clone)] pub struct CmpByValWrapper(pub T); pub trait TryByValWrap { type Wrap; fn wrap_by_val(&self) -> Self::Wrap; } pub(crate) trait DerefVTable { const VTABLE: unsafe fn(*const *const ()) -> *const (); } unsafe fn no_deref(ptr: *const *const ()) -> *const () { ptr as *const () } unsafe fn deref(ptr: *const *const ()) -> *const () { *ptr } impl DerefVTable for ByVal { const VTABLE: PtrToDeref = { if ByVal::::FIT_IN_PTR { no_deref } else { deref } }; } impl DerefVTable for ByRef { const VTABLE: PtrToDeref = { deref }; }