qubit-codec-binary 0.9.4

Buffer-oriented binary codecs for Rust
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
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

use core::convert::Infallible;
use core::marker::PhantomData;
use core::num::NonZeroUsize;

use qubit_codec::BigEndian;
use qubit_codec::ByteOrderSpec;
use qubit_codec::Codec;
use qubit_codec::DecodeFailure;
use qubit_codec::LittleEndian;
use qubit_codec::NativeEndian;
use qubit_utils::UncheckedSlice;
use qubit_utils::nonzero;

/// Type-level unchecked binary codec for one scalar type and one byte order.
///
/// `BinaryCodec` is intentionally a zero-sized codec type. It exposes
/// type-level unchecked helpers for direct hot-path use and also implements
/// [`Codec`] for generic codec pipelines. Callers must validate buffer lengths
/// before entering the hot path.
///
/// # Type Parameters
///
/// - `T`: Scalar value type to decode from bytes and encode into bytes.
/// - `O`: Type-level byte order marker. Multi-byte scalar implementations use
///   [`BigEndian`], [`LittleEndian`], or [`NativeEndian`]. Prefer big- or
///   little-endian formats for persistent and cross-platform data;
///   native-endian encoding is only suitable for platform-local data. Single-
///   byte scalar implementations accept any [`ByteOrderSpec`] marker because
///   byte order does not affect one-byte values.
///
/// # Examples
///
/// ```
/// use qubit_codec::BigEndian;
/// use qubit_codec_binary::BinaryCodec;
///
/// let mut output = [0_u8; BinaryCodec::<u32, BigEndian>::MAX_ENCODE_UNITS_PER_VALUE];
/// let written = unsafe {
///     BinaryCodec::<u32, BigEndian>::encode(0x0102_0304, &mut output, 0)
/// };
/// assert_eq!(4, written);
/// assert_eq!([1, 2, 3, 4], output);
///
/// let (decoded, consumed) = unsafe {
///     BinaryCodec::<u32, BigEndian>::decode(&output, 0)
/// };
/// assert_eq!(0x0102_0304, decoded);
/// assert_eq!(4, consumed.get());
/// ```
///
/// Byte-order markers are also required for single-byte codecs, even though
/// the marker has no effect on the encoded bytes:
///
/// ```compile_fail
/// use qubit_codec_binary::{BinaryCodec, Strict};
///
/// let mut output = [0_u8; 1];
/// let _ = unsafe { BinaryCodec::<u8, Strict>::encode(1, &mut output, 0) };
/// ```
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct BinaryCodec<T, O> {
    /// Associates the value and byte-order types without storing either one.
    marker: PhantomData<fn() -> (T, O)>,
}

impl<O> BinaryCodec<u8, O>
where
    O: ByteOrderSpec,
{
    /// Minimum number of bytes required to encode or decode this type.
    pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

    /// Maximum number of bytes emitted when encoding this type.
    pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

    /// Maximum number of bytes consumed when decoding this type.
    pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

    /// Decodes a value from `input` starting at `input_index` without bounds
    /// checks.
    ///
    /// # Parameters
    ///
    /// - `input`: Source byte buffer.
    /// - `input_index`: Start index in `input`.
    ///
    /// # Returns
    ///
    /// Returns the decoded value and the non-zero number of consumed bytes.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input.as_ptr().add(input_index)` is
    /// valid to read [`Self::MIN_UNITS_PER_VALUE`] bytes.
    #[must_use]
    #[inline]
    pub unsafe fn decode(input: &[u8], input_index: usize) -> (u8, NonZeroUsize) {
        debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());

        // SAFETY: The caller guarantees that the indexed byte is readable.
        (
            unsafe { UncheckedSlice::read(input, input_index) },
            nonzero(Self::MIN_UNITS_PER_VALUE),
        )
    }

    /// Encodes `value` into `output` starting at `output_index` without bounds
    /// checks.
    ///
    /// # Parameters
    ///
    /// - `value`: Value to encode.
    /// - `output`: Destination byte buffer.
    /// - `output_index`: Start index in `output`.
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written, equal to
    /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
    /// The next output position is `output_index + written`, where `written`
    /// is the returned byte count.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `output.as_mut_ptr().add(output_index)`
    /// is valid to write [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes.
    #[inline]
    pub unsafe fn encode(value: u8, output: &mut [u8], output_index: usize) -> usize {
        debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());

        // SAFETY: The caller guarantees that the indexed byte is writable.
        unsafe {
            UncheckedSlice::write(output, output_index, value);
        }
        Self::MAX_ENCODE_UNITS_PER_VALUE
    }
}

impl<O> Codec for BinaryCodec<u8, O>
where
    O: ByteOrderSpec,
{
    /// Logical unsigned byte decoded from or encoded into the unit buffer.
    type Value = u8;
    /// One byte used as both the input and output unit.
    type Unit = u8;
    /// Decoding a byte cannot fail after the caller satisfies the safety
    /// contract.
    type DecodeError = Infallible;
    /// Encoding a byte cannot fail after the caller satisfies the safety
    /// contract.
    type EncodeError = Infallible;

    const MIN_UNITS_PER_VALUE: usize = 1;
    const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;

    const MAX_DECODE_UNITS_PER_VALUE: usize = 1;

    /// Decodes one byte from the supplied input slice.
    ///
    /// # Parameters
    ///
    /// - `input`: Source byte buffer.
    /// - `input_index`: Index of the byte to decode.
    ///
    /// # Returns
    ///
    /// Returns the decoded byte and the number of bytes consumed.
    ///
    /// # Safety
    ///
    /// The caller must satisfy the `Codec::decode` buffer and index
    /// preconditions; the input must contain the byte at `input_index`.
    #[inline]
    unsafe fn decode(
        &mut self,
        input: &[u8],
        input_index: usize,
    ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
        // SAFETY: The caller upholds the `Codec::decode` contract.
        Ok(unsafe { Self::decode(input, input_index) })
    }

    /// Encodes one byte into the supplied output slice.
    ///
    /// # Parameters
    ///
    /// - `value`: Byte value to encode.
    /// - `output`: Destination byte buffer.
    /// - `output_index`: Index where the encoded byte is written.
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written.
    ///
    /// # Safety
    ///
    /// The caller must satisfy the `Codec::encode` buffer and index
    /// preconditions; the output must have space at `output_index`.
    #[inline]
    unsafe fn encode(
        &mut self,
        value: &u8,
        output: &mut [u8],
        output_index: usize,
    ) -> Result<usize, Self::EncodeError> {
        // SAFETY: The caller upholds the `Codec::encode` contract.
        unsafe {
            Self::encode(*value, output, output_index);
        }
        Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
    }
}

impl<O> BinaryCodec<i8, O>
where
    O: ByteOrderSpec,
{
    /// Minimum number of bytes required to encode or decode this type.
    pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

    /// Maximum number of bytes emitted when encoding this type.
    pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

    /// Maximum number of bytes consumed when decoding this type.
    pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

    /// Decodes a value from `input` starting at `input_index` without bounds
    /// checks.
    ///
    /// # Parameters
    ///
    /// - `input`: Source byte buffer.
    /// - `input_index`: Start index in `input`.
    ///
    /// # Returns
    ///
    /// Returns the decoded value and the non-zero number of consumed bytes.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input.as_ptr().add(input_index)` is
    /// valid to read [`Self::MIN_UNITS_PER_VALUE`] bytes.
    #[must_use]
    #[inline]
    pub unsafe fn decode(input: &[u8], input_index: usize) -> (i8, NonZeroUsize) {
        debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());

        // SAFETY: The caller guarantees that the indexed byte is readable.
        (
            unsafe { UncheckedSlice::read(input, input_index) } as i8,
            nonzero(Self::MIN_UNITS_PER_VALUE),
        )
    }

    /// Encodes `value` into `output` starting at `output_index` without bounds
    /// checks.
    ///
    /// # Parameters
    ///
    /// - `value`: Value to encode.
    /// - `output`: Destination byte buffer.
    /// - `output_index`: Start index in `output`.
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written, equal to
    /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
    /// The next output position is `output_index + written`, where `written`
    /// is the returned byte count.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `output.as_mut_ptr().add(output_index)`
    /// is valid to write [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes.
    #[inline]
    pub unsafe fn encode(value: i8, output: &mut [u8], output_index: usize) -> usize {
        debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());

        // SAFETY: The caller guarantees that the indexed byte is writable.
        unsafe {
            UncheckedSlice::write(output, output_index, value as u8);
        }
        Self::MAX_ENCODE_UNITS_PER_VALUE
    }
}

impl<O> Codec for BinaryCodec<i8, O>
where
    O: ByteOrderSpec,
{
    /// Logical signed byte decoded from or encoded into the unit buffer.
    type Value = i8;
    /// One byte used as both the input and output unit.
    type Unit = u8;
    /// Decoding a byte cannot fail after the caller satisfies the safety
    /// contract.
    type DecodeError = Infallible;
    /// Encoding a byte cannot fail after the caller satisfies the safety
    /// contract.
    type EncodeError = Infallible;

    const MIN_UNITS_PER_VALUE: usize = 1;
    const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;

    const MAX_DECODE_UNITS_PER_VALUE: usize = 1;

    /// Decodes one signed byte from the supplied input slice.
    ///
    /// # Parameters
    ///
    /// - `input`: Source byte buffer.
    /// - `input_index`: Index of the byte to decode.
    ///
    /// # Returns
    ///
    /// Returns the decoded signed byte and the number of bytes consumed.
    ///
    /// # Safety
    ///
    /// The caller must satisfy the `Codec::decode` buffer and index
    /// preconditions; the input must contain the byte at `input_index`.
    #[inline]
    unsafe fn decode(
        &mut self,
        input: &[u8],
        input_index: usize,
    ) -> Result<(i8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
        // SAFETY: The caller upholds the `Codec::decode` contract.
        Ok(unsafe { Self::decode(input, input_index) })
    }

    /// Encodes one signed byte into the supplied output slice.
    ///
    /// # Parameters
    ///
    /// - `value`: Signed byte value to encode.
    /// - `output`: Destination byte buffer.
    /// - `output_index`: Index where the encoded byte is written.
    ///
    /// # Returns
    ///
    /// Returns the number of bytes written.
    ///
    /// # Safety
    ///
    /// The caller must satisfy the `Codec::encode` buffer and index
    /// preconditions; the output must have space at `output_index`.
    #[inline]
    unsafe fn encode(
        &mut self,
        value: &i8,
        output: &mut [u8],
        output_index: usize,
    ) -> Result<usize, Self::EncodeError> {
        // SAFETY: The caller upholds the `Codec::encode` contract.
        unsafe {
            Self::encode(*value, output, output_index);
        }
        Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
    }
}

macro_rules! impl_integer_binary_codec {
    ($ty:ty, $len:expr) => {
        impl BinaryCodec<$ty, BigEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a value from `input` starting at `input_index` without
            /// bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Start byte index in `input`.
            ///
            /// # Returns
            ///
            /// Returns the decoded value and the non-zero number of consumed
            /// bytes.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
            ///   for reading.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(
                    input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
                    "BinaryCodec decode requires sufficient readable bytes",
                );

                // SAFETY:
                // The caller guarantees that the readable range is fully
                // in-bounds. This unaligned helper handles byte-aligned load.
                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };

                (<$ty>::from_be(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
            }

            /// Encodes `value` into `output` starting at `output_index`
            /// without bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `value`: Value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Start byte index in `output`.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written, equal to
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
            /// The next output position is `output_index + written`, where `written`
            /// is the returned byte count.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
            /// - `output[output_index..output_index +
            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);

                let raw = value.to_be();

                // SAFETY:
                // The caller guarantees that the writable range is fully
                // in-bounds. This unaligned helper handles byte-aligned store.
                unsafe {
                    UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
                }
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, BigEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;

            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;

            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                // SAFETY: The caller upholds the `Codec::decode`
                // contract.
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                // SAFETY: The caller upholds the `Codec::encode`
                // contract.
                unsafe {
                    Self::encode(*value, output, output_index);
                }
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }

        impl BinaryCodec<$ty, LittleEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a value from `input` starting at `input_index` without
            /// bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Start byte index in `input`.
            ///
            /// # Returns
            ///
            /// Returns the decoded value and the non-zero number of consumed
            /// bytes.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
            ///   for reading.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);

                // SAFETY:
                // The caller guarantees that the readable range is fully
                // in-bounds. This unaligned helper handles byte-aligned load.
                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };

                (<$ty>::from_le(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
            }

            /// Encodes `value` into `output` starting at `output_index`
            /// without bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `value`: Value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Start byte index in `output`.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written, equal to
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
            /// The next output position is `output_index + written`, where `written`
            /// is the returned byte count.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
            /// - `output[output_index..output_index +
            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(
                    output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
                    "BinaryCodec encode requires sufficient writable bytes",
                );

                let raw = value.to_le();

                // SAFETY:
                // The caller guarantees that the writable range is fully
                // in-bounds. This unaligned helper handles byte-aligned store.
                unsafe {
                    UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
                }
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, LittleEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;

            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;

            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                // SAFETY: The caller upholds the `Codec::decode`
                // contract.
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                // SAFETY: The caller upholds the `Codec::encode`
                // contract.
                unsafe {
                    Self::encode(*value, output, output_index);
                }
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }
    };
}

macro_rules! impl_float_binary_codec {
    ($ty:ty, $bits:ty, $len:expr) => {
        impl BinaryCodec<$ty, BigEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a value from `input` starting at `input_index` without
            /// bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Start byte index in `input`.
            ///
            /// # Returns
            ///
            /// Returns the decoded floating-point value and the non-zero number
            /// of consumed bytes.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
            ///   for reading.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(
                    input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
                    "BinaryCodec decode requires sufficient readable bytes",
                );

                // SAFETY:
                // The caller guarantees that the readable range is fully
                // in-bounds. This unaligned helper handles byte-aligned load.
                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };

                (
                    <$ty>::from_bits(<$bits>::from_be(raw)),
                    nonzero(Self::MIN_UNITS_PER_VALUE),
                )
            }

            /// Encodes `value` into `output` starting at `output_index`
            /// without bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `value`: Floating-point value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Start byte index in `output`.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written, equal to
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
            /// The next output position is `output_index + written`, where `written`
            /// is the returned byte count.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
            /// - `output[output_index..output_index +
            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);

                let raw = value.to_bits().to_be();

                // SAFETY:
                // The caller guarantees that the writable range is fully
                // in-bounds. This unaligned helper handles byte-aligned store.
                unsafe {
                    UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
                }
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, BigEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;

            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;

            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                // SAFETY: The caller upholds the `Codec::decode`
                // contract.
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                // SAFETY: The caller upholds the `Codec::encode`
                // contract.
                unsafe {
                    Self::encode(*value, output, output_index);
                }
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }

        impl BinaryCodec<$ty, LittleEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;

            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;

            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a value from `input` starting at `input_index` without
            /// bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Start byte index in `input`.
            ///
            /// # Returns
            ///
            /// Returns the decoded floating-point value and the non-zero number
            /// of consumed bytes.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
            ///   for reading.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);

                // SAFETY:
                // The caller guarantees that the readable range is fully
                // in-bounds. This unaligned helper handles byte-aligned load.
                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };

                (
                    <$ty>::from_bits(<$bits>::from_le(raw)),
                    nonzero(Self::MIN_UNITS_PER_VALUE),
                )
            }

            /// Encodes `value` into `output` starting at `output_index`
            /// without bounds checks.
            ///
            /// This function is intended for hot binary codec paths where the
            /// caller has already validated the buffer length externally.
            ///
            /// # Parameters
            ///
            /// - `value`: Floating-point value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Start byte index in `output`.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written, equal to
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
            /// The next output position is `output_index + written`, where `written`
            /// is the returned byte count.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that:
            ///
            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
            /// - `output[output_index..output_index +
            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(
                    output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
                    "BinaryCodec encode requires sufficient writable bytes",
                );

                let raw = value.to_bits().to_le();

                // SAFETY:
                // The caller guarantees that the writable range is fully
                // in-bounds. This unaligned helper handles byte-aligned store.
                unsafe {
                    UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
                }
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, LittleEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;

            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;

            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                // SAFETY: The caller upholds the `Codec::decode`
                // contract.
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                // SAFETY: The caller upholds the `Codec::encode`
                // contract.
                unsafe {
                    Self::encode(*value, output, output_index);
                }
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }
    };
}

macro_rules! impl_native_integer_binary_codec {
    ($ty:ty, $len:expr) => {
        impl BinaryCodec<$ty, NativeEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a native-endian value without bounds checks.
            ///
            /// # Parameters
            ///
            /// - `input`: Byte slice containing the encoded value.
            /// - `input_index`: Absolute byte index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded value and the fixed number of bytes read.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that at least
            /// [`Self::MIN_UNITS_PER_VALUE`] bytes are readable from `input_index`
            /// within `input`.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(
                    input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
                    "BinaryCodec decode requires sufficient readable bytes",
                );

                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
                (raw, nonzero(Self::MIN_UNITS_PER_VALUE))
            }

            /// Encodes a native-endian value without bounds checks.
            ///
            /// # Parameters
            ///
            /// - `value`: Value to encode.
            /// - `output`: Destination byte slice.
            /// - `output_index`: Absolute byte index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the fixed number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that at least
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes are writable from
            /// `output_index` within `output`.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);

                unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value) };
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, NativeEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;
            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                unsafe { Self::encode(*value, output, output_index) };
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }
    };
}

macro_rules! impl_native_float_binary_codec {
    ($ty:ty, $bits:ty, $len:expr) => {
        impl BinaryCodec<$ty, NativeEndian> {
            /// Minimum number of bytes required to encode or decode this type.
            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
            /// Maximum number of bytes emitted when encoding this type.
            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
            /// Maximum number of bytes consumed when decoding this type.
            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;

            /// Decodes a native-endian floating-point value without bounds
            /// checks.
            ///
            /// # Parameters
            ///
            /// - `input`: Byte slice containing the encoded value.
            /// - `input_index`: Absolute byte index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded floating-point value and fixed byte count.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that at least
            /// [`Self::MIN_UNITS_PER_VALUE`] bytes are readable from `input_index`
            /// within `input`.
            #[must_use]
            #[inline]
            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);

                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
                (<$ty>::from_bits(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
            }

            /// Encodes a native-endian floating-point value without bounds
            /// checks.
            ///
            /// # Parameters
            ///
            /// - `value`: Floating-point value to encode.
            /// - `output`: Destination byte slice.
            /// - `output_index`: Absolute byte index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the fixed number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must guarantee that at least
            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes are writable from
            /// `output_index` within `output`.
            #[inline]
            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
                debug_assert!(
                    output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
                    "BinaryCodec encode requires sufficient writable bytes",
                );

                unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value.to_bits()) };
                Self::MAX_ENCODE_UNITS_PER_VALUE
            }
        }

        impl Codec for BinaryCodec<$ty, NativeEndian> {
            /// The scalar value represented by this codec implementation.
            type Value = $ty;
            /// One byte of the encoded representation.
            type Unit = u8;
            /// Decoding cannot fail after the buffer preconditions are met.
            type DecodeError = Infallible;
            /// Encoding cannot fail after the buffer preconditions are met.
            type EncodeError = Infallible;
            const MIN_UNITS_PER_VALUE: usize = $len;
            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;

            /// Decodes one scalar value from the supplied input slice.
            ///
            /// # Parameters
            ///
            /// - `input`: Source byte buffer.
            /// - `input_index`: Index where decoding starts.
            ///
            /// # Returns
            ///
            /// Returns the decoded scalar and the number of bytes consumed.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::decode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn decode(
                &mut self,
                input: &[u8],
                input_index: usize,
            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
                Ok(unsafe { Self::decode(input, input_index) })
            }

            /// Encodes one scalar value into the supplied output slice.
            ///
            /// # Parameters
            ///
            /// - `value`: Scalar value to encode.
            /// - `output`: Destination byte buffer.
            /// - `output_index`: Index where encoding starts.
            ///
            /// # Returns
            ///
            /// Returns the number of bytes written.
            ///
            /// # Safety
            ///
            /// The caller must satisfy the `Codec::encode` buffer and index
            /// preconditions for this scalar's fixed encoded width.
            #[inline]
            unsafe fn encode(
                &mut self,
                value: &$ty,
                output: &mut [u8],
                output_index: usize,
            ) -> Result<usize, Self::EncodeError> {
                unsafe { Self::encode(*value, output, output_index) };
                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
            }
        }
    };
}

impl_integer_binary_codec!(u16, 2);
impl_integer_binary_codec!(u32, 4);
impl_integer_binary_codec!(u64, 8);
impl_integer_binary_codec!(u128, 16);
impl_integer_binary_codec!(i16, 2);
impl_integer_binary_codec!(i32, 4);
impl_integer_binary_codec!(i64, 8);
impl_integer_binary_codec!(i128, 16);
impl_float_binary_codec!(f32, u32, 4);
impl_float_binary_codec!(f64, u64, 8);
impl_native_integer_binary_codec!(u16, 2);
impl_native_integer_binary_codec!(u32, 4);
impl_native_integer_binary_codec!(u64, 8);
impl_native_integer_binary_codec!(u128, 16);
impl_native_integer_binary_codec!(i16, 2);
impl_native_integer_binary_codec!(i32, 4);
impl_native_integer_binary_codec!(i64, 8);
impl_native_integer_binary_codec!(i128, 16);
impl_native_float_binary_codec!(f32, u32, 4);
impl_native_float_binary_codec!(f64, u64, 8);