tracepoint_decode 0.4.1

Rust API for decoding tracepoints
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
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use eventheader_types::*;

use core::array;
use core::fmt;

#[cfg(feature = "rustc_1_77")]
use core::net;

use crate::*;

/// Encoding of a string field of an event.
#[derive(Clone, Copy, Debug)]
pub enum PerfTextEncoding {
    /// Corresponds to [`FieldFormat::String8`], i.e. "unspecified single-byte character set",
    /// generally decoded as Latin1 (ISO-8859-1) or Windows-1252.
    Latin1,

    /// UTF-8 string.
    Utf8,

    /// UTF-16 string, big-endian byte order.
    Utf16BE,

    /// UTF-16 string, little-endian byte order.
    Utf16LE,

    /// UTF-32 string, big-endian byte order.
    Utf32BE,

    /// UTF-32 string, little-endian byte order.
    Utf32LE,
}

impl PerfTextEncoding {
    /// Returns `(Option<PerfTextEncoding>, bom_size)` corresponding to the BOM at the start of
    /// the given bytes. If no BOM is present, returns `(None, 0)`.
    pub fn from_bom(bytes: &[u8]) -> (Option<Self>, u8) {
        let len = bytes.len();
        let result = if len >= 4
            && bytes[0] == 0x00
            && bytes[1] == 0x00
            && bytes[2] == 0xFE
            && bytes[3] == 0xFF
        {
            (Some(Self::Utf32BE), 4)
        } else if len >= 4
            && bytes[0] == 0xFF
            && bytes[1] == 0xFE
            && bytes[2] == 0x00
            && bytes[3] == 0x00
        {
            (Some(Self::Utf32LE), 4)
        } else if len >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF {
            (Some(Self::Utf16BE), 2)
        } else if len >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE {
            (Some(Self::Utf16LE), 2)
        } else if len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF {
            (Some(Self::Utf8), 3)
        } else {
            (None, 0)
        };

        return result;
    }
}

/// Flags used when formatting a value as a string.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PerfConvertOptions(u32);

#[allow(non_upper_case_globals)]
impl PerfConvertOptions {
    /// Returns a `PerfConvertOptions` with the specified numeric value.
    pub const fn from_int(value: u32) -> Self {
        return Self(value);
    }

    /// Returns the numeric value corresponding to this `PerfConvertOptions` value.
    pub const fn as_int(self) -> u32 {
        return self.0;
    }

    /// Returns true if `self & flag != 0`.
    pub const fn has_flag(self, flag: Self) -> bool {
        return self.0 & flag.0 != 0;
    }

    /// Returns `self & flag`.
    pub const fn and(self, flag: Self) -> Self {
        return Self(self.0 & flag.0);
    }

    /// Returns `self & !flag`.
    pub const fn and_not(self, flag: Self) -> Self {
        return Self(self.0 & !flag.0);
    }

    /// Returns `self | flag`.
    pub const fn or(self, flag: Self) -> Self {
        return Self(self.0 | flag.0);
    }

    /// No flags set.
    pub const None: Self = Self(0);

    /// Add spaces to the output, e.g. `"Name": [ 1, 2, 3 ]` instead of `"Name":[1,2,3]`.
    pub const Space: Self = Self(0x01);

    /// When formatting with `write_item_and_move_next_sibling`, include the
    /// "Name": prefix for the root item.
    pub const RootName: Self = Self(0x02);

    /// When formatting with `write_item_and_move_next_sibling`, for items with a
    /// non-zero tag, add a tag suffix to the item's "Name": prefix, e.g.
    /// "Name;tag=0xNNNN": "ItemValue".
    pub const FieldTag: Self = Self(0x04);

    /// If set, `f32` will format with `"{:.9}"` or `"{:.9e}"`, and `f64` will format with
    /// `"{:.17}"` or `"{:.17e}"`. If unset, both will format with `"{:}"` or `"{:e}"`.
    pub const FloatExtraPrecision: Self = Self(0x10);

    /// If set, non-finite float will format as a string like "NaN" or "-Infinity".
    /// If unset, non-finite float will format as a null.
    pub const FloatNonFiniteAsString: Self = Self(0x20);

    /// If set, hex integer will format in JSON as a string like "0xF123".
    /// If unset, a hex integer will format in JSON as a decimal like 61731.
    pub const IntHexAsString: Self = Self(0x40);

    /// If set, boolean outside 0..1 will format as a string like "BOOL(-123)".
    /// If unset, boolean outside 0..1 will format as a number like -123.
    pub const BoolOutOfRangeAsString: Self = Self(0x80);

    /// If set, UnixTime within supported range will format as a string like "2024-04-08T23:59:59Z".
    /// If unset, UnixTime within supported range will format as a number like 1712620799.
    /// Supported range varies by platform, but typically includes at least 1601..9999.
    pub const UnixTimeWithinRangeAsString: Self = Self(0x100);

    /// If set, UnixTime64 outside supported range will format as a string like "TIME(-62135596801)".
    /// If unset, UnixTime64 outside supported range will format as a number like -62135596801.
    /// Supported range varies by platform, but typically includes at least 1601..9999.
    pub const UnixTimeOutOfRangeAsString: Self = Self(0x200);

    /// If set, Errno within 0..133 will format as a string like "ERRNO(0)" or "ENOENT(2)".
    /// If unset, Errno within 0..133 will format as a number like 0 or 2.
    pub const ErrnoKnownAsString: Self = Self(0x400);

    /// If set, Errno outside 0..133 will format as a string like "ERRNO(-1)".
    /// If unset, Errno outside 0..133 will format as a number like -1.
    pub const ErrnoUnknownAsString: Self = Self(0x800);

    /// For non-JSON string conversions: replace control characters with space.
    /// Conflicts with StringControlCharsJsonEscape.
    pub const StringControlCharsReplaceWithSpace: Self = Self(0x10000);

    /// For non-JSON string conversions: escape control characters using JSON-compatible
    /// escapes sequences, e.g. "\n" for newline or "\u0000" for NUL.
    /// Conflicts with StringControlCharsReplaceWithSpace.
    pub const StringControlCharsJsonEscape: Self = Self(0x20000);

    /// Mask for string control character flags.
    pub const StringControlCharsMask: Self =
        Self(Self::StringControlCharsReplaceWithSpace.0 | Self::StringControlCharsJsonEscape.0);

    /// Default flags.
    pub const Default: Self = Self(
        Self::Space.0
            | Self::RootName.0
            | Self::FieldTag.0
            | Self::FloatNonFiniteAsString.0
            | Self::IntHexAsString.0
            | Self::BoolOutOfRangeAsString.0
            | Self::UnixTimeWithinRangeAsString.0
            | Self::UnixTimeOutOfRangeAsString.0
            | Self::ErrnoKnownAsString.0
            | Self::ErrnoUnknownAsString.0
            | Self::StringControlCharsReplaceWithSpace.0,
    );

    /// All flags set.
    pub const All: Self = Self(!0u32);
}

/// Options for the "meta" suffix generated by [`EventHeaderEventInfo::json_meta_display`],
/// [`crate::PerfSampleEventInfo::json_meta_display`], and
/// [`crate::PerfNonSampleEventInfo::json_meta_display`].
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PerfMetaOptions(u32);

#[allow(non_upper_case_globals)]
impl PerfMetaOptions {
    /// Returns a `PerfMetaOptions` with the specified numeric value.
    pub const fn from_int(value: u32) -> Self {
        return Self(value);
    }

    /// Returns the numeric value corresponding to this `PerfMetaOptions` value.
    pub const fn as_int(self) -> u32 {
        return self.0;
    }

    /// Returns true if `self & flag != 0`.
    pub const fn has_flag(self, flag: Self) -> bool {
        return self.0 & flag.0 != 0;
    }

    /// Returns `self & flag`.
    pub const fn and(self, flag: Self) -> Self {
        return Self(self.0 & flag.0);
    }

    /// Returns `self & !flag`.
    pub const fn and_not(self, flag: Self) -> Self {
        return Self(self.0 & !flag.0);
    }

    /// Returns `self | flag`.
    pub const fn or(self, flag: Self) -> Self {
        return Self(self.0 | flag.0);
    }

    /// No flags set (disables the "meta" suffix).
    pub const None: Self = Self(0);

    /// Event identity, `"n":"provider:event"` before the user fields (not in the suffix).
    /// This flag is not used by `json_meta_display`, but may be used by the caller to
    /// track whether the caller wants to add the "n" field to the event.
    pub const N: Self = Self(0x1);

    /// timestamp.
    pub const Time: Self = Self(0x2);

    /// cpu index.
    pub const Cpu: Self = Self(0x4);

    /// process id.
    pub const Pid: Self = Self(0x8);

    /// thread id (only if different from Pid).
    pub const Tid: Self = Self(0x10);

    /// eventheader id (decimal integer, omitted if 0).
    pub const Id: Self = Self(0x20);

    /// eventheader version (decimal integer, omitted if 0).
    pub const Version: Self = Self(0x40);

    /// eventheader level (decimal integer, omitted if 0).
    pub const Level: Self = Self(0x80);

    /// eventheader keyword (hexadecimal string, omitted if 0).
    pub const Keyword: Self = Self(0x100);

    /// eventheader opcode (decimal integer, omitted if 0).
    pub const Opcode: Self = Self(0x200);

    /// eventheader tag (hexadecimal string, omitted if 0).
    pub const Tag: Self = Self(0x400);

    /// eventheader activity ID (UUID string, omitted if not set).
    pub const Activity: Self = Self(0x800);

    /// eventheader related activity ID (UUID string, omitted if not set).
    pub const RelatedActivity: Self = Self(0x1000);

    /// provider name or system name (string).
    pub const Provider: Self = Self(0x10000);

    /// event name or tracepoint name (string).
    pub const Event: Self = Self(0x20000);

    /// eventheader provider options (string, omitted if none).
    pub const Options: Self = Self(0x40000);

    /// eventheader flags (hexadecimal string).
    pub const Flags: Self = Self(0x80000);

    /// Include the common_* fields before the user fields.
    /// Not used by `json_meta_display`.
    pub const Common: Self = Self(0x100000);

    /// Include N..RelatedActivity.
    pub const Default: Self = Self(0xffff);

    /// All flags set.
    pub const All: Self = Self(!0);
}

/// Provides access to the metadata
/// of a perf event item. An item is a field of the event or an element of an
/// array field of the event.
///
/// The item may represent one of the following, determined by the
/// `Metadata.IsScalar` and `Metadata.TypeSize`
/// properties:
///
/// - **Simple scalar:** `IsScalar && TypeSize != 0`
///
///   Non-array field, or one element of an array field.
///   Value type is simple (fixed-size value).
///
///   `ElementCount` is always 1.
///
///   `Format` is significant and `StructFieldCount` should be ignored
///   (simple type is never `Struct`).
///
/// - **Complex scalar:** `IsScalar && TypeSize == 0`
///
///   Non-array field, or one element of an array field.
///   Value type is complex (variable-size or struct value).
///
///   `ElementCount` is always 1.
///
///   If `Encoding == Struct`, this is the beginning or end of a structure,
///   `Format` should be ignored, and `StructFieldCount` is significant.
///   Otherwise, this is a variable-length value, `Format` is significant,
///   and `StructFieldCount` should be ignored.
///
/// - **Simple array:** `!IsScalar && TypeSize != 0`
///
///   Array field (array-begin or array-end item).
///   Array element type is simple (fixed-size element).
///
///   `ElementCount` is the number of elements in the array.
///
///   `Format` is significant and `StructFieldCount` should be ignored
///   (simple type is never `Struct`).
///
/// - **Complex array:** `!IsScalar && TypeSize == 0`
///
///   Array field (array-begin or array-end item).
///   Array element type is complex (variable-size or struct element).
///
///   `ElementCount` is the number of elements in the array.
///
///   If `Encoding == Struct`, this is the beginning or end of an array of structures,
///   `Format` should be ignored, and `StructFieldCount` is significant.
///   Otherwise, this is an array of variable-length values, `Format` is significant,
///   and `StructFieldCount` should be ignored.
#[derive(Clone, Copy, Debug)]
pub struct PerfItemMetadata {
    element_count: u16,
    field_tag: u16,
    type_size: u8,
    encoding_and_array_flag_and_is_scalar: FieldEncoding,
    format: FieldFormat,
    byte_reader: PerfByteReader,
}

impl PerfItemMetadata {
    /// Initializes a "null" instance of the PerfItemMetadata struct.
    pub const fn null() -> Self {
        Self {
            element_count: 0,
            field_tag: 0,
            type_size: 0,
            encoding_and_array_flag_and_is_scalar: FieldEncoding::Invalid,
            format: FieldFormat::Default,
            byte_reader: PerfByteReader::new(false),
        }
    }

    /// Initializes a new instance of the PerfItemMetadata struct.
    ///
    /// These are not normally created directly. You'll normally get instances of this struct from
    /// [EventHeaderEnumerator]`.item_metadata()` or indirectly from [EventHeaderEnumerator]`.item_info()`.
    ///
    /// - **byte_reader:**
    ///   Reader that is configured for the event data's byte order.
    ///
    /// - **encoding_and_array_flag:**
    ///   The field encoding, including the appropriate array flag if the field is an array element,
    ///   array-begin, or array-end. The chain flag must be unset.
    ///
    /// - **format:**
    ///   The field format. The chain flag must be unset.
    ///
    /// - **is_scalar:**
    ///   True if this represents a non-array value or a single element of an array.
    ///   False if this represents an array-begin or an array-end.
    ///
    /// - **type_size:**
    ///   For simple encodings (e.g. Value8, Value16, Value32, Value64, Value128),
    ///   this is the size of one element in bytes (1, 2, 4, 8, 16). For complex types
    ///   (e.g. Struct or string), this is 0.
    ///
    /// - **element_count:**
    ///   For array-begin or array-end, this is number of elements in the array.
    ///   For non-array or for array element, this is 1.
    ///   This may be 0 in the case of a variable-length array of length 0.
    ///
    /// - **field_tag:**
    ///   Field tag, or 0 if none.
    pub const fn new(
        byte_reader: PerfByteReader,
        encoding_and_array_flag: FieldEncoding,
        format: FieldFormat,
        is_scalar: bool,
        type_size: u8,
        element_count: u16,
        field_tag: u16,
    ) -> Self {
        // Chain flags must be masked-out by caller.
        debug_assert!(!encoding_and_array_flag.has_chain_flag());
        debug_assert!(!format.has_chain_flag());

        debug_assert!(encoding_and_array_flag.array_flags() != FieldEncoding::ArrayFlagMask);

        #[cfg(debug_assertions)]
        if is_scalar {
            // If scalar, elementCount must be 1.
            debug_assert!(element_count == 1);
        } else {
            // If not scalar, must be an array.
            debug_assert!(encoding_and_array_flag.is_array());
        }

        #[cfg(debug_assertions)]
        if matches!(
            encoding_and_array_flag.without_flags(),
            FieldEncoding::Struct
        ) {
            debug_assert!(type_size == 0); // Structs are not simple types.
            debug_assert!(format.as_int() != 0); // No zero-length structs.
        }

        let is_scalar_flag = if is_scalar {
            FieldEncoding::ChainFlag
        } else {
            0
        };

        return Self {
            element_count,
            field_tag,
            type_size,
            encoding_and_array_flag_and_is_scalar: encoding_and_array_flag
                .with_flags(is_scalar_flag),
            format,
            byte_reader,
        };
    }

    /// For array-begin or array-end item, this is number of elements in the array.
    /// For non-array or for element of an array, this is 1.
    /// This may be 0 in the case of a variable-length array of length 0.
    pub const fn element_count(&self) -> u16 {
        return self.element_count;
    }

    /// Field tag, or 0 if none.
    pub const fn field_tag(&self) -> u16 {
        return self.field_tag;
    }

    /// For simple encodings (e.g. Value8, Value16, Value32, Value64, Value128),
    /// this is the size of one element in bytes (1, 2, 4, 8, 16). For complex types
    /// (e.g. Struct or string), this is 0.
    pub const fn type_size(&self) -> u8 {
        return self.type_size;
    }

    /// Item's underlying encoding. The encoding indicates how to determine the item's
    /// size. The encoding also implies a default formatting that should be used if
    /// the specified format is `Default` (0), unrecognized, or unsupported. The value
    /// returned by this property does not include any flags.
    pub const fn encoding(&self) -> FieldEncoding {
        return self.encoding_and_array_flag_and_is_scalar.without_flags();
    }

    /// Returns the field's `CArrayFlag` or `VArrayFlag` if the item represents an array-begin
    /// field, an array-end field, or an element within an array field.
    /// Returns 0 for a non-array item.
    pub const fn array_flag(&self) -> u8 {
        return self.encoding_and_array_flag_and_is_scalar.array_flags();
    }

    /// Returns true if this item is a scalar (a non-array field or a single element of an array field).
    /// Returns false if this item is an array (an array-begin or an array-end item).
    pub const fn is_scalar(&self) -> bool {
        return self.encoding_and_array_flag_and_is_scalar.has_chain_flag();
    }

    /// Returns true if this item represents an element within an array.
    /// Returns false if this item is a non-array field, an array-begin, or an array-end.
    pub const fn is_element(&self) -> bool {
        let enc = self.encoding_and_array_flag_and_is_scalar.as_int();
        // Return item_is_scalar && field_is_array
        return 0 != (enc & FieldEncoding::ChainFlag) && 0 != (enc & FieldEncoding::ArrayFlagMask);
    }

    /// Field's semantic type. May be `Default`.
    /// Meaningful only when `encoding() != Struct` (`format` is aliased with `struct_field_count`).
    pub const fn format(&self) -> FieldFormat {
        return self.format;
    }

    /// Number of fields in the struct. Should never be 0.
    /// Meaningful only when `encoding() == Struct` (`struct_field_count` is aliased with `format`).
    pub const fn struct_field_count(&self) -> u8 {
        return self.format.as_int();
    }

    /// A [`PerfByteReader`] that can be used to fix the byte order of this item's data.
    /// This is the same as `PerfByteReader::new(self.source_big_endian())`.
    pub const fn byte_reader(&self) -> PerfByteReader {
        return self.byte_reader;
    }

    /// True if this item's data uses big-endian byte order.
    /// This is the same as `self.byte_reader().source_big_endian()`.
    pub const fn source_big_endian(&self) -> bool {
        return self.byte_reader.source_big_endian();
    }
}

impl Default for PerfItemMetadata {
    fn default() -> Self {
        PerfItemMetadata::null()
    }
}

/// Provides access to the metadata
/// and content
/// of a perf event item. An item is a field of the event or an element of an
/// array field of the event.
///
/// The item may represent one of the following, determined by the
/// `Metadata.IsScalar` and `Metadata.TypeSize`
/// properties:
///
/// - **Simple scalar:** `IsScalar && TypeSize != 0`
///
///   Non-array field, or one element of an array field.
///   Value type is simple (fixed-size value).
///
///   `ElementCount` is always 1.
///
///   `Format` is significant and `StructFieldCount` should be ignored
///   (simple type is never `Struct`).
///
///   `Bytes` contains the field's value and
///   `Bytes.Length == TypeSize`,
///   e.g. for a `Value32`, `TypeSize == 4` and `Bytes.Length == 4`.
///
/// - **Complex scalar:** `IsScalar && TypeSize == 0`
///
///   Non-array field, or one element of an array field.
///   Value type is complex (variable-size or struct value).
///
///   `ElementCount` is always 1.
///
///   If `Encoding == Struct`, this is the beginning or end of a structure,
///   `Format` should be ignored, and `StructFieldCount` is significant.
///   Otherwise, this is a variable-length value, `Format` is significant,
///   and `StructFieldCount` should be ignored.
///
///   If `Encoding == Struct` then `Bytes` will be empty and you should use
///   [`EventHeaderEnumerator`]`.MoveNext()` to visit the struct's member fields.
///   Otherwise, `Bytes` will contain the field's variable-length value without any length
///   prefix or nul-termination suffix.
///
/// - **Simple array:** `!IsScalar && TypeSize != 0`
///
///   Array field (array-begin or array-end item).
///   Array element type is simple (fixed-size element).
///
///   `ElementCount` is the number of elements in the array.
///
///   `Format` is significant and `StructFieldCount` should be ignored
///   (simple type is never `Struct`).
///
///   For array-end, `Bytes` will be empty.
///
///   For array-begin, `Bytes` contains the field's values and
///   `Bytes.Length == TypeSize * ElementCount`,
///   e.g. for a `Value32`, `TypeSize == 4` and `Bytes.Length == 4 * ElementCount`.
///   You may use [`EventHeaderEnumerator`]`.MoveNext()` to visit the array elements,
///   or you may process the array values directly and then use
///   [`EventHeaderEnumerator`]`.MoveNextSibling()` to skip the array elements.
///
/// - **Complex array:** `!IsScalar && TypeSize == 0`
///
///   Array field (array-begin or array-end item).
///   Array element type is complex (variable-size or struct element).
///
///   `ElementCount` is the number of elements in the array.
///
///   If `Encoding == Struct`, this is the beginning or end of an array of structures,
///   `Format` should be ignored, and `StructFieldCount` is significant.
///   Otherwise, this is an array of variable-length values, `Format` is significant,
///   and `StructFieldCount` should be ignored.
///
///   `Bytes` will be empty. Use [`EventHeaderEnumerator`]`.MoveNext()`
///   to visit the array elements.
#[derive(Clone, Copy, Debug)]
pub struct PerfItemValue<'dat> {
    bytes: &'dat [u8],
    metadata: PerfItemMetadata,
}

impl<'dat> PerfItemValue<'dat> {
    /// Initializes a new instance of the `PerfItemValue` struct.
    /// These are not normally created directly. You'll usually get instances of this struct from
    /// [`EventHeaderEnumerator`]`.item_info()`.
    pub const fn new(bytes: &'dat [u8], metadata: PerfItemMetadata) -> Self {
        #[cfg(debug_assertions)]
        if metadata.type_size != 0 && !bytes.is_empty() {
            debug_assert!(
                bytes.len() == metadata.type_size as usize * metadata.element_count as usize
            );
        }

        #[cfg(debug_assertions)]
        if metadata.encoding().as_int() == FieldEncoding::Struct.as_int() {
            debug_assert!(bytes.is_empty());
        }

        return Self { bytes, metadata };
    }

    /// The content of this item, in event byte order.
    /// This may be empty for a complex item such as a struct, or an array
    /// of variable-size elements, in which case you must access the individual
    /// sub-items using the event's enumerator.
    pub fn bytes(&self) -> &'dat [u8] {
        return self.bytes;
    }

    /// The metadata (type, endian, tag) of this item.
    pub fn metadata(&self) -> PerfItemMetadata {
        return self.metadata;
    }

    /// A [`PerfByteReader`] that can be used to fix the byte order of this item's data.
    /// This is the same as `self.metadata().byte_reader()`.
    pub fn byte_reader(&self) -> PerfByteReader {
        return self.metadata.byte_reader();
    }

    /// True if this item's data uses big-endian byte order.
    /// This is the same as `self.byte_reader().source_big_endian()`.
    pub fn source_big_endian(&self) -> bool {
        return self.metadata.source_big_endian();
    }

    /// For [`FieldEncoding::Value8`]: gets a 1-byte array starting at offset `index * 1`.
    pub fn to_u8x1(&self, index: usize) -> &'dat [u8; 1] {
        debug_assert!(self.bytes.len() > index, "index out of range");
        return array::from_ref(&self.bytes[index]);
    }

    /// For [`FieldEncoding::Value16`]: gets a 2-byte array starting at offset `index * 2`.
    pub fn to_u8x2(&self, index: usize) -> &'dat [u8; 2] {
        const SIZE: usize = 2;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
    }

    /// For [`FieldEncoding::Value32`]: gets a 4-byte array starting at offset `index * 4`.
    pub fn to_u8x4(&self, index: usize) -> &'dat [u8; 4] {
        const SIZE: usize = 4;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
    }

    /// For [`FieldEncoding::Value64`]: gets a 8-byte array starting at offset `index * 8`.
    pub fn to_u8x8(&self, index: usize) -> &'dat [u8; 8] {
        const SIZE: usize = 8;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
    }

    /// For [`FieldEncoding::Value128`]: gets a 16-byte array starting at offset `index * 16`.
    pub fn to_u8x16(&self, index: usize) -> &'dat [u8; 16] {
        const SIZE: usize = 16;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
    }

    /// For [`FieldEncoding::Value8`]: gets a `u8` value starting at offset `index * 1`.
    pub fn to_u8(&self, index: usize) -> u8 {
        debug_assert!(self.bytes.len() > index, "index out of range");
        return self.bytes[index];
    }

    /// For [`FieldEncoding::Value8`]: gets an `i8` value starting at offset `index * 1`.
    pub fn to_i8(&self, index: usize) -> i8 {
        debug_assert!(self.bytes.len() > index, "index out of range");
        return self.bytes[index] as i8;
    }

    /// For [`FieldEncoding::Value16`]: gets a `u16` value starting at offset `index * 2`.
    pub fn to_u16(&self, index: usize) -> u16 {
        debug_assert!(self.bytes.len() / 2 > index, "index out of range");
        return self.metadata.byte_reader.read_u16(&self.bytes[index * 2..]);
    }

    /// For [`FieldEncoding::Value16`]: gets an `i16` value starting at offset `index * 2`.
    pub fn to_i16(&self, index: usize) -> i16 {
        debug_assert!(self.bytes.len() / 2 > index, "index out of range");
        return self.metadata.byte_reader.read_i16(&self.bytes[index * 2..]);
    }

    /// For [`FieldEncoding::Value32`]: gets a `u32` value starting at offset `index * 4`.
    pub fn to_u32(&self, index: usize) -> u32 {
        debug_assert!(self.bytes.len() / 4 > index, "index out of range");
        return self.metadata.byte_reader.read_u32(&self.bytes[index * 4..]);
    }

    /// For [`FieldEncoding::Value32`]: gets an `i32` value starting at offset `index * 4`.
    pub fn to_i32(&self, index: usize) -> i32 {
        debug_assert!(self.bytes.len() / 4 > index, "index out of range");
        return self.metadata.byte_reader.read_i32(&self.bytes[index * 4..]);
    }

    /// For [`FieldEncoding::Value64`]: gets a `u64` value starting at offset `index * 8`.
    pub fn to_u64(&self, index: usize) -> u64 {
        debug_assert!(self.bytes.len() / 8 > index, "index out of range");
        return self.metadata.byte_reader.read_u64(&self.bytes[index * 8..]);
    }

    /// For [`FieldEncoding::Value64`]: gets an `i64` value starting at offset `index * 8`.
    pub fn to_i64(&self, index: usize) -> i64 {
        debug_assert!(self.bytes.len() / 8 > index, "index out of range");
        return self.metadata.byte_reader.read_i64(&self.bytes[index * 8..]);
    }

    /// For [`FieldEncoding::Value32`]: gets an `f32` value starting at offset `index * 4`.
    pub fn to_f32(&self, index: usize) -> f32 {
        debug_assert!(self.bytes.len() / 4 > index, "index out of range");
        return self.metadata.byte_reader.read_f32(&self.bytes[index * 4..]);
    }

    /// For [`FieldEncoding::Value64`]: gets an `f64` value starting at offset `index * 8`.
    pub fn to_f64(&self, index: usize) -> f64 {
        debug_assert!(self.bytes.len() / 8 > index, "index out of range");
        return self.metadata.byte_reader.read_f64(&self.bytes[index * 8..]);
    }

    /// For [`FieldEncoding::Value128`]: gets a big-endian [`Guid`] value starting at offset `index * 16`.
    pub fn to_guid(&self, index: usize) -> Guid {
        const SIZE: usize = 16;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return Guid::from_bytes_be(
            &self.bytes[index * SIZE..index * SIZE + SIZE]
                .try_into()
                .unwrap(),
        );
    }

    /// For [`FieldEncoding::Value16`]: gets a big-endian `u16` value starting at offset `index * 2`.
    pub fn to_port(&self, index: usize) -> u16 {
        const SIZE: usize = 2;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        return u16::from_be_bytes(
            self.bytes[index * SIZE..index * SIZE + SIZE]
                .try_into()
                .unwrap(),
        );
    }

    /// For [`FieldEncoding::Value32`]: gets an [`net::Ipv4Addr`] value starting at offset `index * 4`.
    /// Requires the `rustc_1_77` feature. If not available, use `to_u8x4` for similar functionality.
    #[cfg(feature = "rustc_1_77")]
    pub fn to_ipv4(&self, index: usize) -> net::Ipv4Addr {
        const SIZE: usize = 4;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        let bits: [u8; SIZE] = self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
        return net::Ipv4Addr::new(bits[0], bits[1], bits[2], bits[3]);
    }

    /// For [`FieldEncoding::Value128`]: gets an [`net::Ipv6Addr`] value starting at offset `index * 16`.
    /// Requires the `rustc_1_77` feature. If not available, use `to_u8x16` for similar functionality.
    #[cfg(feature = "rustc_1_77")]
    pub fn to_ipv6(&self, index: usize) -> net::Ipv6Addr {
        const SIZE: usize = 16;
        debug_assert!(self.bytes.len() / SIZE > index, "index out of range");
        let bits: &[u8; SIZE] = self.bytes[index * SIZE..index * SIZE + SIZE]
            .try_into()
            .unwrap();
        return net::Ipv6Addr::new(
            u16::from_be_bytes(bits[0..2].try_into().unwrap()),
            u16::from_be_bytes(bits[2..4].try_into().unwrap()),
            u16::from_be_bytes(bits[4..6].try_into().unwrap()),
            u16::from_be_bytes(bits[6..8].try_into().unwrap()),
            u16::from_be_bytes(bits[8..10].try_into().unwrap()),
            u16::from_be_bytes(bits[10..12].try_into().unwrap()),
            u16::from_be_bytes(bits[12..14].try_into().unwrap()),
            u16::from_be_bytes(bits[14..16].try_into().unwrap()),
        );
    }

    /// For [`FieldEncoding::Value32`]: gets an `i32` value starting at offset `index * 4`.
    pub fn to_time32(&self, index: usize) -> i32 {
        debug_assert!(self.bytes.len() / 4 > index, "index out of range");
        return self.metadata.byte_reader.read_i32(&self.bytes[index * 4..]);
    }

    /// For [`FieldEncoding::Value64`]: gets an `i64` value starting at offset `index * 8`.
    pub fn to_time64(&self, index: usize) -> i64 {
        debug_assert!(self.bytes.len() / 8 > index, "index out of range");
        return self.metadata.byte_reader.read_i64(&self.bytes[index * 8..]);
    }

    /// Interprets the value as a string and returns the string's encoded bytes along
    /// with the encoding to use to convert the bytes to a string. The encoding is
    /// determined based on the field's `format`, `encoding`, and a BOM (if present) in
    /// the value bytes. If a BOM was detected, the returned encoded bytes will NOT
    /// include the BOM.
    pub fn to_string_bytes(&self) -> (&'dat [u8], PerfTextEncoding) {
        // First, check `format` for non-UTF and UTF-with-BOM cases.
        match self.metadata.format {
            FieldFormat::String8 => return (self.bytes, PerfTextEncoding::Latin1),
            FieldFormat::StringUtfBom | FieldFormat::StringXml | FieldFormat::StringJson => {
                let from_bom = PerfTextEncoding::from_bom(self.bytes);
                if let Some(enc) = from_bom.0 {
                    return (&self.bytes[from_bom.1 as usize..], enc);
                }
            }
            _ => {}
        }

        // No BOM but assumed to be UTF. Determine text encoding from element size.
        let enc = match self.metadata.encoding() {
            FieldEncoding::Value8
            | FieldEncoding::ZStringChar8
            | FieldEncoding::StringLength16Char8
            | FieldEncoding::BinaryLength16Char8 => PerfTextEncoding::Utf8,

            FieldEncoding::Value16
            | FieldEncoding::ZStringChar16
            | FieldEncoding::StringLength16Char16 => {
                if self.metadata.source_big_endian() {
                    PerfTextEncoding::Utf16BE
                } else {
                    PerfTextEncoding::Utf16LE
                }
            }

            FieldEncoding::Value32
            | FieldEncoding::ZStringChar32
            | FieldEncoding::StringLength16Char32 => {
                if self.metadata.source_big_endian() {
                    PerfTextEncoding::Utf32BE
                } else {
                    PerfTextEncoding::Utf32LE
                }
            }

            // Invalid, Struct, Value64, Value128: probably garbage, but decode as Latin1.
            _ => PerfTextEncoding::Latin1,
        };

        return (self.bytes, enc);
    }

    /// Interprets the value as an encoded string. Decodes the string using the detected
    /// encoding and writes the decoded string to the provided writer. The encoding is
    /// determined based on the field's `format`, `encoding`, and a BOM (if present) in
    /// the value bytes. The BOM (if present) will NOT be written to the writer.
    ///
    /// - For UTF-8, invalid UTF-8 byte sequences will be treated as Latin-1 sequences.
    /// - For UTF-16 and UTF-32, invalid code units will be replaced with the Unicode
    ///   replacement character (U+FFFD).
    pub fn write_string_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let (bytes, enc) = self.to_string_bytes();
        let mut writer = filters::WriteFilter::new(writer); // Shadow
        let result = match enc {
            PerfTextEncoding::Latin1 => charconv::write_latin1_to(bytes, &mut writer),
            PerfTextEncoding::Utf8 => {
                charconv::write_utf8_with_latin1_fallback_to(bytes, &mut writer)
            }
            PerfTextEncoding::Utf16BE => charconv::write_utf16be_to(bytes, &mut writer),
            PerfTextEncoding::Utf16LE => charconv::write_utf16le_to(bytes, &mut writer),
            PerfTextEncoding::Utf32BE => charconv::write_utf32be_to(bytes, &mut writer),
            PerfTextEncoding::Utf32LE => charconv::write_utf32le_to(bytes, &mut writer),
        };

        return result;
    }

    /// Returns a display object that can be used with `write!` or `format!` macros
    /// to format the item's value as text. The output is the same as with
    /// [`PerfItemValue::write_to`].
    ///
    /// For example, `write!(writer, "{}", value.display())` might generate a string
    /// like `53`, `true`, `Hello`, or `1, 2, 3`.
    pub fn display(&self) -> display::PerfItemValueDisplay {
        return display::PerfItemValueDisplay::new(self);
    }

    /// Writes a string representation of this value to the writer.
    ///
    /// If this value is a scalar, this behaves like `write_scalar_to`.
    ///
    /// If thie value is an array, this behaves like `write_simple_array_to`.
    pub fn write_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        let result = if self.metadata.is_scalar() {
            self.write_scalar_to(writer, convert_options)
        } else {
            self.write_simple_array_to(writer, convert_options)
        };

        return result;
    }

    /// Interprets this as a scalar and writes a string representation to the writer.
    ///
    /// For example:
    ///
    /// - If the value is a decimal integer or a finite float, writes a number like `123` or `-123.456`.
    /// - If the value is a boolean, writes `false` (for 0) or `true` (for 1). For values other
    ///   than 0 or 1, writes a string like `BOOL(-123)` if `convert_options` has
    ///   [`PerfConvertOptions::BoolOutOfRangeAsString`], or a string like `-123` otherwise.
    /// - If the value is a string, control characters (char values 0..31) are
    ///   filtered based on the flags in `convert_options` (kept, replaced with space,
    ///   or JSON-escaped).
    /// - If the value is a struct, writes `Struct[N]`, where `N` is the number of fields in the struct.
    pub fn write_scalar_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size as usize <= self.bytes.len());

        let mut writer = writers::ValueWriter::new(writer, convert_options); // Shadow
        let result = match self.metadata.encoding() {
            FieldEncoding::Invalid => writer.write_str_with_no_filter("null"),
            FieldEncoding::Struct => writer.write_fmt_with_no_filter(format_args!(
                "Struct[{}]",
                self.metadata.struct_field_count()
            )),
            FieldEncoding::Value8 => self.write_value8_to(&mut writer, 0),
            FieldEncoding::Value16 => self.write_value16_to(&mut writer, 0),
            FieldEncoding::Value32 => self.write_value32_to(&mut writer, 0),
            FieldEncoding::Value64 => self.write_value64_to(&mut writer, 0),
            FieldEncoding::Value128 => self.write_value128_to(&mut writer, 0),
            FieldEncoding::ZStringChar8 => {
                self.write_scalar_string_to(&mut writer, PerfTextEncoding::Utf8)
            }
            FieldEncoding::ZStringChar16 | FieldEncoding::StringLength16Char16 => self
                .write_scalar_string_to(
                    &mut writer,
                    if self.metadata.byte_reader.source_big_endian() {
                        PerfTextEncoding::Utf16BE
                    } else {
                        PerfTextEncoding::Utf16LE
                    },
                ),
            FieldEncoding::ZStringChar32 | FieldEncoding::StringLength16Char32 => self
                .write_scalar_string_to(
                    &mut writer,
                    if self.metadata.byte_reader.source_big_endian() {
                        PerfTextEncoding::Utf32BE
                    } else {
                        PerfTextEncoding::Utf32LE
                    },
                ),
            FieldEncoding::BinaryLength16Char8 | FieldEncoding::StringLength16Char8 => {
                match self.metadata.format() {
                    FieldFormat::UnsignedInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_display_with_no_filter(self.to_u8(0) as u32),
                        2 => writer.write_display_with_no_filter(self.to_u16(0) as u32),
                        4 => writer.write_display_with_no_filter(self.to_u32(0)),
                        8 => writer.write_display_with_no_filter(self.to_u64(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::SignedInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_display_with_no_filter(self.to_i8(0) as i32),
                        2 => writer.write_display_with_no_filter(self.to_i16(0) as i32),
                        4 => writer.write_display_with_no_filter(self.to_i32(0)),
                        8 => writer.write_display_with_no_filter(self.to_i64(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::HexInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_hex32(self.to_u8(0) as u32),
                        2 => writer.write_hex32(self.to_u16(0) as u32),
                        4 => writer.write_hex32(self.to_u32(0)),
                        8 => writer.write_hex64(self.to_u64(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Errno => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_errno(self.to_u32(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Pid => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_display_with_no_filter(self.to_i32(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Time => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_time64(self.to_time32(0) as i64),
                        8 => writer.write_time64(self.to_time64(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Boolean => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_bool(self.to_u8(0) as u32),
                        2 => writer.write_bool(self.to_u16(0) as u32),
                        4 => writer.write_bool(self.to_u32(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Float => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_float32(self.to_f32(0)),
                        8 => writer.write_float64(self.to_f64(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::HexBytes => writer.write_hexbytes(self.bytes),
                    FieldFormat::String8 => {
                        writer.write_latin1_with_control_chars_filter(self.bytes)
                    }
                    FieldFormat::StringUtf => {
                        writer.write_utf8_with_control_chars_filter(self.bytes)
                    }
                    FieldFormat::StringUtfBom
                    | FieldFormat::StringXml
                    | FieldFormat::StringJson => {
                        if let (Some(bom_encoding), bom_len) =
                            PerfTextEncoding::from_bom(self.bytes)
                        {
                            writer.write_with_control_chars_filter(
                                &self.bytes[bom_len as usize..],
                                bom_encoding,
                            )
                        } else {
                            writer.write_utf8_with_control_chars_filter(self.bytes)
                        }
                    }
                    FieldFormat::Uuid => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        16 => writer.write_uuid(self.to_u8x16(0)),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::Port => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        2 => writer.write_display_with_no_filter(self.to_port(0) as u32),
                        _ => self.write_char8_default_to(&mut writer),
                    },
                    FieldFormat::IPAddress | FieldFormat::IPAddressObsolete => {
                        match self.bytes.len() {
                            0 => writer.write_str_with_no_filter("null"),
                            4 => writer.write_ipv4(*self.to_u8x4(0)),
                            16 => writer.write_ipv6(self.to_u8x16(0)),
                            _ => self.write_char8_default_to(&mut writer),
                        }
                    }
                    _ => self.write_char8_default_to(&mut writer),
                }
            }
            _ => writer
                .write_fmt_with_no_filter(format_args!("Encoding[{}]", self.metadata.encoding())),
        };

        return result;
    }

    /// Interprets this as the beginning of an array of simple type.
    /// Converts the specified element of the array to a string and writes it to the writer.
    ///
    /// Requires `type_size != 0` (can only format fixed-length types).
    ///
    /// Requires `index <= bytes.len() / type_size`.
    ///
    /// The element is formatted as described for `write_scalar_to`.
    pub fn write_simple_element_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        index: usize,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size != 0);
        debug_assert!(index <= 65535);
        debug_assert!((index + 1) * self.metadata.type_size as usize <= self.bytes.len());

        let mut writer = writers::ValueWriter::new(writer, convert_options); // Shadow
        let result = match self.metadata.encoding() {
            FieldEncoding::Value8 => self.write_value8_to(&mut writer, index),
            FieldEncoding::Value16 => self.write_value16_to(&mut writer, index),
            FieldEncoding::Value32 => self.write_value32_to(&mut writer, index),
            FieldEncoding::Value64 => self.write_value64_to(&mut writer, index),
            FieldEncoding::Value128 => self.write_value128_to(&mut writer, index),
            _ => writer
                .write_fmt_with_no_filter(format_args!("Encoding[{}]", self.metadata.encoding())),
        };

        return result;
    }

    /// Interprets this as the beginning of an array of simple type.
    /// Converts this to a comma-separated list of items and writes it to the writer.
    ///
    /// Each array element is formatted as described for `write_scalar_to`.
    ///
    /// If this is an array-begin or array-end of complex type, this will simply write
    /// `Array[N]`, where `N` is the number of elements in the array.
    pub fn write_simple_array_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size != 0);

        let separator = if convert_options.has_flag(PerfConvertOptions::Space) {
            ", "
        } else {
            ","
        };

        let mut writer = writers::ValueWriter::new(writer, convert_options); // Shadow
        match self.metadata.encoding() {
            FieldEncoding::Value8 => {
                let count = self.bytes.len();
                for i in 0..count {
                    if i > 0 {
                        writer.write_str_with_no_filter(separator)?;
                    }
                    self.write_value8_to(&mut writer, i)?;
                }
            }
            FieldEncoding::Value16 => {
                let count = self.bytes.len() / 2;
                for i in 0..count {
                    if i > 0 {
                        writer.write_str_with_no_filter(separator)?;
                    }
                    self.write_value16_to(&mut writer, i)?;
                }
            }
            FieldEncoding::Value32 => {
                let count = self.bytes.len() / 4;
                for i in 0..count {
                    if i > 0 {
                        writer.write_str_with_no_filter(separator)?;
                    }
                    self.write_value32_to(&mut writer, i)?;
                }
            }
            FieldEncoding::Value64 => {
                let count = self.bytes.len() / 8;
                for i in 0..count {
                    if i > 0 {
                        writer.write_str_with_no_filter(separator)?;
                    }
                    self.write_value64_to(&mut writer, i)?;
                }
            }
            FieldEncoding::Value128 => {
                let count = self.bytes.len() / 16;
                for i in 0..count {
                    if i > 0 {
                        writer.write_str_with_no_filter(separator)?;
                    }
                    self.write_value128_to(&mut writer, i)?;
                }
            }
            _ => writer
                .write_fmt_with_no_filter(format_args!("Encoding[{}]", self.metadata.encoding()))?,
        }

        return Ok(());
    }

    /// Returns a display object that can be used with `write!` or `format!` macros
    /// to format the item's value as JSON. The output is the same as with
    /// [`PerfItemValue::write_json_to`].
    ///
    /// For example, `write!(str, "{}", value.json_display())` might generate a string
    /// like `53`, `true`, `"Hello"`, or `[1, 2, 3]`.
    pub fn json_display(&self) -> display::PerfItemValueJsonDisplay {
        return display::PerfItemValueJsonDisplay::new(self);
    }

    /// Writes a JSON representation of this value to the writer.
    ///
    /// If this value is a scalar, this behaves like `write_json_scalar_to`.
    ///
    /// If thie value is an array, this behaves like `write_json_simple_array_to`.
    pub fn write_json_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        let result = if self.metadata.is_scalar() {
            self.write_json_scalar_to(writer, convert_options)
        } else {
            self.write_json_simple_array_to(writer, convert_options)
        };

        return result;
    }

    /// Interprets this as a scalar and writes a JSON representation to the writer.
    ///
    /// If this value is a struct, the value will be written as `{}`.
    /// Structs need to be processed by the enumerator.
    pub fn write_json_scalar_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        let mut writer = writers::ValueWriter::new(writer, convert_options);
        return self.write_json_scalar_to_impl(&mut writer);
    }

    pub(crate) fn write_json_scalar_to_impl<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size as usize <= self.bytes.len());

        let result = match self.metadata.encoding() {
            FieldEncoding::Invalid => writer.write_str_with_no_filter("null"),
            FieldEncoding::Struct => writer.write_str_with_no_filter("{}"),
            FieldEncoding::Value8 => self.write_json_value8_to(writer, 0),
            FieldEncoding::Value16 => self.write_json_value16_to(writer, 0),
            FieldEncoding::Value32 => self.write_json_value32_to(writer, 0),
            FieldEncoding::Value64 => self.write_json_value64_to(writer, 0),
            FieldEncoding::Value128 => self.write_json_value128_to(writer, 0),
            FieldEncoding::ZStringChar8 => {
                self.write_json_scalar_string_to(writer, PerfTextEncoding::Utf8)
            }
            FieldEncoding::ZStringChar16 | FieldEncoding::StringLength16Char16 => self
                .write_json_scalar_string_to(
                    writer,
                    if self.metadata.byte_reader.source_big_endian() {
                        PerfTextEncoding::Utf16BE
                    } else {
                        PerfTextEncoding::Utf16LE
                    },
                ),
            FieldEncoding::ZStringChar32 | FieldEncoding::StringLength16Char32 => self
                .write_json_scalar_string_to(
                    writer,
                    if self.metadata.byte_reader.source_big_endian() {
                        PerfTextEncoding::Utf32BE
                    } else {
                        PerfTextEncoding::Utf32LE
                    },
                ),
            FieldEncoding::BinaryLength16Char8 | FieldEncoding::StringLength16Char8 => {
                match self.metadata.format() {
                    FieldFormat::UnsignedInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_display_with_no_filter(self.to_u8(0) as u32),
                        2 => writer.write_display_with_no_filter(self.to_u16(0) as u32),
                        4 => writer.write_display_with_no_filter(self.to_u32(0)),
                        8 => writer.write_display_with_no_filter(self.to_u64(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::SignedInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_display_with_no_filter(self.to_i8(0) as i32),
                        2 => writer.write_display_with_no_filter(self.to_i16(0) as i32),
                        4 => writer.write_display_with_no_filter(self.to_i32(0)),
                        8 => writer.write_display_with_no_filter(self.to_i64(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::HexInt => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_json_hex32(self.to_u8(0) as u32),
                        2 => writer.write_json_hex32(self.to_u16(0) as u32),
                        4 => writer.write_json_hex32(self.to_u32(0)),
                        8 => writer.write_json_hex64(self.to_u64(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Errno => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_json_errno(self.to_u32(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Pid => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_display_with_no_filter(self.to_i32(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Time => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_json_time64(self.to_time32(0) as i64),
                        8 => writer.write_json_time64(self.to_time64(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Boolean => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        1 => writer.write_json_bool(self.to_u8(0) as u32),
                        2 => writer.write_json_bool(self.to_u16(0) as u32),
                        4 => writer.write_json_bool(self.to_u32(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Float => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        4 => writer.write_json_float32(self.to_f32(0)),
                        8 => writer.write_json_float64(self.to_f64(0)),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::HexBytes => writer.write_quoted(|w| w.write_hexbytes(self.bytes)),
                    FieldFormat::String8 => {
                        writer.write_quoted(|w| w.write_latin1_with_json_escape(self.bytes))
                    }
                    FieldFormat::StringUtf => {
                        writer.write_quoted(|w| w.write_utf8_with_json_escape(self.bytes))
                    }
                    FieldFormat::StringUtfBom
                    | FieldFormat::StringXml
                    | FieldFormat::StringJson => {
                        if let (Some(bom_encoding), bom_len) =
                            PerfTextEncoding::from_bom(self.bytes)
                        {
                            writer.write_quoted(|w| {
                                w.write_with_json_escape(
                                    &self.bytes[bom_len as usize..],
                                    bom_encoding,
                                )
                            })
                        } else {
                            writer.write_quoted(|w| w.write_utf8_with_json_escape(self.bytes))
                        }
                    }
                    FieldFormat::Uuid => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        16 => writer.write_quoted(|w| w.write_uuid(self.to_u8x16(0))),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::Port => match self.bytes.len() {
                        0 => writer.write_str_with_no_filter("null"),
                        2 => writer.write_display_with_no_filter(self.to_port(0) as u32),
                        _ => self.write_json_char8_default_to(writer),
                    },
                    FieldFormat::IPAddress | FieldFormat::IPAddressObsolete => {
                        match self.bytes.len() {
                            0 => writer.write_str_with_no_filter("null"),
                            4 => writer.write_quoted(|w| w.write_ipv4(*self.to_u8x4(0))),
                            16 => writer.write_quoted(|w| w.write_ipv6(self.to_u8x16(0))),
                            _ => self.write_json_char8_default_to(writer),
                        }
                    }
                    _ => self.write_json_char8_default_to(writer),
                }
            }
            _ => writer.write_fmt_with_no_filter(format_args!(
                "\"Encoding[{}]\"",
                self.metadata.encoding()
            )),
        };

        return result;
    }

    /// Interprets this as the beginning of an array of simple type.
    /// Converts the specified element of the array to JSON and writes it to the writer.
    ///
    /// Requires `type_size != 0` (can only format fixed-length types).
    ///
    /// Requires `index <= bytes.len() / type_size`.
    ///
    /// The element is formatted as described for `write_json_scalar_to`.
    pub fn write_json_simple_element_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        index: usize,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size != 0);
        debug_assert!(index <= 65535);
        debug_assert!((index + 1) * self.metadata.type_size as usize <= self.bytes.len());

        let mut writer = writers::ValueWriter::new(writer, convert_options); // Shadow
        let result = match self.metadata.encoding() {
            FieldEncoding::Value8 => self.write_json_value8_to(&mut writer, index),
            FieldEncoding::Value16 => self.write_json_value16_to(&mut writer, index),
            FieldEncoding::Value32 => self.write_json_value32_to(&mut writer, index),
            FieldEncoding::Value64 => self.write_json_value64_to(&mut writer, index),
            FieldEncoding::Value128 => self.write_json_value128_to(&mut writer, index),
            _ => writer.write_fmt_with_no_filter(format_args!(
                "\"Encoding[{}]\"",
                self.metadata.encoding()
            )),
        };

        return result;
    }

    /// Interprets this as the beginning of an array of simple type.
    /// Converts this to a JSON array and writes it to the writer.
    ///
    /// Each array element is formatted as described for `write_json_scalar_to`.
    ///
    /// If this value is an array of complex type, the value will be written as `[]`.
    /// Complex arrays need to be processed by the enumerator.
    pub fn write_json_simple_array_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut W,
        convert_options: PerfConvertOptions,
    ) -> fmt::Result {
        let mut json = writers::JsonWriter::new(writer, convert_options, false);
        return self.write_json_simple_array_to_impl(&mut json);
    }

    pub(crate) fn write_json_simple_array_to_impl<W: fmt::Write + ?Sized>(
        &self,
        json: &mut writers::JsonWriter<W>,
    ) -> fmt::Result {
        debug_assert!(self.metadata.type_size != 0);

        json.write_array_begin()?;
        match self.metadata.encoding() {
            FieldEncoding::Value8 => {
                let count = self.bytes.len();
                for i in 0..count {
                    json.write_value(|w| self.write_json_value8_to(w, i))?;
                }
            }
            FieldEncoding::Value16 => {
                let count = self.bytes.len() / 2;
                for i in 0..count {
                    json.write_value(|w| self.write_json_value16_to(w, i))?;
                }
            }
            FieldEncoding::Value32 => {
                let count = self.bytes.len() / 4;
                for i in 0..count {
                    json.write_value(|w| self.write_json_value32_to(w, i))?;
                }
            }
            FieldEncoding::Value64 => {
                let count = self.bytes.len() / 8;
                for i in 0..count {
                    json.write_value(|w| self.write_json_value64_to(w, i))?;
                }
            }
            FieldEncoding::Value128 => {
                let count = self.bytes.len() / 16;
                for i in 0..count {
                    json.write_value(|w| self.write_json_value128_to(w, i))?;
                }
            }
            _ => json.write_value(|w| {
                w.write_fmt_with_no_filter(format_args!(
                    "\"Encoding[{}]\"",
                    self.metadata.encoding()
                ))
            })?,
        }

        return json.write_array_end();
    }

    fn write_value8_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt => writer.write_display_with_no_filter(self.to_i8(index) as i32),
            FieldFormat::HexInt => writer.write_hex32(self.to_u8(index) as u32),
            FieldFormat::Boolean => writer.write_bool(self.to_u8(index) as u32),
            FieldFormat::HexBytes => writer.write_hexbytes(self.to_u8x1(index)),
            FieldFormat::String8 => {
                writer.write_latin1_with_control_chars_filter(self.to_u8x1(index))
            }
            _ => writer.write_display_with_no_filter(self.to_u8(index) as u32), // Default, UnsignedInt
        };
    }

    fn write_json_value8_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt => writer.write_display_with_no_filter(self.to_i8(index) as i32),
            FieldFormat::HexInt => writer.write_json_hex32(self.to_u8(index) as u32),
            FieldFormat::Boolean => writer.write_json_bool(self.to_u8(index) as u32),
            FieldFormat::HexBytes => writer.write_quoted(|w| w.write_hexbytes(self.to_u8x1(index))),
            FieldFormat::String8 => {
                writer.write_quoted(|w| w.write_latin1_with_json_escape(self.to_u8x1(index)))
            }
            _ => writer.write_display_with_no_filter(self.to_u8(index) as u32), // Default, UnsignedInt
        };
    }

    fn write_value16_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt => {
                writer.write_display_with_no_filter(self.to_i16(index) as i32)
            }
            FieldFormat::HexInt => writer.write_hex32(self.to_u16(index) as u32),
            FieldFormat::Boolean => writer.write_bool(self.to_u16(index) as u32),
            FieldFormat::HexBytes => writer.write_hexbytes(self.to_u8x2(index)),
            FieldFormat::StringUtf => {
                writer.write_char32_with_control_chars_filter(self.to_u16(index) as u32)
            }
            FieldFormat::Port => writer.write_display_with_no_filter(self.to_port(index) as u32),
            _ => writer.write_display_with_no_filter(self.to_u16(index) as u32), // Default, UnsignedInt
        };
    }

    fn write_json_value16_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt => {
                writer.write_display_with_no_filter(self.to_i16(index) as i32)
            }
            FieldFormat::HexInt => writer.write_json_hex32(self.to_u16(index) as u32),
            FieldFormat::Boolean => writer.write_json_bool(self.to_u16(index) as u32),
            FieldFormat::HexBytes => writer.write_quoted(|w| w.write_hexbytes(self.to_u8x2(index))),
            FieldFormat::StringUtf => {
                writer.write_quoted(|w| w.write_char32_with_json_escape(self.to_u16(index) as u32))
            }
            FieldFormat::Port => writer.write_display_with_no_filter(self.to_port(index) as u32),
            _ => writer.write_display_with_no_filter(self.to_u16(index) as u32), // Default, UnsignedInt
        };
    }

    fn write_value32_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt | FieldFormat::Pid => {
                writer.write_display_with_no_filter(self.to_i32(index))
            }
            FieldFormat::HexInt => writer.write_hex32(self.to_u32(index)),
            FieldFormat::Errno => writer.write_errno(self.to_u32(index)),
            FieldFormat::Time => writer.write_time64(self.to_time32(index) as i64),
            FieldFormat::Boolean => writer.write_bool(self.to_u32(index)),
            FieldFormat::Float => writer.write_float32(self.to_f32(index)),
            FieldFormat::HexBytes => writer.write_hexbytes(self.to_u8x4(index)),
            FieldFormat::StringUtf => {
                writer.write_char32_with_control_chars_filter(self.to_u32(index))
            }
            FieldFormat::IPv4 => writer.write_ipv4(*self.to_u8x4(index)),
            _ => writer.write_display_with_no_filter(self.to_u32(index)), // Default, UnsignedInt
        };
    }

    fn write_json_value32_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt | FieldFormat::Pid => {
                writer.write_display_with_no_filter(self.to_i32(index))
            }
            FieldFormat::HexInt => writer.write_quoted(|w| w.write_hex32(self.to_u32(index))),
            FieldFormat::Errno => writer.write_json_errno(self.to_u32(index)),
            FieldFormat::Time => writer.write_json_time64(self.to_time32(index) as i64),
            FieldFormat::Boolean => writer.write_json_bool(self.to_u32(index)),
            FieldFormat::Float => writer.write_json_float32(self.to_f32(index)),
            FieldFormat::HexBytes => writer.write_quoted(|w| w.write_hexbytes(self.to_u8x4(index))),
            FieldFormat::StringUtf => {
                writer.write_quoted(|w| w.write_char32_with_json_escape(self.to_u32(index)))
            }
            FieldFormat::IPv4 => writer.write_quoted(|w| w.write_ipv4(*self.to_u8x4(index))),
            _ => writer.write_display_with_no_filter(self.to_u32(index)), // Default, UnsignedInt
        };
    }

    fn write_value64_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt | FieldFormat::Pid => {
                writer.write_display_with_no_filter(self.to_i64(index))
            }
            FieldFormat::HexInt => writer.write_hex64(self.to_u64(index)),
            FieldFormat::Time => writer.write_time64(self.to_time64(index)),
            FieldFormat::Float => writer.write_float64(self.to_f64(index)),
            FieldFormat::HexBytes => writer.write_hexbytes(self.to_u8x8(index)),
            _ => writer.write_display_with_no_filter(self.to_u64(index)), // Default, UnsignedInt
        };
    }

    fn write_json_value64_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::SignedInt | FieldFormat::Pid => {
                writer.write_display_with_no_filter(self.to_i64(index))
            }
            FieldFormat::HexInt => writer.write_json_hex64(self.to_u64(index)),
            FieldFormat::Time => writer.write_json_time64(self.to_time64(index)),
            FieldFormat::Float => writer.write_json_float64(self.to_f64(index)),
            FieldFormat::HexBytes => writer.write_quoted(|w| w.write_hexbytes(self.to_u8x8(index))),
            _ => writer.write_display_with_no_filter(self.to_u64(index)), // Default, UnsignedInt
        };
    }

    fn write_value128_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::Uuid => writer.write_uuid(self.to_u8x16(index)),
            FieldFormat::IPv6 => writer.write_ipv6(self.to_u8x16(index)),
            _ => writer.write_hexbytes(self.to_u8x16(index)), // Default, HexBytes
        };
    }

    fn write_json_value128_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        index: usize,
    ) -> fmt::Result {
        return match self.metadata.format() {
            FieldFormat::Uuid => writer.write_quoted(|w| w.write_uuid(self.to_u8x16(index))),
            FieldFormat::IPv6 => writer.write_quoted(|w| w.write_ipv6(self.to_u8x16(index))),
            _ => writer.write_quoted(|w| w.write_hexbytes(self.to_u8x16(index))), // Default, HexBytes
        };
    }

    fn write_char8_default_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
    ) -> fmt::Result {
        return if self.metadata.encoding() == FieldEncoding::BinaryLength16Char8 {
            writer.write_hexbytes(self.bytes)
        } else {
            writer.write_utf8_with_control_chars_filter(self.bytes)
        };
    }

    fn write_scalar_string_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        default_encoding: PerfTextEncoding,
    ) -> fmt::Result {
        let mut bytes = self.bytes;
        let mut encoding = default_encoding;

        match self.metadata.format {
            FieldFormat::HexBytes => return writer.write_hexbytes(bytes),

            FieldFormat::String8 => {
                return writer.write_latin1_with_control_chars_filter(bytes);
            }

            FieldFormat::StringUtfBom | FieldFormat::StringXml | FieldFormat::StringJson => {
                if let (Some(bom_encoding), bom_len) = PerfTextEncoding::from_bom(bytes) {
                    bytes = &bytes[bom_len as usize..];
                    encoding = bom_encoding;
                }
            }

            _ => {}
        }

        return writer.write_with_control_chars_filter(bytes, encoding);
    }

    fn write_json_char8_default_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
    ) -> fmt::Result {
        return if self.metadata.encoding() == FieldEncoding::BinaryLength16Char8 {
            writer.write_quoted(|w| w.write_hexbytes(self.bytes))
        } else {
            writer.write_quoted(|w| w.write_utf8_with_json_escape(self.bytes))
        };
    }

    fn write_json_scalar_string_to<W: fmt::Write + ?Sized>(
        &self,
        writer: &mut writers::ValueWriter<W>,
        default_encoding: PerfTextEncoding,
    ) -> fmt::Result {
        let mut bytes = self.bytes;
        let mut encoding = default_encoding;
        let format = self.metadata.format;

        match format {
            FieldFormat::HexBytes => return writer.write_quoted(|w| w.write_hexbytes(bytes)),

            FieldFormat::String8 => {
                return writer.write_quoted(|w| w.write_latin1_with_json_escape(bytes))
            }

            FieldFormat::StringUtfBom | FieldFormat::StringXml | FieldFormat::StringJson => {
                if let (Some(bom_encoding), bom_len) = PerfTextEncoding::from_bom(bytes) {
                    bytes = &bytes[bom_len as usize..];
                    encoding = bom_encoding;
                }
            }

            _ => {}
        }

        return writer.write_quoted(|w| w.write_with_json_escape(bytes, encoding));
    }
}