Coverage Report

Created: 2025-07-11 07:25

/rust/registry/src/index.crates.io-6f17d22bba15001f/equator-0.4.2/src/lib.rs
Line
Count
Source (jump to first uncovered line)
1
#![no_std]
2
3
use core::fmt;
4
5
#[doc(hidden)]
6
pub use equator_macro as imp;
7
8
#[macro_export]
9
macro_rules! assert {
10
    ($($tokens: tt)*) => {
11
        $crate::imp::assert!($crate, $($tokens)*)
12
    };
13
}
14
15
#[macro_export]
16
macro_rules! debug_assert {
17
    ($($tokens: tt)*) => {
18
        if cfg!(debug_assertions) {
19
            $crate::imp::assert!($crate, $($tokens)*)
20
        }
21
    };
22
}
23
24
#[doc(hidden)]
25
pub mod decompose;
26
#[doc(hidden)]
27
pub mod spec;
28
#[doc(hidden)]
29
pub mod structures;
30
#[doc(hidden)]
31
pub mod traits;
32
33
#[doc(hidden)]
34
pub mod expr {
35
    #[derive(Copy, Clone, Debug)]
36
    #[repr(C)]
37
    pub struct CmpExpr<Cmp, Lhs, Rhs> {
38
        pub cmp: Cmp,
39
        pub lhs: Lhs,
40
        pub rhs: Rhs,
41
    }
42
43
    #[derive(Copy, Clone, Debug)]
44
    #[repr(C)]
45
    pub struct CustomCmpExpr<Cmp, Lhs, Rhs> {
46
        pub cmp: Cmp,
47
        pub lhs: Lhs,
48
        pub rhs: Rhs,
49
    }
50
51
    #[derive(Copy, Clone, Debug)]
52
    pub struct AndExpr<Lhs, Rhs> {
53
        pub lhs: Lhs,
54
        pub rhs: Rhs,
55
    }
56
57
    #[derive(Copy, Clone, Debug)]
58
    pub struct OrExpr<Lhs, Rhs> {
59
        pub lhs: Lhs,
60
        pub rhs: Rhs,
61
    }
62
}
63
64
pub trait CmpError<C, Lhs: ?Sized, Rhs: ?Sized>: Sized {
65
    type Error;
66
}
67
68
pub trait CmpDisplay<C, Lhs: ?Sized, Rhs: ?Sized> {
69
    fn fmt(
70
        &self,
71
        cmp: &C,
72
        lhs: &Lhs,
73
        lhs_source: &str,
74
        lhs_debug: &dyn fmt::Debug,
75
        rhs: &Rhs,
76
        rhs_source: &str,
77
        rhs_debug: &dyn fmt::Debug,
78
        f: &mut fmt::Formatter,
79
    ) -> fmt::Result;
80
}
81
82
pub trait Cmp<Lhs: ?Sized, Rhs: ?Sized>: CmpError<Self, Lhs, Rhs> {
83
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), Self::Error>;
84
}
85
86
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
87
pub struct Eq;
88
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
89
pub struct Ne;
90
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
91
pub struct Le;
92
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
93
pub struct Ge;
94
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
95
pub struct Lt;
96
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
97
pub struct Gt;
98
99
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
100
pub struct EqError;
101
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
102
pub struct NeError;
103
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
104
pub struct LeError;
105
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
106
pub struct GeError;
107
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
108
pub struct LtError;
109
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
110
pub struct GtError;
111
112
0
fn display_cmp_impl(
113
0
    cmp: &str,
114
0
    lhs: &dyn fmt::Debug,
115
0
    lhs_source: &str,
116
0
    rhs: &dyn fmt::Debug,
117
0
    rhs_source: &str,
118
0
    f: &mut fmt::Formatter,
119
0
) -> fmt::Result {
120
0
    write!(f, "Assertion failed: {lhs_source} {cmp} {rhs_source}\n")?;
121
0
    write!(f, "- {lhs_source} = {lhs:#?}\n")?;
122
0
    write!(f, "- {rhs_source} = {rhs:#?}")
123
0
}
124
125
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Eq, Lhs, Rhs> for Eq {
126
    type Error = EqError;
127
}
128
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Eq, Lhs, Rhs> for EqError {
129
0
    fn fmt(
130
0
        &self,
131
0
        cmp: &Eq,
132
0
        lhs: &Lhs,
133
0
        lhs_source: &str,
134
0
        lhs_debug: &dyn fmt::Debug,
135
0
        rhs: &Rhs,
136
0
        rhs_source: &str,
137
0
        rhs_debug: &dyn fmt::Debug,
138
0
        f: &mut fmt::Formatter,
139
0
    ) -> fmt::Result {
140
0
        _ = (lhs, rhs, cmp);
141
0
        display_cmp_impl("==", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
142
0
    }
143
}
144
145
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Ne, Lhs, Rhs> for Ne {
146
    type Error = NeError;
147
}
148
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Ne, Lhs, Rhs> for NeError {
149
0
    fn fmt(
150
0
        &self,
151
0
        cmp: &Ne,
152
0
        lhs: &Lhs,
153
0
        lhs_source: &str,
154
0
        lhs_debug: &dyn fmt::Debug,
155
0
        rhs: &Rhs,
156
0
        rhs_source: &str,
157
0
        rhs_debug: &dyn fmt::Debug,
158
0
        f: &mut fmt::Formatter,
159
0
    ) -> fmt::Result {
160
0
        _ = (lhs, rhs, cmp);
161
0
        display_cmp_impl("!=", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
162
0
    }
163
}
164
165
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Lt, Lhs, Rhs> for Lt {
166
    type Error = LtError;
167
}
168
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Lt, Lhs, Rhs> for LtError {
169
0
    fn fmt(
170
0
        &self,
171
0
        cmp: &Lt,
172
0
        lhs: &Lhs,
173
0
        lhs_source: &str,
174
0
        lhs_debug: &dyn fmt::Debug,
175
0
        rhs: &Rhs,
176
0
        rhs_source: &str,
177
0
        rhs_debug: &dyn fmt::Debug,
178
0
        f: &mut fmt::Formatter,
179
0
    ) -> fmt::Result {
180
0
        _ = (lhs, rhs, cmp);
181
0
        display_cmp_impl("<", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
182
0
    }
183
}
184
185
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Gt, Lhs, Rhs> for Gt {
186
    type Error = GtError;
187
}
188
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Gt, Lhs, Rhs> for GtError {
189
0
    fn fmt(
190
0
        &self,
191
0
        cmp: &Gt,
192
0
        lhs: &Lhs,
193
0
        lhs_source: &str,
194
0
        lhs_debug: &dyn fmt::Debug,
195
0
        rhs: &Rhs,
196
0
        rhs_source: &str,
197
0
        rhs_debug: &dyn fmt::Debug,
198
0
        f: &mut fmt::Formatter,
199
0
    ) -> fmt::Result {
200
0
        _ = (lhs, rhs, cmp);
201
0
        display_cmp_impl(">", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
202
0
    }
203
}
204
205
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Le, Lhs, Rhs> for Le {
206
    type Error = LeError;
207
}
208
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Le, Lhs, Rhs> for LeError {
209
0
    fn fmt(
210
0
        &self,
211
0
        cmp: &Le,
212
0
        lhs: &Lhs,
213
0
        lhs_source: &str,
214
0
        lhs_debug: &dyn fmt::Debug,
215
0
        rhs: &Rhs,
216
0
        rhs_source: &str,
217
0
        rhs_debug: &dyn fmt::Debug,
218
0
        f: &mut fmt::Formatter,
219
0
    ) -> fmt::Result {
220
0
        _ = (lhs, rhs, cmp);
221
0
        display_cmp_impl("<=", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
222
0
    }
Unexecuted instantiation: <equator::LeError as equator::CmpDisplay<equator::Le, dyn core::fmt::Debug, dyn core::fmt::Debug>>::fmt
Unexecuted instantiation: <equator::LeError as equator::CmpDisplay<equator::Le, _, _>>::fmt
223
}
224
225
impl<Lhs: ?Sized, Rhs: ?Sized> CmpError<Ge, Lhs, Rhs> for Ge {
226
    type Error = GeError;
227
}
228
impl<Lhs: ?Sized, Rhs: ?Sized> CmpDisplay<Ge, Lhs, Rhs> for GeError {
229
0
    fn fmt(
230
0
        &self,
231
0
        cmp: &Ge,
232
0
        lhs: &Lhs,
233
0
        lhs_source: &str,
234
0
        lhs_debug: &dyn fmt::Debug,
235
0
        rhs: &Rhs,
236
0
        rhs_source: &str,
237
0
        rhs_debug: &dyn fmt::Debug,
238
0
        f: &mut fmt::Formatter,
239
0
    ) -> fmt::Result {
240
0
        _ = (lhs, rhs, cmp);
241
0
        display_cmp_impl(">=", lhs_debug, lhs_source, rhs_debug, rhs_source, f)
242
0
    }
243
}
244
245
impl<Rhs: ?Sized, Lhs: ?Sized + PartialEq<Rhs>> Cmp<Lhs, Rhs> for Eq {
246
    #[inline(always)]
247
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), EqError> {
248
0
        if *lhs == *rhs {
249
0
            Ok(())
250
        } else {
251
0
            Err(EqError)
252
        }
253
0
    }
254
}
255
impl<Rhs: ?Sized, Lhs: ?Sized + PartialEq<Rhs>> Cmp<Lhs, Rhs> for Ne {
256
    #[inline(always)]
257
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), NeError> {
258
0
        if *lhs != *rhs {
259
0
            Ok(())
260
        } else {
261
0
            Err(NeError)
262
        }
263
0
    }
264
}
265
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Le {
266
    #[inline(always)]
267
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), LeError> {
268
0
        if *lhs <= *rhs {
269
0
            Ok(())
270
        } else {
271
0
            Err(LeError)
272
        }
273
0
    }
Unexecuted instantiation: <equator::Le as equator::Cmp<usize, usize>>::test
Unexecuted instantiation: <equator::Le as equator::Cmp<_, _>>::test
274
}
275
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Ge {
276
    #[inline(always)]
277
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), GeError> {
278
0
        if *lhs >= *rhs {
279
0
            Ok(())
280
        } else {
281
0
            Err(GeError)
282
        }
283
0
    }
284
}
285
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Lt {
286
    #[inline(always)]
287
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), LtError> {
288
0
        if *lhs < *rhs {
289
0
            Ok(())
290
        } else {
291
0
            Err(LtError)
292
        }
293
0
    }
294
}
295
impl<Rhs: ?Sized, Lhs: ?Sized + PartialOrd<Rhs>> Cmp<Lhs, Rhs> for Gt {
296
    #[inline(always)]
297
0
    fn test(&self, lhs: &Lhs, rhs: &Rhs) -> Result<(), GtError> {
298
0
        if *lhs > *rhs {
299
0
            Ok(())
300
        } else {
301
0
            Err(GtError)
302
        }
303
0
    }
304
}
305
306
#[doc(hidden)]
307
pub struct CmpExpr;
308
#[doc(hidden)]
309
pub struct CustomCmpExpr<E>(pub core::marker::PhantomData<E>);
310
#[doc(hidden)]
311
pub struct AndExpr<L, R>(pub L, pub R);
312
#[doc(hidden)]
313
pub struct OrExpr<L, R>(pub L, pub R);
314
315
#[doc(hidden)]
316
pub struct Message<'a>(pub core::fmt::Arguments<'a>);
317
#[doc(hidden)]
318
pub struct NoMessage;
319
320
impl From<NoMessage> for core::fmt::Arguments<'_> {
321
0
    fn from(_: NoMessage) -> Self {
322
0
        core::format_args!("")
323
0
    }
324
}
325
326
impl<'a> From<Message<'a>> for core::fmt::Arguments<'a> {
327
0
    fn from(t: Message<'a>) -> Self {
328
0
        t.0
329
0
    }
330
}
331
332
#[cold]
333
#[inline(never)]
334
#[doc(hidden)]
335
#[track_caller]
336
0
pub fn panic_failed_assert<'a, M: Into<core::fmt::Arguments<'a>>, D: decompose::Recompose>(
337
0
    __marker: core::marker::PhantomData<D>,
338
0
    debug_lhs: D::DebugLhs,
339
0
    debug_rhs: D::DebugRhs,
340
0
    debug_cmp: D::DebugCmp,
341
0
    source: &'static structures::WithSource<D::Source, &'static D::VTable>,
342
0
    message: M,
343
0
) -> ! {
344
0
    panic!(
345
0
        "{:#?}",
346
0
        structures::DebugMessage::<D> {
347
0
            source,
348
0
            debug_lhs,
349
0
            debug_rhs,
350
0
            debug_cmp,
351
0
            message: message.into(),
352
0
        }
353
0
    )
Unexecuted instantiation: equator::panic_failed_assert::<equator::Message, equator::CmpExpr>
Unexecuted instantiation: equator::panic_failed_assert::<equator::Message, bool>
Unexecuted instantiation: equator::panic_failed_assert::<_, _>
354
}
355
356
#[cfg(test)]
357
mod tests {
358
    use super::*;
359
360
    #[test]
361
    #[should_panic]
362
    fn test_assert() {
363
        assert!(false);
364
    }
365
366
    #[cfg(debug_assertions)]
367
    #[test]
368
    #[should_panic]
369
    fn test_debug_assert() {
370
        debug_assert!(false);
371
    }
372
}