erratic 0.10.1

Handling errors in an efficient way.
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
use alloc::{boxed::Box, format};
use core::{
    any::TypeId,
    convert::Infallible,
    error::{self, Error},
    fmt::{self, Debug, Display},
    mem::{self, ManuallyDrop, MaybeUninit},
    ptr::NonNull,
    result,
};

use crate::{
    backtrace::WithBacktrace,
    context, match_else,
    nae::Nae,
    payload,
    ptr::{Align4, Align4Own, Align4PtrCompat, Align4Ref, Metadata, Mut, Ref},
    render, rtti,
};

/// Triple-state error storage.
///
/// # Safety invariants
///
/// All three union variants share the invariant that the first byte's lowest 2 bits
/// encode the discriminant:
///   - `00`: [`const_body`](RawError::const_body) (static string literal)
///   - `01`: [`boxed_body`](RawError::boxed_body) (heap-allocated [`DynBody`])
///   - `10`: [`inline_body`](RawError::inline_body) (inline [`Align4PtrCompat`])
///
/// The discriminant is written at construction and must never be modified afterward.
#[repr(C)]
pub union RawError<S>
where
    S: 'static,
{
    const_body: ManuallyDrop<Align4Ref<'static, ConstBody>>,
    boxed_body: ManuallyDrop<Align4Own<DynBody>>,
    inline_body: ManuallyDrop<Align4PtrCompat<S>>,
}

enum SelectRef<'a, S>
where
    S: 'static,
{
    Const(&'a Align4Ref<'static, ConstBody>),
    Boxed(&'a Align4Own<DynBody>),
    Inline(&'a Align4PtrCompat<S>),
}

enum SelectMut<'a, S>
where
    S: 'static,
{
    Const(&'a mut Align4Ref<'static, ConstBody>),
    Boxed(&'a mut Align4Own<DynBody>),
    Inline(&'a mut Align4PtrCompat<S>),
}

enum SelectOwn<S>
where
    S: 'static,
{
    Const(Align4Ref<'static, ConstBody>),
    Boxed(ManuallyDrop<Align4Own<DynBody>>),
    Inline(Align4PtrCompat<S>),
}

impl<S> RawError<S> {
    const KIND_CONST: Metadata = Metadata::_0;
    const KIND_BOXED: Metadata = Metadata::_1;
    const KIND_INLINE: Metadata = Metadata::_2;

    /// Reads the 2-bit discriminant from the first byte of the union.
    fn kind(&self) -> Metadata {
        // # Safety: All three union variants have `repr(C)` layout, and their first field
        // is `Align4Ptr`/`Align4PtrCompat`, all of which store the metadata at offset 0.
        unsafe { Metadata((&raw const (*self) as *const u8).read() & Metadata::MASK) }
    }

    /// Selects a shared reference to the active union variant.
    fn select_ref(&self) -> SelectRef<'_, S> {
        // # Safety: `RawError::kind` always returns a valid discriminant.
        unsafe {
            match self.kind() {
                Self::KIND_CONST => SelectRef::Const(&self.const_body),
                Self::KIND_BOXED => SelectRef::Boxed(&self.boxed_body),
                Self::KIND_INLINE => SelectRef::Inline(&self.inline_body),
                _ => unreachable!(),
            }
        }
    }

    /// Selects a mutable reference to the active union variant.
    fn select_mut(&mut self) -> SelectMut<'_, S> {
        // # Safety: `RawError::kind` always returns a valid discriminant.
        unsafe {
            match self.kind() {
                Self::KIND_CONST => SelectMut::Const(&mut self.const_body),
                Self::KIND_BOXED => SelectMut::Boxed(&mut self.boxed_body),
                Self::KIND_INLINE => SelectMut::Inline(&mut self.inline_body),
                _ => unreachable!(),
            }
        }
    }

    /// Takes ownership of the active union variant.
    fn select_own(self) -> SelectOwn<S> {
        let kind = self.kind();
        let mut this = ManuallyDrop::new(self);

        // # Safety: `RawError::kind` always returns a valid discriminant.
        unsafe {
            match kind {
                Self::KIND_CONST => SelectOwn::Const(ManuallyDrop::take(&mut this.const_body)),
                Self::KIND_BOXED => {
                    SelectOwn::Boxed(ManuallyDrop::new(ManuallyDrop::take(&mut this.boxed_body)))
                }
                Self::KIND_INLINE => SelectOwn::Inline(ManuallyDrop::take(&mut this.inline_body)),
                _ => unreachable!(),
            }
        }
    }
}

impl RawError<Infallible> {
    /// Constructs a const-variant [`RawError`] from a typed literal.
    pub fn new_const<L>() -> Self
    where
        L: context::Context + ?Sized,
    {
        #[cfg(feature = "backtrace")]
        if let Ok(source) = WithBacktrace::try_attach(Nae::new()) {
            return RawError::<Infallible>::new_boxed_::<_, _, L>(
                None,
                source,
                payload::Empty::new(),
            );
        }

        // Note: Relies on const promotion to produce a new constant.
        let body: &'static Align4<ConstBody> = &const {
            Align4(ConstBody {
                context: L::LITERAL,
            })
        };
        Self {
            const_body: ManuallyDrop::new(Align4Ref::new(body, Self::KIND_CONST)),
        }
    }

    /// Converts to a state-tagged error without storing any runtime state.
    pub fn with_phantom_state<S>(self) -> RawError<S>
    where
        S: 'static,
    {
        match self.select_own() {
            SelectOwn::Const(body) => RawError {
                const_body: ManuallyDrop::new(body),
            },
            SelectOwn::Inline(_body) => unreachable!(),
            SelectOwn::Boxed(body) => RawError { boxed_body: body },
        }
    }
}

impl<S> RawError<S> {
    /// Constructs an inline-variant [`RawError`] with `state` stored directly.
    pub fn try_new_inline(state: S) -> result::Result<Self, S>
    where
        S: Debug + Send + Sync + 'static,
    {
        #[cfg(feature = "backtrace")]
        if let Ok(source) = WithBacktrace::try_attach(Nae::new()) {
            return Ok(Self::new_boxed_::<_, _, context::Blank>(
                Some(state),
                source,
                payload::Empty::new(),
            ));
        }

        Ok(Self {
            inline_body: ManuallyDrop::new(Align4PtrCompat::new(Self::KIND_INLINE, state)?),
        })
    }

    /// Constructs a [RawError], storing the given state inline when the state fits within
    /// the inline storage.
    pub fn new_inline_or_boxed(state: S) -> Self
    where
        S: Debug + Send + Sync + 'static,
    {
        let Err(state) = match_else!(Self::try_new_inline(state),
            Ok(this) => return this,
        );
        Self::new_boxed::<Nae, payload::Empty, context::Blank>(
            Some(state),
            Nae::new(),
            payload::Empty::new(),
        )
    }

    /// Constructs a boxed-variant [`RawError`] containing source, payload, and context.
    ///
    /// The source, payload, and context are packed into a single heap allocation
    /// alongside a vtable for type-erased access.
    pub fn new_boxed<E, P, L>(state: Option<S>, source: E, payload: P) -> Self
    where
        S: Debug + Send + Sync + 'static,
        E: error::Error + Send + Sync + 'static,
        P: Display + Send + Sync + 'static,
        L: context::Context + ?Sized,
    {
        match WithBacktrace::try_attach(source) {
            Ok(source) => Self::new_boxed_::<_, _, L>(state, source, payload),
            Err(source) => Self::new_boxed_::<_, _, L>(state, source, payload),
        }
    }

    fn new_boxed_<E, P, L>(state: Option<S>, source: E, payload: P) -> Self
    where
        S: Debug + Send + Sync + 'static,
        E: error::Error + Send + Sync + 'static,
        P: Display + Send + Sync + 'static,
        L: context::Context + ?Sized,
    {
        let (vtable, state) = DynBody::<S, E, P, L::Repr>::vtable_from_state(state);

        // # Safety
        //
        // The `Align4Own` pointer is cast to `DynBody<Infallible, (), (), ()>` for uniform storage.
        // This is valid because all monomorphizations of `DynBody<S, E, P, L::Repr>` share
        // the same vtable pointer, and the concrete `S`, `E`, `P`, `C` are erased.
        // The cast only changes the type parameter defaults — it does not violate the layout
        // because `()` is a ZST.
        // Due to `Align4Own`assumes a correct layout, and that's not true after the cast,
        // we need to put it in a `ManuallyDrop` and drop it via vtable instead.
        Self {
            boxed_body: unsafe {
                ManuallyDrop::new(
                    Align4Own::from_boxed(
                        Box::new(Align4(DynBody::<S, E, P, L::Repr> {
                            vtable,
                            state,
                            source,
                            payload,
                            context: L::new_context(),
                        })),
                        RawError::<S>::KIND_BOXED,
                    )
                    .cast::<DynBody>(),
                )
            },
        }
    }

    /// Returns a reference to the displayable context.
    pub fn context(&self) -> Option<&'_ (dyn Display + Send + Sync + 'static)> {
        match self.select_ref() {
            // Safety: Projection from `ConstBody` to `ConstBody::context` is safe.
            SelectRef::Const(body) => unsafe {
                Some(
                    body.borrow()
                        .project(|body| &raw const (*body).context)
                        .deref(),
                )
            },
            SelectRef::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                // Safety: The body pointer is confirmed valid.
                (vtable.context)(body.borrow())
            },
            SelectRef::Inline(_body) => None,
        }
    }

    /// Returns a reference to the displayable payload, if present.
    pub fn payload(&self) -> Option<&'_ (dyn Display + Send + Sync + 'static)> {
        match self.select_ref() {
            SelectRef::Inline(_body) => None,
            SelectRef::Const(_body) => None,
            SelectRef::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                // Safety: The body pointer is confirmed valid.
                (vtable.payload)(body.borrow())
            },
        }
    }

    /// Returns a reference to the wrapped source error, if present.
    pub fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self.select_ref() {
            SelectRef::Const(_body) => None,
            SelectRef::Inline(_body) => None,
            SelectRef::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                // Safety: The body pointer is confirmed valid.
                (vtable.source)(body.borrow())
            },
        }
    }

    /// Attempts to downcast the stored source error to `E`.
    ///
    /// Returns `None` if the source is not of type `E` or does not exist.
    pub fn downcast_source_ref<E>(&self) -> Option<&E>
    where
        E: error::Error + 'static,
    {
        self.source()?.downcast_ref::<E>()
    }

    /// Attempts to downcast the stored payload to `P`.
    pub fn downcast_payload_ref<P>(&self) -> Option<&P>
    where
        P: 'static,
    {
        match self.select_ref() {
            SelectRef::Const(_body) => None,
            SelectRef::Inline(_body) => None,
            SelectRef::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                let mut result = None::<&P>;
                // Safety: The body pointer is confirmed valid.
                (vtable.downcast_payload_ref)(
                    body.borrow(),
                    TypeId::of::<P>(),
                    NonNull::from_mut(&mut result).cast(),
                );
                result
            },
        }
    }

    /// Attempts to downcast the stored payload to `P` by mutable reference.
    pub fn downcast_payload_mut<P>(&mut self) -> Option<&mut P>
    where
        P: 'static,
    {
        match self.select_mut() {
            SelectMut::Const(_body) => None,
            SelectMut::Inline(_body) => None,
            SelectMut::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                let mut result = None::<&mut P>;
                // Safety: The body pointer is confirmed valid.
                (vtable.downcast_payload_mut)(
                    body.borrow_mut(),
                    TypeId::of::<P>(),
                    NonNull::from_mut(&mut result).cast(),
                );
                result
            },
        }
    }

    /// Returns a shared reference to the stored state.
    pub fn state(&self) -> Option<&S> {
        match self.select_ref() {
            SelectRef::Const(_body) => None,
            SelectRef::Inline(_body) => unsafe {
                // Safety: Access `InlineBody::value` is safe.
                Some(self.inline_body.borrow_value())
            },
            SelectRef::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                let mut state = None::<&S>;
                // Safety: The body and state pointers are confirmed valid.
                (vtable.state)(
                    body.borrow(),
                    TypeId::of::<S>(),
                    NonNull::from_mut(&mut state).cast(),
                );

                state
            },
        }
    }

    /// Consumes `self` and returns the stored state.
    pub fn into_state(self) -> Option<S> {
        match self.select_own() {
            SelectOwn::Const(_body) => None,
            SelectOwn::Inline(body) => Some(body.into_value()),
            SelectOwn::Boxed(body) => {
                unsafe {
                    let vtable = DynBody::vtable(body.borrow());
                    let mut state = None::<S>;

                    // Safety: The body and state pointers are confirmed valid.
                    (vtable.into_state)(
                        body,
                        TypeId::of::<S>(),
                        NonNull::from_mut(&mut state).cast(),
                    );
                    state
                }
            }
        }
    }

    /// Consumes `self` and returns the boxed source error, if any.
    pub fn into_source(self) -> Option<Box<dyn error::Error + Send + Sync + 'static>> {
        match self.select_own() {
            SelectOwn::Const(_body) => None,
            SelectOwn::Inline(_body) => None,
            SelectOwn::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                // Safety: The body pointer is confirmed valid.
                (vtable.into_source)(body)
            },
        }
    }

    /// Consumes `self` and extracts the source error and payload by type.
    ///
    /// Returns `None` if the types do not match or no source/payload exists.
    ///
    /// # Existence Guarantee
    /// It's guaranteed that at least one components exist.
    pub fn into_parts<P, E>(self) -> (Option<S>, Option<&'static str>, Option<P>, Option<E>)
    where
        E: 'static,
        P: 'static,
    {
        match self.select_own() {
            SelectOwn::Const(body) => (
                None,
                Some(
                    // Safety: The project to context is inbound.
                    unsafe { body.borrow().project(|v| &raw const (*v).context).copied() },
                ),
                None,
                None,
            ),
            SelectOwn::Inline(body) => (Some(body.into_value()), None, None, None),
            SelectOwn::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                let mut state: Option<S> = None;
                let mut context: Option<&'static str> = None;
                let mut payload: Option<P> = None;
                let mut err: Option<E> = None;

                // Safety: The body, state, context, payload, and error pointers are confirmed valid.
                (vtable.into_parts)(
                    body,
                    TypeId::of::<E>(),
                    NonNull::from_mut(&mut err).cast(),
                    TypeId::of::<P>(),
                    NonNull::from_mut(&mut payload).cast(),
                    NonNull::from_mut(&mut context).cast(),
                    TypeId::of::<S>(),
                    NonNull::from_mut(&mut state).cast(),
                );

                (state, context, payload, err)
            },
        }
    }

    pub fn extract_state(
        self,
    ) -> result::Result<(S, Option<RawError<Infallible>>), RawError<Infallible>> {
        match self.select_own() {
            SelectOwn::Const(body) => Err(RawError {
                const_body: ManuallyDrop::new(body),
            }),
            SelectOwn::Inline(body) => Ok((body.into_value(), None)),
            SelectOwn::Boxed(body) => {
                unsafe {
                    let vtable = DynBody::vtable(body.borrow());
                    let mut state_dst = None::<S>;
                    let mut error_dst = None::<ManuallyDrop<Align4Own<DynBody>>>;
                    // Safety: The body, state, error pointers are confirmed valid.
                    (vtable.extract_state)(
                        body,
                        TypeId::of::<S>(),
                        NonNull::from_mut(&mut state_dst).cast(),
                        NonNull::from_mut(&mut error_dst).cast(),
                    );

                    match (state_dst, error_dst) {
                        (Some(state), Some(body)) => {
                            Ok((state, Some(RawError { boxed_body: body })))
                        }
                        (Some(state), None) => Ok((state, None)),
                        (None, Some(body)) => Err(RawError { boxed_body: body }),
                        (None, None) => unreachable!(),
                    }
                }
            }
        }
    }

    // Sets the state if the underlying storage type is compatible.
    pub fn try_set_state(&mut self, state: S) -> result::Result<(), S> {
        match self.select_mut() {
            SelectMut::Const(_body) => Err(state),
            SelectMut::Inline(body) => {
                body.set_value(state);
                Ok(())
            }
            SelectMut::Boxed(body) => {
                let vtable = DynBody::vtable(body.borrow());
                let mut state = Some(state);

                unsafe {
                    (vtable.try_set_state)(
                        body.borrow_mut(),
                        TypeId::of::<S>(),
                        NonNull::from_mut(&mut state).cast(),
                    )
                };

                if let Some(state) = state {
                    Err(state)
                } else {
                    Ok(())
                }
            }
        }
    }

    /// Iterates over the source error chain, starting from the immediate source.
    pub fn chain(&self) -> impl Iterator<Item = &(dyn error::Error + 'static)> {
        struct Chain<'a>(Option<&'a (dyn error::Error + 'static)>);

        impl<'a> Iterator for Chain<'a> {
            type Item = &'a (dyn error::Error + 'static);

            fn next(&mut self) -> Option<Self::Item> {
                let next = self.0.and_then(|err| err.source());

                mem::replace(&mut self.0, next)
            }
        }

        Chain(
            self.source()
                .map(|err| err as &(dyn error::Error + 'static)),
        )
    }

    /// Convert into a boxed error without reallocation if already boxed, otherwise box the error.
    pub fn into_boxed_error(self) -> Box<dyn error::Error + Send + Sync + 'static>
    where
        S: Debug,
    {
        match self.select_own() {
            SelectOwn::Const(body) => unsafe {
                // Safety: Projection from `ConstBody` to `ConstBody::context` is safe.
                let context = body
                    .borrow()
                    .project(|body| &raw const (*body).context)
                    .deref();
                (*context).into()
            },
            SelectOwn::Inline(body) => format!("{:?}", body.borrow_value()).into(),
            SelectOwn::Boxed(body) => unsafe {
                let vtable = DynBody::vtable(body.borrow());
                // Safety: The body pointer is confirmed valid.
                (vtable.into_boxed_error)(body)
            },
        }
    }
}

impl<S> Drop for RawError<S> {
    fn drop(&mut self) {
        match self.kind() {
            Self::KIND_CONST => {}
            Self::KIND_INLINE => unsafe {
                // Safety: The variant hasn't been moved out, as `self` remains owned and not forgotten.
                ManuallyDrop::drop(&mut self.inline_body);
            },
            Self::KIND_BOXED => {
                unsafe {
                    let vtable = DynBody::vtable(self.boxed_body.borrow());
                    // Safety: The body pointer is confirmed valid.
                    (vtable.drop)(ManuallyDrop::new(ManuallyDrop::take(&mut self.boxed_body)));
                }
            }
            _ => unreachable!(),
        }
    }
}

impl<S> Debug for RawError<S>
where
    S: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        render::format_debug(
            f,
            self.state(),
            self.context(),
            self.payload(),
            self.source(),
            WithBacktrace::search_debug(self),
        )
    }
}

impl<S> Display for RawError<S>
where
    S: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        render::format_display(
            f,
            self.state(),
            self.context(),
            self.payload(),
            self.source(),
            WithBacktrace::search_display(self),
        )
    }
}

impl<S> error::Error for RawError<S>
where
    S: Debug,
{
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        self.source()
            .map(|err| err as &(dyn error::Error + 'static))
    }
}

#[repr(C)]
pub struct ConstBody {
    context: &'static str,
}

/// Heap-allocated error body with type-erased source, payload, and context.
///
/// The concrete types `E`, `P`, `C` are only known at construction time and at
/// the monomorphized vtable function sites. The `RawError` stores the body as
/// `DynBody<S, (), (), ()>`. The `S` can also be erased when the state is
/// extracted.
///
/// # Safety
///
/// The `vtable` pointer must point to a `DynBodyVTable` that was monomorphized
/// for the same `S`, `E`, `P`, `C` as the stored data.
#[repr(C)]
struct DynBody<S = Infallible, E = (), P = (), C = ()>
where
    S: 'static,
    E: 'static,
    P: 'static,
    C: 'static,
{
    vtable: Align4Ref<'static, DynBodyVTable>, // Note: The vtable must be the first field as the other fields may be erased.
    state: MaybeUninit<S>,
    source: E,
    payload: P,
    context: C,
}

/// Virtual function table for type-erased operations on [`DynBody`].
///
/// Each function pointer is monomorphized for the concrete `S`, `E`, `P`, `C`.
///
/// # Safety
///
/// All function pointers must be valid for the concrete types stored in the `DynBody`.
/// The `Ref`/`Mut`/`Align4Own` arguments must point to a `DynBody` whose type parameters
/// match the monomorphization that produced the function pointer.
struct DynBodyVTable {
    /// See [DynBody::drop].
    drop: unsafe fn(ManuallyDrop<Align4Own<DynBody>>),
    /// See [DynBody::into_state].
    into_state: unsafe fn(ManuallyDrop<Align4Own<DynBody>>, TypeId, NonNull<()>),
    /// See [DynBody::into_source].
    into_source: unsafe fn(
        ManuallyDrop<Align4Own<DynBody>>,
    ) -> Option<Box<dyn error::Error + Send + Sync + 'static>>,
    /// See [DynBody::into_parts].
    into_parts: unsafe fn(
        ManuallyDrop<Align4Own<DynBody>>,
        TypeId,
        NonNull<()>,
        TypeId,
        NonNull<()>,
        NonNull<()>,
        TypeId,
        NonNull<()>,
    ),
    /// See [DynBody::extract_state].
    extract_state: unsafe fn(ManuallyDrop<Align4Own<DynBody>>, TypeId, NonNull<()>, NonNull<()>),
    /// See [DynBody::into_boxed_error].
    into_boxed_error: unsafe fn(
        ManuallyDrop<Align4Own<DynBody>>,
    ) -> Box<dyn error::Error + Send + Sync + 'static>,
    /// See [DynBody::try_set_state].
    try_set_state: unsafe fn(Mut<DynBody>, TypeId, NonNull<()>) -> bool,
    /// See [DynBody::source].
    source: unsafe fn(Ref<'_, DynBody>) -> Option<&(dyn error::Error + 'static)>,
    /// See [DynBody::state].
    state: unsafe fn(Ref<'_, DynBody>, TypeId, NonNull<()>),
    /// See [DynBody::payload].
    payload: unsafe fn(Ref<'_, DynBody>) -> Option<&(dyn Display + Send + Sync + 'static)>,
    /// See [DynBody::context].
    context: unsafe fn(Ref<'_, DynBody>) -> Option<&(dyn Display + Send + Sync + 'static)>,
    /// See [DynBody::downcast_payload_ref].
    downcast_payload_ref: unsafe fn(Ref<'_, DynBody>, TypeId, NonNull<()>),
    /// See [DynBody::downcast_payload_mut].
    downcast_payload_mut: unsafe fn(Mut<'_, DynBody>, TypeId, NonNull<()>),
}

impl DynBodyVTable {
    const fn new<S, E, P, L>() -> Self
    where
        S: Debug + Send + Sync + 'static,
        E: error::Error + Send + Sync + 'static,
        L: Display + Send + Sync + 'static,
        P: Display + Send + Sync + 'static,
    {
        DynBodyVTable {
            drop: DynBody::<S, E, P, L>::drop,
            into_state: DynBody::<S, E, P, L>::into_state,
            into_source: DynBody::<S, E, P, L>::into_source,
            into_parts: DynBody::<S, E, P, L>::into_parts,
            extract_state: DynBody::<S, E, P, L>::extract_state,
            into_boxed_error: DynBody::<S, E, P, L>::into_boxed_error,
            try_set_state: DynBody::<S, E, P, L>::try_set_state,
            source: DynBody::<S, E, P, L>::source,
            state: DynBody::<S, E, P, L>::state,
            payload: DynBody::<S, E, P, L>::payload,
            context: DynBody::<S, E, P, L>::context,
            downcast_payload_ref: DynBody::<S, E, P, L>::downcast_payload_ref,
            downcast_payload_mut: DynBody::<S, E, P, L>::downcast_payload_mut,
        }
    }
}

impl<S, E, P, C> DynBody<S, E, P, C> {
    const NO_STATE: Metadata = Metadata::_0;
    const HAS_STATE: Metadata = Metadata::_1;

    /// Returns a static shared reference to the vtable.
    fn vtable(this: Ref<'_, DynBody<S, E, P, C>>) -> &'static DynBodyVTable {
        unsafe {
            this.project(|body| &raw const (*body).vtable)
                .deref()
                .borrow()
                .deref()
        }
    }
}

impl<S, E, P, C> DynBody<S, E, P, C>
where
    S: Debug + Send + Sync + 'static,
    E: error::Error + Send + Sync + 'static,
    P: Display + Send + Sync + 'static,
    C: Display + Send + Sync + 'static,
{
    fn vtable_from_state(state: Option<S>) -> (Align4Ref<'static, DynBodyVTable>, MaybeUninit<S>) {
        (
            Align4Ref::new(
                &const { Align4(DynBodyVTable::new::<S, E, P, C>()) },
                match state {
                    Some(_) => Self::HAS_STATE,
                    None => Self::NO_STATE,
                },
            ),
            match state {
                Some(state) => MaybeUninit::new(state),
                None => MaybeUninit::uninit(),
            },
        )
    }

    /// Check if the state exisis.
    fn has_state(&self) -> bool {
        unsafe {
            // # Safety: `Align4Ref` is `repr(C)` and stores the metadata at offset 0.
            match Metadata((&raw const (self.vtable) as *const u8).read() & Metadata::MASK) {
                Self::NO_STATE => false,
                Self::HAS_STATE => true,
                _ => unreachable!(),
            }
        }
    }

    /// Returns a shared reference to the state, if any.
    fn try_get_state(&self) -> Option<&S> {
        self.has_state()
            .then(|| unsafe { self.state.assume_init_ref() })
    }

    /// Replaces the stored state with a new value. Returns the old one, if any.
    fn replace_state(&mut self, state: Option<S>) -> Option<S> {
        unsafe {
            let (has_state, old_state) = match (self.has_state(), state) {
                (false, None) => (false, None),
                (false, Some(state)) => {
                    self.state.write(state);
                    (true, None)
                }
                (true, None) => (false, Some(self.state.assume_init_read())),
                (true, Some(state)) => {
                    let old_state = self.state.assume_init_read();
                    self.state.write(state);
                    (true, Some(old_state))
                }
            };
            let pvt = self.vtable.borrow_raw().deref();
            self.vtable = Align4Ref::new(
                pvt,
                match has_state {
                    false => Self::NO_STATE,
                    true => Self::HAS_STATE,
                },
            );

            old_state
        }
    }

    /// Consumes `self` and decomposes into its raw components:
    /// `(state, source, payload, context)`.
    fn destruct(self) -> (Option<S>, E, P, C) {
        let has_state = self.has_state();
        let mut this = MaybeUninit::new(self);
        let this = this.as_mut_ptr();
        unsafe {
            let state = has_state.then(|| (&raw mut (*this).state).read().assume_init());
            let source = (&raw mut (*this).source).read();
            let payload = (&raw mut (*this).payload).read();
            let context = (&raw mut (*this).context).read();
            (state, source, payload, context)
        }
    }
}

impl<S, E, P, C> DynBody<S, E, P, C>
where
    S: Debug + Send + Sync + 'static,
    E: error::Error + Send + Sync + 'static,
    P: Display + Send + Sync + 'static,
    C: Display + Send + Sync + 'static,
{
    /// Drops the boxed body.
    ///
    /// # Safety
    ///
    /// - `this` must be a valid `Align4Own` pointing to a heap-allocated `DynBody<S, E, P, C>`.
    unsafe fn drop(mut this: ManuallyDrop<Align4Own<DynBody>>) {
        let _ = unsafe { ManuallyDrop::take(&mut this).cast::<Self>().into_boxed() };
    }

    /// Extracts `state` from the boxed body and drops the allocation.
    ///
    /// # Safety
    ///
    /// - `this` must be a valid `Align4Own` pointing to a heap-allocated `DynBody<S, E, P, C>`.
    /// - `state_dst` must be a valid, aligned, mutable pointer to `Option<S>`.
    unsafe fn into_state(
        mut this: ManuallyDrop<Align4Own<DynBody>>,
        state_ty: TypeId,
        state_dst: NonNull<()>,
    ) {
        let Align4(mut this) =
            *unsafe { ManuallyDrop::take(&mut this).cast::<Self>().into_boxed() };

        if TypeId::of::<S>() == state_ty {
            // Safety: The caller guarantees `state_dst` points to a valid `Option<S>`.
            let dst = unsafe { state_dst.cast::<Option<S>>().as_mut() };
            *dst = this.replace_state(None);
        }
    }

    /// Extracts the source error as a trait object from the boxed body.
    ///
    /// # Safety
    ///
    /// Same as [`into_state`](DynBody::into_state). Returns `None` if `E` is [`Nae`].
    unsafe fn into_source(
        mut this: ManuallyDrop<Align4Own<DynBody>>,
    ) -> Option<Box<dyn error::Error + Send + Sync + 'static>> {
        let Align4(this) = *unsafe { ManuallyDrop::take(&mut this).cast::<Self>().into_boxed() };
        if rtti::is_same_ty::<E, Nae>() {
            return None;
        };

        let (_, source, ..) = this.destruct();

        match rtti::concretize::<_, WithBacktrace>(source) {
            Ok(with_backtrace) => with_backtrace.into_source(),
            Err(source) => Some(Box::new(source)),
        }
    }

    /// Decomposes the boxed body: extracts source and payload into caller-provided
    /// `Option`s (if the `TypeId` matches), and returns the state.
    ///
    /// # Safety
    ///
    /// - `this` must be a valid `Align4Own` pointing to `DynBody<S, E, P, C>`.
    /// - `source_dst`, `payload_dst`,`context_dst`, `state_dst` must be valid, aligned, mutable
    ///   pointers to `Option<E>`, `Option<P>`, `Option<&'static str>` and `Option<S>` respectively.
    #[allow(clippy::too_many_arguments)]
    unsafe fn into_parts(
        mut this: ManuallyDrop<Align4Own<DynBody>>,
        source_ty: TypeId,
        source_dst: NonNull<()>,
        payload_ty: TypeId,
        payload_dst: NonNull<()>,
        context_dst: NonNull<()>,
        state_ty: TypeId,
        state_dst: NonNull<()>,
    ) {
        let Align4(this) = *unsafe { ManuallyDrop::take(&mut this).cast::<Self>().into_boxed() };
        let (state, source, payload, context) = this.destruct();

        if !rtti::is_same_ty::<E, Nae>() {
            match rtti::concretize::<_, WithBacktrace>(source) {
                Ok(with_backtrace) => unsafe {
                    with_backtrace.take_source(source_ty, source_dst);
                },
                Err(source) if TypeId::of::<E>() == source_ty => {
                    // Safety: The caller guarantees `source_dst` points to a valid `Option<E>`.
                    let dst = unsafe { source_dst.cast::<Option<E>>().as_mut() };
                    dst.replace(source);
                }
                _ => {}
            }
        }
        if !rtti::is_same_ty::<P, payload::Empty>() && TypeId::of::<P>() == payload_ty {
            // Safety: The caller guarantees `payload_dst` points to a valid `Option<P>`.
            let dst = unsafe { payload_dst.cast::<Option<P>>().as_mut() };
            dst.replace(payload);
        }
        if !rtti::is_same_ty::<C, context::Unit>() && rtti::is_same_ty::<C, &'static str>() {
            // Safety: The caller guarantees `context_dst` points to a valid `Option<&'static str>`.
            let dst = unsafe { context_dst.cast::<Option<C>>().as_mut() };
            dst.replace(context);
        }
        if TypeId::of::<S>() == state_ty {
            let dst = unsafe { state_dst.cast::<Option<S>>().as_mut() };
            *dst = state;
        }
    }

    /// Extracts the state from the boxed body. This function guarantees at least one of the `Option` will be filled.
    ///
    /// # Safety
    ///
    /// - `this` must be a valid `Align4Own` pointing to `DynBody<S, E, P, C>`.
    /// - `state_dst` must be a valid, aligned, mutable pointer to `Option<StateTy>`.
    /// - `error_dst` must be a valid, aligned, mutable pointer to `Option<ManuallyDrop<Align4Own<DynBody>>>`
    unsafe fn extract_state(
        mut this: ManuallyDrop<Align4Own<DynBody>>,
        state_ty: TypeId,
        state_dst: NonNull<()>,
        error_dst: NonNull<()>,
    ) {
        let mut this = unsafe { ManuallyDrop::take(&mut this).cast::<Self>() };

        if TypeId::of::<S>() == state_ty {
            let dst = unsafe { state_dst.cast::<Option<S>>().as_mut() };
            *dst = this.borrow_mut().deref_mut().replace_state(None);
        }

        let has_context = !rtti::is_same_ty::<C, context::Unit>();
        let has_payload = !rtti::is_same_ty::<P, payload::Empty>();
        let has_source = !{
            // TODO: This will discard the backtrace. It's ideal to keep
            // the backtrace even if the error becomes empty.
            (rtti::is_same_ty::<E, Nae>())
                || (rtti::is_same_ty::<E, WithBacktrace>()
                    && this.borrow().deref().source.source().is_none())
        };

        match (has_context, has_payload, has_source) {
            (false, false, false) => {
                mem::drop(this.into_boxed());
            }
            _ => {
                let error_dst = unsafe {
                    error_dst
                        .cast::<Option<ManuallyDrop<Align4Own<DynBody>>>>()
                        .as_mut()
                };
                // Safety:
                // Erase the remaining fields to ZSTs is dangerous as the destructor assumes a correct layout.
                // So we wrap it in `ManuallyDrop` to prevent the destructor from being automatically called.
                error_dst.replace(ManuallyDrop::new(unsafe { this.cast::<DynBody>() }));
            }
        };
    }

    /// Convert the thin `DynBody` pointer to `Box<Error>` without reallocation.
    ///
    /// # Safety
    ///
    /// Same as [`into_parts`](DynBody::into_parts).
    unsafe fn into_boxed_error(
        mut this: ManuallyDrop<Align4Own<DynBody>>,
    ) -> Box<dyn error::Error + Send + Sync + 'static> {
        let this = unsafe { ManuallyDrop::take(&mut this).cast::<Self>().into_boxed() };
        let this_raw = Box::into_raw(this);

        // # Safety
        //
        // The `Ailgn4` wrapper is `repr(C)` and has only one field, so the pointer cast is valid.
        unsafe { Box::from_raw(this_raw as *mut DynBody<S, E, P, C>) }
    }

    /// Replace the state if type matches.
    ///
    /// # Safety
    ///
    /// - `this` must be a valid `Mut` pointing to `DynBody<S, E, P, C>`.
    /// - `state_src` must be a valid, aligned, mutable pointer to `Option<StateTy>`.
    unsafe fn try_set_state(
        this: Mut<'_, DynBody>,
        state_ty: TypeId,
        state_src: NonNull<()>,
    ) -> bool {
        let this = unsafe { this.cast::<Self>().deref_mut() };

        if TypeId::of::<S>() == state_ty {
            let state_src = unsafe { state_src.cast::<Option<S>>().as_mut() };
            let Some(state_src) = state_src.take() else {
                return false;
            };
            this.replace_state(Some(state_src));
            true
        } else {
            false
        }
    }

    /// Returns a reference to the source error.
    ///
    /// # Safety
    ///
    /// - `this` must point to a valid `DynBody<S, E, P, C>`.
    unsafe fn source(this: Ref<'_, DynBody>) -> Option<&(dyn error::Error + 'static)> {
        let this = unsafe { this.cast::<Self>().deref() };

        if rtti::is_same_ty::<E, Nae>() {
            return None;
        }

        match (
            rtti::is_same_ty::<E, WithBacktrace>(),
            WithBacktrace::searching(),
        ) {
            (true, false) => this.source.source(),
            (true, true) | (false, _) => Some(&this.source as _),
        }
    }

    /// Returns a reference to the state.
    ///
    /// # Safety
    ///
    /// - `this` must point to a valid `DynBody<S, E, P, C>`.
    /// - `dst` must be a valid, aligned, mutable pointer to `Option<&StateTy>`.
    unsafe fn state(this: Ref<'_, DynBody>, state_ty: TypeId, state_dst: NonNull<()>) {
        let this = unsafe { this.cast::<Self>().deref() };

        if TypeId::of::<S>() == state_ty {
            let dst = unsafe { state_dst.cast::<Option<&S>>().as_mut() };

            if let Some(state) = this.try_get_state() {
                dst.replace(state);
            }
        }
    }

    /// Returns a reference to the displayable payload.
    ///
    /// # Safety
    ///
    /// - `this` must point to a valid `DynBody<S, E, P, C>`.
    unsafe fn payload(this: Ref<'_, DynBody>) -> Option<&(dyn Display + Send + Sync + 'static)> {
        let this = unsafe { this.cast::<Self>().deref() };

        if rtti::is_same_ty::<P, payload::Empty>() {
            None
        } else {
            Some(&this.payload as &(dyn Display + Send + Sync + 'static))
        }
    }

    /// Returns a reference to the displayable context.
    ///
    /// # Safety
    ///
    /// - `this` must point to a valid `DynBody<S, E, P, C>`.
    unsafe fn context(this: Ref<'_, DynBody>) -> Option<&(dyn Display + Send + Sync + 'static)> {
        let this = unsafe { this.cast::<Self>().deref() };

        if rtti::is_same_ty::<C, context::Unit>() {
            None
        } else {
            Some(&this.context as &(dyn Display + Send + Sync + 'static))
        }
    }

    /// Attempts to downcast the payload field to the requested type `P`.
    ///
    /// Writes `Some(&P)` into `dst` if the type matches, otherwise does nothing.
    ///
    /// # Safety
    ///
    /// Same as [`downcast_source_ref`](DynBody::downcast_source_ref) for the payload field.
    /// - `dst` must be a valid, aligned, mutable pointer to `Option<&Ty>`.
    unsafe fn downcast_payload_ref(this: Ref<'_, DynBody>, ty: TypeId, dst: NonNull<()>) {
        let this = unsafe { this.cast::<Self>().deref() };

        if !rtti::is_same_ty::<P, payload::Empty>() && TypeId::of::<P>() == ty {
            let dst = unsafe { dst.cast::<Option<&P>>().as_mut() };
            *dst = Some(&this.payload);
        }
    }

    /// Attempts to downcast the payload field to the requested type `P` (mutable).
    ///
    /// Writes `Some(&mut P)` into `dst` if the type matches, otherwise does nothing.
    ///
    /// # Safety
    ///
    /// Same as [`downcast_payload_ref`](DynBody::downcast_payload_ref) with mutable access.
    /// - `dst` must be a valid, aligned, mutable pointer to `Option<&mut Ty>`.
    unsafe fn downcast_payload_mut(this: Mut<'_, DynBody>, ty: TypeId, dst: NonNull<()>) {
        let this = unsafe { this.cast::<Self>().deref_mut() };

        if !rtti::is_same_ty::<P, payload::Empty>() && TypeId::of::<P>() == ty {
            let dst = unsafe { dst.cast::<Option<&mut P>>().as_mut() };
            *dst = Some(&mut this.payload);
        }
    }
}

impl<S, E, P, C> Drop for DynBody<S, E, P, C> {
    fn drop(&mut self) {
        unsafe {
            match Metadata((&raw const (self.vtable) as *const u8).read() & Metadata::MASK) {
                Self::HAS_STATE => {
                    MaybeUninit::assume_init_drop(&mut self.state);
                }
                Self::NO_STATE => {}
                _ => unreachable!(),
            }
        }
    }
}

impl<S, E, P, C> fmt::Debug for DynBody<S, E, P, C>
where
    S: Debug + Send + Sync + 'static,
    E: error::Error + Send + Sync + 'static,
    P: Display + Send + Sync + 'static,
    C: Display + Send + Sync + 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        render::format_debug(
            f,
            self.try_get_state(),
            (!rtti::is_same_ty::<C, context::Unit>()).then_some(&self.context),
            (!rtti::is_same_ty::<P, payload::Empty>()).then_some(&self.payload),
            self.source(),
            WithBacktrace::search_debug(self),
        )
    }
}

impl<S, E, P, C> fmt::Display for DynBody<S, E, P, C>
where
    S: Debug + Send + Sync + 'static,
    E: error::Error + Send + Sync + 'static,
    P: Display + Send + Sync + 'static,
    C: Display + Send + Sync + 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        render::format_display(
            f,
            self.try_get_state(),
            (!rtti::is_same_ty::<C, context::Unit>()).then_some(&self.context),
            (!rtti::is_same_ty::<P, payload::Empty>()).then_some(&self.payload),
            self.source(),
            WithBacktrace::search_display(self),
        )
    }
}

impl<S, E, P, C> error::Error for DynBody<S, E, P, C>
where
    S: Debug + Send + Sync + 'static,
    E: error::Error + Send + Sync + 'static,
    P: Display + Send + Sync + 'static,
    C: Display + Send + Sync + 'static,
{
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        if rtti::is_same_ty::<E, Nae>() {
            return None;
        }
        match (
            rtti::is_same_ty::<E, WithBacktrace>(),
            WithBacktrace::searching(),
        ) {
            (true, false) => self.source.source(),
            (true, true) | (false, _) => Some(&self.source as _),
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::{
        format,
        string::{String, ToString},
    };
    use core::{
        convert::Infallible,
        error,
        fmt::{self, Display},
        mem,
    };

    use super::*;
    use crate::{
        context::{Blank, Literal},
        nae::Nae,
        payload,
    };

    // --- Test helpers ---

    /// A custom source error for testing.
    #[derive(Debug)]
    struct TestError(&'static str);

    impl Display for TestError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl error::Error for TestError {}

    /// A typed literal for testing.
    struct TestContext;

    impl Literal for TestContext {
        const LITERAL: &'static str = "test context";
    }

    /// A custom payload type.
    #[derive(Debug, PartialEq)]
    struct TestPayload(u32);

    impl Display for TestPayload {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "payload({})", self.0)
        }
    }

    // --- RawError kind() discrimination ---

    #[cfg(not(feature = "backtrace"))]
    #[test]
    fn kind_discriminates_const() {
        let err = RawError::new_const::<TestContext>();
        assert_eq!(err.kind(), RawError::<()>::KIND_CONST);
    }

    #[cfg(not(feature = "backtrace"))]
    #[test]
    fn kind_discriminates_inline() {
        let err = RawError::try_new_inline(4216u16).unwrap();
        assert_eq!(err.kind(), RawError::<u16>::KIND_INLINE);
    }

    #[cfg(not(feature = "backtrace"))]
    #[test]
    fn kind_discriminates_boxed() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        assert_eq!(err.kind(), RawError::<()>::KIND_BOXED);
    }

    // --- Const variant ---

    #[test]
    fn const_variant_context() {
        let err = RawError::new_const::<TestContext>();
        let ctx = err.context();
        assert!(ctx.is_some());
        assert_eq!(ctx.unwrap().to_string(), "test context");
    }

    #[test]
    fn const_variant_source_is_none() {
        let err = RawError::new_const::<TestContext>();
        assert!(err.source().is_none());
    }

    #[test]
    fn const_variant_payload_is_none() {
        let err = RawError::new_const::<TestContext>();
        assert!(err.payload().is_none());
    }

    #[test]
    fn const_variant_into_state() {
        let err = RawError::new_const::<TestContext>();
        let state = err.into_state();
        assert!(matches!(state, None));
    }

    // --- Inline variant ---

    #[test]
    fn inline_variant_state() {
        let err = RawError::try_new_inline(42u16).unwrap();
        assert!(matches!(err.state(), Some(42)));
    }

    #[test]
    fn inline_variant_into_state() {
        let err = RawError::try_new_inline(42u16).unwrap();
        assert!(matches!(err.state(), Some(42)));
    }

    #[test]
    fn inline_variant_context_is_none() {
        let err = RawError::try_new_inline(42u16).unwrap();
        assert!(err.context().is_none());
    }

    #[test]
    fn inline_variant_source_is_none() {
        let err = RawError::try_new_inline(42u16).unwrap();
        assert!(err.source().is_none());
    }

    #[test]
    fn inline_variant_payload_is_none() {
        let err = RawError::try_new_inline(42u16).unwrap();
        assert!(err.payload().is_none());
    }

    // Boxed variant ---

    #[test]
    fn boxed_variant_source() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let src = err.source();
        assert!(src.is_some());
        assert_eq!(src.unwrap().to_string(), "oops");
    }

    #[test]
    fn boxed_variant_downcast_source() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let downcasted = err.downcast_source_ref::<TestError>();
        assert!(downcasted.is_some());
        assert_eq!(downcasted.unwrap().0, "oops");
    }

    #[test]
    fn boxed_variant_downcast_source_wrong_type() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let downcasted = err.downcast_source_ref::<Nae>();
        assert!(downcasted.is_none());
    }

    #[test]
    fn boxed_variant_payload() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            TestPayload(42),
        );
        let pl = err.payload();
        assert!(pl.is_some());
        assert_eq!(pl.unwrap().to_string(), "payload(42)");
    }

    #[test]
    fn boxed_variant_downcast_payload() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            TestPayload(42),
        );
        let downcasted = err.downcast_payload_ref::<TestPayload>();
        assert!(downcasted.is_some());
        assert_eq!(downcasted.unwrap().0, 42);
    }

    #[test]
    fn boxed_variant_context() {
        let err = RawError::new_boxed::<_, _, TestContext>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let ctx = err.context();
        assert!(ctx.is_some());
        assert_eq!(ctx.unwrap().to_string(), "test context");
    }

    #[test]
    fn boxed_variant_nae_source_is_none() {
        // When source is `Nae`, `.source()` should return `None`.
        let err =
            RawError::new_boxed::<_, _, Blank>(Some(42u32), Nae::new(), payload::Empty::new());
        assert!(err.source().is_none());
        assert!(matches!(err.state(), Some(42)));
    }

    #[test]
    fn boxed_variant_empty_payload_is_none() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        assert!(err.payload().is_none());
    }

    // --- into_source ---

    #[test]
    fn boxed_variant_into_source_returns_boxed_error() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let src = err.into_source();
        assert!(src.is_some());
        assert_eq!(src.unwrap().to_string(), "oops");
    }

    #[test]
    fn boxed_variant_into_source_nae_returns_none() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            Nae::new(),
            payload::Empty::new(),
        );
        assert!(err.into_source().is_none());
    }

    // --- into_parts ---

    #[test]
    fn boxed_variant_into_parts_matches_types() {
        let err = RawError::new_boxed::<_, _, TestContext>(
            Some("state"),
            TestError("oops"),
            TestPayload(99),
        );
        let (state, context, payload, source) = err.into_parts::<TestPayload, TestError>();
        assert!(matches!(state, Some("state")));
        assert!(source.is_some());
        assert_eq!(source.unwrap().0, "oops");
        assert!(payload.is_some());
        assert_eq!(payload.unwrap().0, 99);
        assert!(matches!(context, Some(TestContext::LITERAL)))
    }

    #[test]
    fn boxed_variant_into_parts_wrong_source_type() {
        let err = RawError::new_boxed::<_, _, Blank>(
            None::<Infallible>,
            TestError("oops"),
            payload::Empty::new(),
        );
        let (_, _, payload, source) = err.into_parts::<payload::Empty, String>();
        assert!(source.is_none());
        assert!(payload.is_none());
    }

    #[test]
    fn const_variant_into_parts() {
        let err = RawError::new_const::<TestContext>();
        let (state, _, payload, source) = err.into_parts::<TestPayload, TestError>();
        assert!(source.is_none());
        assert!(payload.is_none());
        assert_eq!(state, None);
    }

    #[test]
    fn inline_variant_into_parts() {
        let err = RawError::try_new_inline(42u16).unwrap();
        let (state, _, payload, source) = err.into_parts::<TestPayload, TestError>();
        assert!(source.is_none());
        assert!(payload.is_none());
        assert!(matches!(state, Some(42)));
    }

    // --- Drop safety (checked via sanitizer / basic leak check) ---

    /// Allocate a boxed variant and ensure it can be observed to drop.
    #[test]
    fn boxed_variant_drop_does_not_leak() {
        use core::sync::atomic::{AtomicBool, Ordering};

        static DROPPED: AtomicBool = AtomicBool::new(false);

        #[derive(Debug)]
        struct DropWatch;

        impl Drop for DropWatch {
            fn drop(&mut self) {
                DROPPED.store(true, Ordering::SeqCst);
            }
        }

        impl Display for DropWatch {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "")
            }
        }

        impl error::Error for DropWatch {}

        {
            let _err = RawError::new_boxed::<_, _, Blank>(
                None::<Infallible>,
                DropWatch,
                payload::Empty::new(),
            );
        } // drop here
        assert!(DROPPED.load(Ordering::SeqCst));
    }

    // --- State round-trip for const variant (S = ()) ---

    #[test]
    fn const_variant_state_is_none() {
        let err = RawError::new_const::<TestContext>();
        assert!(err.state().is_none());
    }

    // --- Size checks ---

    #[test]
    fn raw_error_size() {
        assert_eq!(mem::size_of::<RawError<()>>(), mem::size_of::<usize>());
        assert_eq!(mem::size_of::<RawError<u128>>(), mem::size_of::<usize>());
        assert_eq!(
            mem::size_of::<RawError<Infallible>>(),
            mem::size_of::<usize>()
        );
    }

    // --- State extraction ---

    #[test]
    fn state_extraction() {
        {
            let err = RawError::new_inline_or_boxed(42u16);
            assert!(matches!(err.extract_state(), Ok((42, None))));
        }
        {
            let err = RawError::new_inline_or_boxed(42u128);
            assert!(matches!(err.extract_state(), Ok((42, None))));
        }
        {
            let err =
                RawError::new_boxed::<_, _, Blank>(None::<Infallible>, Nae::new(), format!("oops"));
            assert!(matches!(err.extract_state(), Err(err) if format!("{err:-}") == "oops"));
        }
        {
            let err = RawError::new_boxed::<_, _, Blank>(Some(42i32), Nae::new(), format!("oops"));
            assert!(
                matches!(err.extract_state(), Ok((42i32, Some(err))) if format!("{err:-}") == "oops")
            );
        }
    }
}