#![no_std] use core::fmt; #[doc(hidden)] pub use equator_macro as imp; #[macro_export] macro_rules! assert { ($($tokens: tt)*) => { $crate::imp::assert!($crate, $($tokens)*) }; } #[macro_export] macro_rules! debug_assert { ($($tokens: tt)*) => { if cfg!(debug_assertions) { $crate::imp::assert!($crate, $($tokens)*) } }; } #[doc(hidden)] pub mod decompose; #[doc(hidden)] pub mod spec; #[doc(hidden)] pub mod structures; #[doc(hidden)] pub mod traits; #[doc(hidden)] pub mod expr { #[derive(Copy, Clone, Debug)] #[repr(C)] pub struct CmpExpr { pub cmp: Cmp, pub lhs: Lhs, pub rhs: Rhs, } #[derive(Copy, Clone, Debug)] #[repr(C)] pub struct CustomCmpExpr { pub cmp: Cmp, pub lhs: Lhs, pub rhs: Rhs, } #[derive(Copy, Clone, Debug)] pub struct AndExpr { pub lhs: Lhs, pub rhs: Rhs, } #[derive(Copy, Clone, Debug)] pub struct OrExpr { pub lhs: Lhs, pub rhs: Rhs, } } pub trait CmpError: Sized { type Error; } pub trait CmpDisplay { fn fmt( &self, cmp: &C, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result; } pub trait Cmp: CmpError { fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), Self::Error>; } #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Eq; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Ne; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Le; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Ge; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Lt; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Gt; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct EqError; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct NeError; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct LeError; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct GeError; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct LtError; #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct GtError; fn display_cmp_impl( cmp: &str, lhs: &dyn fmt::Debug, lhs_source: &str, rhs: &dyn fmt::Debug, rhs_source: &str, f: &mut fmt::Formatter, ) -> fmt::Result { write!(f, "Assertion failed: {lhs_source} {cmp} {rhs_source}\n")?; write!(f, "- {lhs_source} = {lhs:#?}\n")?; write!(f, "- {rhs_source} = {rhs:#?}") } impl CmpError for Eq { type Error = EqError; } impl CmpDisplay for EqError { fn fmt( &self, cmp: &Eq, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl("==", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl CmpError for Ne { type Error = NeError; } impl CmpDisplay for NeError { fn fmt( &self, cmp: &Ne, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl("!=", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl CmpError for Lt { type Error = LtError; } impl CmpDisplay for LtError { fn fmt( &self, cmp: &Lt, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl("<", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl CmpError for Gt { type Error = GtError; } impl CmpDisplay for GtError { fn fmt( &self, cmp: &Gt, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl(">", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl CmpError for Le { type Error = LeError; } impl CmpDisplay for LeError { fn fmt( &self, cmp: &Le, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl("<=", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl CmpError for Ge { type Error = GeError; } impl CmpDisplay for GeError { fn fmt( &self, cmp: &Ge, lhs: &Lhs, lhs_source: &str, lhs_debug: &dyn fmt::Debug, rhs: &Rhs, rhs_source: &str, rhs_debug: &dyn fmt::Debug, f: &mut fmt::Formatter, ) -> fmt::Result { _ = (lhs, rhs, cmp); display_cmp_impl(">=", lhs_debug, lhs_source, rhs_debug, rhs_source, f) } } impl> Cmp for Eq { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), EqError> { if *lhs == *rhs { Ok(()) } else { Err(EqError) } } } impl> Cmp for Ne { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), NeError> { if *lhs != *rhs { Ok(()) } else { Err(NeError) } } } impl> Cmp for Le { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), LeError> { if *lhs <= *rhs { Ok(()) } else { Err(LeError) } } } impl> Cmp for Ge { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), GeError> { if *lhs >= *rhs { Ok(()) } else { Err(GeError) } } } impl> Cmp for Lt { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), LtError> { if *lhs < *rhs { Ok(()) } else { Err(LtError) } } } impl> Cmp for Gt { #[inline(always)] fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), GtError> { if *lhs > *rhs { Ok(()) } else { Err(GtError) } } } #[doc(hidden)] pub struct CmpExpr; #[doc(hidden)] pub struct CustomCmpExpr(pub core::marker::PhantomData); #[doc(hidden)] pub struct AndExpr(pub L, pub R); #[doc(hidden)] pub struct OrExpr(pub L, pub R); #[doc(hidden)] pub struct Message<'a>(pub core::fmt::Arguments<'a>); #[doc(hidden)] pub struct NoMessage; impl From for core::fmt::Arguments<'_> { fn from(_: NoMessage) -> Self { core::format_args!("") } } impl<'a> From> for core::fmt::Arguments<'a> { fn from(t: Message<'a>) -> Self { t.0 } } #[cold] #[inline(never)] #[doc(hidden)] #[track_caller] pub fn panic_failed_assert<'a, M: Into>, D: decompose::Recompose>( __marker: core::marker::PhantomData, debug_lhs: D::DebugLhs, debug_rhs: D::DebugRhs, debug_cmp: D::DebugCmp, source: &'static structures::WithSource, message: M, ) -> ! { panic!( "{:#?}", structures::DebugMessage:: { source, debug_lhs, debug_rhs, debug_cmp, message: message.into(), } ) } #[cfg(test)] mod tests { use super::*; #[test] #[should_panic] fn test_assert() { assert!(false); } #[cfg(debug_assertions)] #[test] #[should_panic] fn test_debug_assert() { debug_assert!(false); } }