use crate::{ decompose::{PtrToCmp, PtrToDebug, PtrToDeref, PtrToDisplay}, expr, spec, spec::by_val::{ByVal, DerefVTable}, spec::Wrapper, Cmp, CmpDisplay, }; use core::{fmt, ops::Deref}; pub trait Expr { type Result: Eval; type Marker; fn eval_expr(&self) -> bool; #[inline(always)] fn __marker(&self) -> core::marker::PhantomData { core::marker::PhantomData } } pub trait Eval { fn eval(&self) -> bool; } impl Eval for Result<(), E> { #[inline(always)] fn eval(&self) -> bool { self.is_ok() } } impl Eval for expr::AndExpr { #[inline(always)] fn eval(&self) -> bool { self.lhs.eval() && self.rhs.eval() } } impl Eval for expr::OrExpr { #[inline(always)] fn eval(&self) -> bool { self.lhs.eval() || self.rhs.eval() } } impl Expr for bool { type Result = Result<(), ()>; type Marker = bool; #[inline(always)] fn eval_expr(&self) -> bool { *self } } impl< Lhs3: Deref, Rhs3: Deref, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > Expr for expr::CmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where Lhs3::Target: Deref, Rhs3::Target: Deref, ::Target: Deref, ::Target: Deref, { type Result = Result<(), C::Error>; type Marker = crate::CmpExpr; #[inline(always)] fn eval_expr(&self) -> bool { self.cmp.0 .0 .0.test(&***self.lhs, &***self.rhs).is_ok() } } impl< Lhs3: Deref, Rhs3: Deref, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > Expr for expr::CustomCmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where Lhs3::Target: Deref, Rhs3::Target: Deref, ::Target: Deref, ::Target: Deref, { type Result = Result<(), C::Error>; type Marker = crate::CustomCmpExpr; #[inline(always)] fn eval_expr(&self) -> bool { self.cmp.0 .0 .0.test(&***self.lhs, &***self.rhs).is_ok() } } impl Expr for expr::AndExpr { type Result = expr::AndExpr; type Marker = crate::AndExpr; #[inline(always)] fn eval_expr(&self) -> bool { self.lhs.eval_expr() && self.rhs.eval_expr() } } impl Expr for expr::OrExpr { type Result = expr::OrExpr; type Marker = crate::OrExpr; #[inline(always)] fn eval_expr(&self) -> bool { self.lhs.eval_expr() || self.rhs.eval_expr() } } pub trait DynInfoType { type VTable: Copy + 'static; const NULL_VTABLE: &'static Self::VTable; } pub trait DynInfo: DynInfoType { const VTABLE: &'static Self::VTable; #[inline(always)] fn vtable(&self) -> &'static Self::VTable { Self::VTABLE } } impl DynInfoType for bool { type VTable = (); const NULL_VTABLE: &'static Self::VTable = &(); } impl DynInfo for bool { const VTABLE: &'static Self::VTable = &(); } impl DynInfoType for expr::AndExpr { type VTable = expr::AndExpr<&'static Lhs::VTable, &'static Rhs::VTable>; const NULL_VTABLE: &'static Self::VTable = &expr::AndExpr { lhs: Lhs::NULL_VTABLE, rhs: Rhs::NULL_VTABLE, }; } impl DynInfoType for expr::OrExpr { type VTable = expr::OrExpr<&'static Lhs::VTable, &'static Rhs::VTable>; const NULL_VTABLE: &'static Self::VTable = &expr::OrExpr { lhs: Lhs::NULL_VTABLE, rhs: Rhs::NULL_VTABLE, }; } impl DynInfo for expr::AndExpr { const VTABLE: &'static Self::VTable = &expr::AndExpr { lhs: Lhs::VTABLE, rhs: Rhs::VTABLE, }; } impl DynInfo for expr::OrExpr { const VTABLE: &'static Self::VTable = &expr::OrExpr { lhs: Lhs::VTABLE, rhs: Rhs::VTABLE, }; } unsafe fn as_display_vptr<'a, C, Lhs, Rhs, E: CmpDisplay>( ptr: *const (), ) -> &'a dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug> { #[repr(transparent)] struct DynDisplay( E, core::marker::PhantomData, core::marker::PhantomData, core::marker::PhantomData, ); impl> CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug> for DynDisplay { fn fmt( &self, cmp: &*const (), lhs: &dyn fmt::Debug, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &dyn fmt::Debug, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { unsafe { let lhs = &*(lhs as *const dyn fmt::Debug as *const Lhs); let rhs = &*(rhs as *const dyn fmt::Debug as *const Rhs); let cmp = &*((*cmp) as *const C); let err = &self.0; err.fmt( cmp, lhs, lhs_source, lhs_debug, rhs, rhs_source, rhs_debug, f, ) } } } core::mem::transmute::< &'_ dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, &'static dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, >( (&*(ptr as *const DynDisplay)) as &dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, ) } unsafe fn as_dyn_display_vptr<'a, C, E: CmpDisplay>( ptr: *const (), ) -> &'a dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug> { #[repr(transparent)] struct DynDisplay(E, core::marker::PhantomData); impl> CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug> for DynDisplay { fn fmt( &self, cmp: &*const (), lhs: &(dyn fmt::Debug + 'static), lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &(dyn fmt::Debug + 'static), rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { unsafe { let cmp = &*((*cmp) as *const C); let err = &self.0; err.fmt( cmp, lhs, lhs_source, lhs_debug, rhs, rhs_source, rhs_debug, f, ) } } } core::mem::transmute::< &'_ dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, &'static dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, >( (&*(ptr as *const DynDisplay)) as &dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>, ) } unsafe fn as_cmp_vptr< Lhs2: Deref, Rhs2: Deref, C: Cmp<::Target, ::Target>, >( out: *mut (), cmp: *const (), lhs: *const (), rhs: *const (), ) where Lhs2::Target: Deref, Rhs2::Target: Deref, { let out = out as *mut Result<(), C::Error>; let cmp = &*(cmp as *const C); let lhs = &*(lhs as *const Lhs2); let rhs = &*(rhs as *const Rhs2); out.write(cmp.test(&**lhs, &**rhs)); } unsafe fn as_debug_vptr(ptr: *const ()) -> &'static dyn fmt::Debug { core::mem::transmute::<&'_ dyn fmt::Debug, &'static dyn fmt::Debug>( (&*(ptr as *const T)) as &dyn fmt::Debug, ) } impl< Lhs3: Deref + fmt::Debug + DerefVTable, Rhs3: Deref + fmt::Debug + DerefVTable, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > DynInfoType for expr::CmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where C::Error: CmpDisplay, Lhs3::Target: Deref, Rhs3::Target: Deref, ::Target: Deref, ::Target: Deref, { type VTable = expr::CmpExpr<(PtrToDisplay, PtrToCmp), (PtrToDebug, PtrToDeref), (PtrToDebug, PtrToDeref)>; const NULL_VTABLE: &'static Self::VTable = &expr::CmpExpr { cmp: ( as_dyn_display_vptr::< spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, >, as_cmp_vptr::>, ByVal>, crate::Eq>, ), lhs: (as_debug_vptr::, Lhs3::VTABLE), rhs: (as_debug_vptr::, Rhs3::VTABLE), }; } impl< Lhs3: Deref + fmt::Debug + DerefVTable, Rhs3: Deref + fmt::Debug + DerefVTable, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > DynInfoType for expr::CustomCmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where C::Error: CmpDisplay< C, <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, Lhs3::Target: Deref, Rhs3::Target: Deref, ::Target: Deref, ::Target: Deref, { type VTable = expr::CustomCmpExpr< (PtrToDisplay, PtrToCmp), (PtrToDebug, PtrToDeref), (PtrToDebug, PtrToDeref), >; const NULL_VTABLE: &'static Self::VTable = &expr::CustomCmpExpr { cmp: ( as_display_vptr::< spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, Wrapper, Wrapper, spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, >, as_cmp_vptr::>, ByVal>, crate::Eq>, ), lhs: (as_debug_vptr::, Lhs3::VTABLE), rhs: (as_debug_vptr::, Rhs3::VTABLE), }; } impl< Lhs3: Deref + fmt::Debug + DerefVTable, Rhs3: Deref + fmt::Debug + DerefVTable, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > DynInfo for expr::CmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where C::Error: CmpDisplay, Lhs3::Target: Sized + Deref, Rhs3::Target: Sized + Deref, ::Target: Deref, ::Target: Deref, { const VTABLE: &'static Self::VTable = &expr::CmpExpr { cmp: ( as_dyn_display_vptr::< spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, >, as_cmp_vptr::, ), lhs: (as_debug_vptr::, Lhs3::VTABLE), rhs: (as_debug_vptr::, Rhs3::VTABLE), }; } impl< Lhs3: Deref + fmt::Debug + DerefVTable, Rhs3: Deref + fmt::Debug + DerefVTable, C: Cmp< <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, > DynInfo for expr::CustomCmpExpr< &spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, &Lhs3, &Rhs3, > where C::Error: CmpDisplay< C, <<::Target as Deref>::Target as Deref>::Target, <<::Target as Deref>::Target as Deref>::Target, >, Lhs3::Target: Sized + Deref, Rhs3::Target: Sized + Deref, ::Target: Deref, ::Target: Deref, { const VTABLE: &'static Self::VTable = &expr::CustomCmpExpr { cmp: ( as_display_vptr::< spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, Wrapper, Wrapper, spec::by_val::CmpByValWrapper< spec::sized::CmpSizedWrapper>, >, >, as_cmp_vptr::, ), lhs: (as_debug_vptr::, Lhs3::VTABLE), rhs: (as_debug_vptr::, Rhs3::VTABLE), }; }