[go: up one dir, main page]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![no_std]

use core::fmt;

#[doc(hidden)]
pub use equator_macro::assert as __assert_impl;

#[macro_export]
macro_rules! assert {
    ($($tokens: tt)*) => {
        $crate::__assert_impl!($crate, $($tokens)*)
    };
}

#[macro_export]
macro_rules! debug_assert {
    ($($tokens: tt)*) => {
        if cfg!(debug_assertions) {
            $crate::__assert_impl!($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)]
    #[repr(C)]
    pub struct CmpExpr<Cmp, Lhs, Rhs> {
        pub cmp: Cmp,
        pub lhs: Lhs,
        pub rhs: Rhs,
    }

    #[derive(Copy, Clone)]
    #[repr(C)]
    pub struct CustomCmpExpr<Cmp, Lhs, Rhs> {
        pub cmp: Cmp,
        pub lhs: Lhs,
        pub rhs: Rhs,
    }

    #[derive(Copy, Clone)]
    pub struct AndExpr<Lhs, Rhs> {
        pub lhs: Lhs,
        pub rhs: Rhs,
    }

    #[derive(Copy, Clone)]
    pub struct OrExpr<Lhs, Rhs> {
        pub lhs: Lhs,
        pub rhs: Rhs,
    }
}

pub trait Cmp<Lhs: ?Sized, Rhs: ?Sized>: Sized {
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool;
}
pub trait DisplayCmp {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter) -> fmt::Result;
}

#[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;

impl DisplayCmp for Eq {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} == {rhs}")
    }
}
impl DisplayCmp for Ne {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} != {rhs}")
    }
}
impl DisplayCmp for Le {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} <= {rhs}")
    }
}
impl DisplayCmp for Ge {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} >= {rhs}")
    }
}
impl DisplayCmp for Lt {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} < {rhs}")
    }
}
impl DisplayCmp for Gt {
    fn fmt(&self, lhs: &str, rhs: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{lhs} > {rhs}")
    }
}

impl<Rhs: ?Sized, Lhs: ?Sized + PartialEq<Rhs>> Cmp<Lhs, Rhs> for Eq {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs == *rhs
    }
}
impl<Rhs: ?Sized, Lhs: ?Sized + PartialEq<Rhs>> Cmp<Lhs, Rhs> for Ne {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs != *rhs
    }
}
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Le {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs <= *rhs
    }
}
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Ge {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs >= *rhs
    }
}
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Lt {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs < *rhs
    }
}
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Gt {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        *lhs > *rhs
    }
}

impl<Lhs: ?Sized, Rhs: ?Sized, C: Cmp<Lhs, Rhs>> Cmp<Lhs, Rhs> for &C {
    #[inline(always)]
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> bool {
        (**self).test(lhs, rhs)
    }
}

#[doc(hidden)]
pub struct CmpExpr;
#[doc(hidden)]
pub struct CustomCmpExpr;
#[doc(hidden)]
pub struct AndExpr<L, R>(L, R);
#[doc(hidden)]
pub struct OrExpr<L, R>(L, R);

#[doc(hidden)]
pub struct Message<'a>(pub core::fmt::Arguments<'a>);
#[doc(hidden)]
pub struct NoMessage;

impl From<NoMessage> for core::fmt::Arguments<'_> {
    fn from(_: NoMessage) -> Self {
        core::format_args!("")
    }
}

impl<'a> From<Message<'a>> for core::fmt::Arguments<'a> {
    fn from(t: Message<'a>) -> Self {
        t.0
    }
}

#[inline(always)]
#[doc(hidden)]
pub const fn vtable_for<T: traits::DynInfo>(_: &T) -> &'static T::VTable {
    T::VTABLE
}

#[cold]
#[inline(never)]
#[doc(hidden)]
#[track_caller]
pub fn panic_failed_assert<'a, M: Into<core::fmt::Arguments<'a>>, D: decompose::Recompose>(
    debug_lhs: D::DebugLhs,
    debug_rhs: D::DebugRhs,
    debug_cmp: D::DebugCmp,
    source: &'static structures::WithSource<D::Source, &'static D::VTable>,
    message: M,
) -> ! {
    panic!(
        "{:#?}",
        structures::DebugMessage::<D> {
            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);
    }
}