/rust/registry/src/index.crates.io-1949cf8c6b5b557f/equator-0.4.2/src/decompose.rs
Line | Count | Source |
1 | | use crate::{ |
2 | | expr, |
3 | | structures::{DebugMessage, DebugMessageImpl}, |
4 | | traits::Eval, |
5 | | CmpDisplay, |
6 | | }; |
7 | | use core::fmt; |
8 | | |
9 | | pub type PtrToDeref = unsafe fn(*const *const ()) -> *const (); |
10 | | pub type PtrToCmp = unsafe fn(out: *mut (), cmp: *const (), lhs: *const (), rhs: *const ()); |
11 | | pub type PtrToDebug = unsafe fn(*const ()) -> &'static dyn fmt::Debug; |
12 | | pub type PtrToDisplay = |
13 | | unsafe fn(*const ()) -> &'static dyn CmpDisplay<*const (), dyn fmt::Debug, dyn fmt::Debug>; |
14 | | |
15 | | pub trait Decompose { |
16 | | type Decomposed: Recompose; |
17 | | } |
18 | | |
19 | | pub trait Recompose: Sized { |
20 | | type Result: Eval; |
21 | | type Source; |
22 | | type VTable: 'static; |
23 | | type DebugLhs: Copy + fmt::Debug; |
24 | | type DebugRhs: Copy + fmt::Debug; |
25 | | type DebugCmp: Copy + fmt::Debug; |
26 | | |
27 | | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result; |
28 | | fn eval_impl( |
29 | | debug_lhs: &Self::DebugLhs, |
30 | | debug_rhs: &Self::DebugRhs, |
31 | | debug_cmp: Self::DebugCmp, |
32 | | vtable: &Self::VTable, |
33 | | ) -> Self::Result; |
34 | | |
35 | 0 | fn debug_final(full: &DebugMessage<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
36 | 0 | let result = &Self::eval_impl( |
37 | 0 | &full.debug_lhs, |
38 | 0 | &full.debug_rhs, |
39 | 0 | full.debug_cmp, |
40 | 0 | &full.source.vtable, |
41 | 0 | ); |
42 | | |
43 | 0 | let message = full.message; |
44 | 0 | let inner = DebugMessageImpl::<'_, Self> { |
45 | 0 | result, |
46 | 0 | source: &full.source.source, |
47 | 0 | debug_lhs: &full.debug_lhs, |
48 | 0 | debug_rhs: &full.debug_rhs, |
49 | 0 | debug_cmp: full.debug_cmp, |
50 | 0 | vtable: full.source.vtable, |
51 | 0 | }; |
52 | 0 | write!( |
53 | 0 | f, |
54 | 0 | "Assertion failed at {}:{}:{}\n", |
55 | | full.source.file, full.source.line, full.source.col |
56 | 0 | )?; |
57 | 0 | if message.as_str() != Some("") { |
58 | 0 | write!(f, "{message:#?}\n")?; |
59 | 0 | } |
60 | 0 | Self::debug_impl(&inner, f) |
61 | 0 | } Unexecuted instantiation: <bool as equator::decompose::Recompose>::debug_final Unexecuted instantiation: <equator::CmpExpr as equator::decompose::Recompose>::debug_final Unexecuted instantiation: <_ as equator::decompose::Recompose>::debug_final |
62 | | } |
63 | | |
64 | | impl Recompose for bool { |
65 | | type Result = Result<(), ()>; |
66 | | type Source = &'static str; |
67 | | type VTable = (); |
68 | | type DebugLhs = (); |
69 | | type DebugRhs = (); |
70 | | type DebugCmp = bool; |
71 | | |
72 | 0 | fn eval_impl( |
73 | 0 | _: &Self::DebugLhs, |
74 | 0 | _: &Self::DebugRhs, |
75 | 0 | debug_cmp: Self::DebugCmp, |
76 | 0 | _: &Self::VTable, |
77 | 0 | ) -> Self::Result { |
78 | 0 | if debug_cmp { |
79 | 0 | Ok(()) |
80 | | } else { |
81 | 0 | Err(()) |
82 | | } |
83 | 0 | } |
84 | | |
85 | 0 | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
86 | 0 | let source = *message.source; |
87 | 0 | let result = message.result.is_ok(); |
88 | 0 | write!(f, "Assertion failed: {source}\n")?; |
89 | 0 | write!(f, "- {source} = {result:#?}") |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl Recompose for crate::CmpExpr { |
94 | | type Result = Result<(), ()>; |
95 | | type Source = expr::CmpExpr<(), &'static str, &'static str>; |
96 | | type VTable = |
97 | | expr::CmpExpr<(PtrToDisplay, PtrToCmp), (PtrToDebug, PtrToDeref), (PtrToDebug, PtrToDeref)>; |
98 | | type DebugLhs = *const (); |
99 | | type DebugRhs = *const (); |
100 | | type DebugCmp = (); |
101 | | |
102 | 0 | fn eval_impl( |
103 | 0 | debug_lhs: &Self::DebugLhs, |
104 | 0 | debug_rhs: &Self::DebugRhs, |
105 | 0 | _: Self::DebugCmp, |
106 | 0 | vtable: &Self::VTable, |
107 | 0 | ) -> Self::Result { |
108 | 0 | let debug_lhs = unsafe { (vtable.lhs.1)(debug_lhs) }; |
109 | 0 | let debug_rhs = unsafe { (vtable.rhs.1)(debug_rhs) }; |
110 | 0 | let mut result = core::mem::MaybeUninit::<Self::Result>::uninit(); |
111 | | unsafe { |
112 | 0 | (vtable.cmp.1)( |
113 | 0 | (&mut result) as *mut core::mem::MaybeUninit<Self::Result> as *mut (), |
114 | 0 | core::ptr::NonNull::<()>::dangling().as_ptr(), |
115 | 0 | debug_lhs, |
116 | 0 | debug_rhs, |
117 | 0 | ) |
118 | | } |
119 | 0 | unsafe { result.assume_init() } |
120 | 0 | } |
121 | | |
122 | 0 | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
123 | 0 | let lhs_source = message.source.lhs; |
124 | 0 | let rhs_source = message.source.rhs; |
125 | 0 | let debug_lhs = unsafe { (message.vtable.lhs.1)(message.debug_lhs) }; |
126 | 0 | let debug_rhs = unsafe { (message.vtable.rhs.1)(message.debug_rhs) }; |
127 | | |
128 | 0 | let lhs = unsafe { (message.vtable.lhs.0)(debug_lhs) }; |
129 | 0 | let rhs = unsafe { (message.vtable.rhs.0)(debug_rhs) }; |
130 | | |
131 | 0 | let err = |
132 | 0 | unsafe { (message.vtable.cmp.0)(message.result.as_ref().unwrap_err() as *const ()) }; |
133 | 0 | err.fmt( |
134 | 0 | &(core::ptr::NonNull::<()>::dangling().as_ptr() as *const ()), |
135 | 0 | lhs, |
136 | 0 | lhs_source, |
137 | 0 | lhs, |
138 | 0 | rhs, |
139 | 0 | rhs_source, |
140 | 0 | rhs, |
141 | 0 | f, |
142 | | ) |
143 | 0 | } |
144 | | } |
145 | | |
146 | | impl<E> Recompose for crate::CustomCmpExpr<E> { |
147 | | type Result = Result<(), E>; |
148 | | type Source = expr::CustomCmpExpr<(), &'static str, &'static str>; |
149 | | type VTable = expr::CustomCmpExpr< |
150 | | (PtrToDisplay, PtrToCmp), |
151 | | (PtrToDebug, PtrToDeref), |
152 | | (PtrToDebug, PtrToDeref), |
153 | | >; |
154 | | type DebugLhs = *const (); |
155 | | type DebugRhs = *const (); |
156 | | type DebugCmp = *const (); |
157 | | |
158 | 0 | fn eval_impl( |
159 | 0 | debug_lhs: &Self::DebugLhs, |
160 | 0 | debug_rhs: &Self::DebugRhs, |
161 | 0 | debug_cmp: Self::DebugCmp, |
162 | 0 | vtable: &Self::VTable, |
163 | 0 | ) -> Self::Result { |
164 | 0 | let debug_lhs = unsafe { (vtable.lhs.1)(debug_lhs) }; |
165 | 0 | let debug_rhs = unsafe { (vtable.rhs.1)(debug_rhs) }; |
166 | | |
167 | 0 | let mut result = core::mem::MaybeUninit::<Self::Result>::uninit(); |
168 | | unsafe { |
169 | 0 | (vtable.cmp.1)( |
170 | 0 | (&mut result) as *mut core::mem::MaybeUninit<Self::Result> as *mut (), |
171 | 0 | debug_cmp, |
172 | 0 | debug_lhs, |
173 | 0 | debug_rhs, |
174 | 0 | ) |
175 | | } |
176 | 0 | unsafe { result.assume_init() } |
177 | 0 | } |
178 | | |
179 | 0 | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
180 | 0 | let lhs_source = message.source.lhs; |
181 | 0 | let rhs_source = message.source.rhs; |
182 | 0 | let debug_lhs = unsafe { (message.vtable.lhs.1)(message.debug_lhs) }; |
183 | 0 | let debug_rhs = unsafe { (message.vtable.rhs.1)(message.debug_rhs) }; |
184 | | |
185 | 0 | let lhs = unsafe { (message.vtable.lhs.0)(debug_lhs) }; |
186 | 0 | let rhs = unsafe { (message.vtable.rhs.0)(debug_rhs) }; |
187 | | |
188 | 0 | let err = unsafe { |
189 | 0 | (message.vtable.cmp.0)(message.result.as_ref().unwrap_err() as *const E as *const ()) |
190 | | }; |
191 | 0 | err.fmt( |
192 | 0 | &message.debug_cmp, |
193 | 0 | lhs, |
194 | 0 | lhs_source, |
195 | 0 | lhs, |
196 | 0 | rhs, |
197 | 0 | rhs_source, |
198 | 0 | rhs, |
199 | 0 | f, |
200 | | ) |
201 | 0 | } |
202 | | } |
203 | | |
204 | | impl<L: Recompose, R: Recompose> Recompose for crate::AndExpr<L, R> { |
205 | | type Result = expr::AndExpr<L::Result, R::Result>; |
206 | | type Source = expr::AndExpr<L::Source, R::Source>; |
207 | | type VTable = expr::AndExpr<&'static L::VTable, &'static R::VTable>; |
208 | | type DebugCmp = expr::AndExpr<L::DebugCmp, R::DebugCmp>; |
209 | | type DebugLhs = expr::AndExpr<L::DebugLhs, R::DebugLhs>; |
210 | | type DebugRhs = expr::AndExpr<L::DebugRhs, R::DebugRhs>; |
211 | | |
212 | 0 | fn eval_impl( |
213 | 0 | debug_lhs: &Self::DebugLhs, |
214 | 0 | debug_rhs: &Self::DebugRhs, |
215 | 0 | debug_cmp: Self::DebugCmp, |
216 | 0 | vtable: &Self::VTable, |
217 | 0 | ) -> Self::Result { |
218 | 0 | let lhs = L::eval_impl(&debug_lhs.lhs, &debug_rhs.lhs, debug_cmp.lhs, vtable.lhs); |
219 | 0 | let rhs = R::eval_impl(&debug_lhs.rhs, &debug_rhs.rhs, debug_cmp.rhs, vtable.rhs); |
220 | 0 | expr::AndExpr { lhs, rhs } |
221 | 0 | } |
222 | | |
223 | 0 | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
224 | 0 | let lhs = DebugMessageImpl::<'_, L> { |
225 | 0 | result: &message.result.lhs, |
226 | 0 | source: &message.source.lhs, |
227 | 0 | vtable: message.vtable.lhs, |
228 | 0 | debug_lhs: &message.debug_lhs.lhs, |
229 | 0 | debug_rhs: &message.debug_rhs.lhs, |
230 | 0 | debug_cmp: message.debug_cmp.lhs, |
231 | 0 | }; |
232 | 0 | let rhs = DebugMessageImpl::<'_, R> { |
233 | 0 | result: &message.result.rhs, |
234 | 0 | source: &message.source.rhs, |
235 | 0 | vtable: message.vtable.rhs, |
236 | 0 | debug_lhs: &message.debug_lhs.rhs, |
237 | 0 | debug_rhs: &message.debug_rhs.rhs, |
238 | 0 | debug_cmp: message.debug_cmp.rhs, |
239 | 0 | }; |
240 | | |
241 | 0 | let lhs_eval = lhs.result.eval(); |
242 | 0 | let rhs_eval = rhs.result.eval(); |
243 | 0 | if !(lhs_eval && rhs_eval) { |
244 | 0 | if !lhs_eval { |
245 | 0 | L::debug_impl(&lhs, f)?; |
246 | 0 | if !rhs_eval { |
247 | 0 | f.write_str("\n")?; |
248 | 0 | } |
249 | 0 | } |
250 | 0 | if !rhs_eval { |
251 | 0 | R::debug_impl(&rhs, f)?; |
252 | 0 | } |
253 | 0 | } |
254 | 0 | Ok(()) |
255 | 0 | } |
256 | | } |
257 | | |
258 | | impl<L: Recompose, R: Recompose> Recompose for crate::OrExpr<L, R> { |
259 | | type Result = expr::OrExpr<L::Result, R::Result>; |
260 | | type Source = expr::OrExpr<L::Source, R::Source>; |
261 | | type VTable = expr::OrExpr<&'static L::VTable, &'static R::VTable>; |
262 | | type DebugCmp = expr::OrExpr<L::DebugCmp, R::DebugCmp>; |
263 | | type DebugLhs = expr::OrExpr<L::DebugLhs, R::DebugLhs>; |
264 | | type DebugRhs = expr::OrExpr<L::DebugRhs, R::DebugRhs>; |
265 | | |
266 | 0 | fn eval_impl( |
267 | 0 | debug_lhs: &Self::DebugLhs, |
268 | 0 | debug_rhs: &Self::DebugRhs, |
269 | 0 | debug_cmp: Self::DebugCmp, |
270 | 0 | vtable: &Self::VTable, |
271 | 0 | ) -> Self::Result { |
272 | 0 | let lhs = L::eval_impl(&debug_lhs.lhs, &debug_rhs.lhs, debug_cmp.lhs, vtable.lhs); |
273 | 0 | let rhs = R::eval_impl(&debug_lhs.rhs, &debug_rhs.rhs, debug_cmp.rhs, vtable.rhs); |
274 | 0 | expr::OrExpr { lhs, rhs } |
275 | 0 | } |
276 | | |
277 | 0 | fn debug_impl(message: &DebugMessageImpl<'_, Self>, f: &mut fmt::Formatter) -> fmt::Result { |
278 | 0 | let lhs = DebugMessageImpl::<'_, L> { |
279 | 0 | result: &message.result.lhs, |
280 | 0 | source: &message.source.lhs, |
281 | 0 | vtable: message.vtable.lhs, |
282 | 0 | debug_lhs: &message.debug_lhs.lhs, |
283 | 0 | debug_rhs: &message.debug_rhs.lhs, |
284 | 0 | debug_cmp: message.debug_cmp.lhs, |
285 | 0 | }; |
286 | 0 | let rhs = DebugMessageImpl::<'_, R> { |
287 | 0 | result: &message.result.rhs, |
288 | 0 | source: &message.source.rhs, |
289 | 0 | vtable: message.vtable.rhs, |
290 | 0 | debug_lhs: &message.debug_lhs.rhs, |
291 | 0 | debug_rhs: &message.debug_rhs.rhs, |
292 | 0 | debug_cmp: message.debug_cmp.rhs, |
293 | 0 | }; |
294 | | |
295 | 0 | let lhs_eval = lhs.result.eval(); |
296 | 0 | let rhs_eval = rhs.result.eval(); |
297 | 0 | if !(lhs_eval || rhs_eval) { |
298 | 0 | if !lhs_eval { |
299 | 0 | L::debug_impl(&lhs, f)?; |
300 | 0 | if !rhs_eval { |
301 | 0 | f.write_str("\n")?; |
302 | 0 | } |
303 | 0 | } |
304 | 0 | if !rhs_eval { |
305 | 0 | R::debug_impl(&rhs, f)?; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | Ok(()) |
309 | 0 | } |
310 | | } |
311 | | |
312 | | impl Decompose for &'static str { |
313 | | type Decomposed = bool; |
314 | | } |
315 | | impl Decompose for expr::CmpExpr<(), &'static str, &'static str> { |
316 | | type Decomposed = crate::CmpExpr; |
317 | | } |
318 | | impl Decompose for expr::CustomCmpExpr<(), &'static str, &'static str> { |
319 | | type Decomposed = crate::CustomCmpExpr<()>; |
320 | | } |
321 | | impl<L: Decompose, R: Decompose> Decompose for expr::AndExpr<L, R> { |
322 | | type Decomposed = crate::AndExpr<L::Decomposed, R::Decomposed>; |
323 | | } |
324 | | impl<L: Decompose, R: Decompose> Decompose for expr::OrExpr<L, R> { |
325 | | type Decomposed = crate::OrExpr<L::Decomposed, R::Decomposed>; |
326 | | } |