Skip to main content

qubit_codec_binary/codec/
binary_codec.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9use core::convert::Infallible;
10use core::marker::PhantomData;
11use core::num::NonZeroUsize;
12
13use qubit_codec::BigEndian;
14use qubit_codec::ByteOrderSpec;
15use qubit_codec::Codec;
16use qubit_codec::DecodeFailure;
17use qubit_codec::LittleEndian;
18use qubit_codec::NativeEndian;
19use qubit_utils::UncheckedSlice;
20use qubit_utils::nonzero;
21
22/// Type-level unchecked binary codec for one scalar type and one byte order.
23///
24/// `BinaryCodec` is intentionally a zero-sized codec type. It exposes
25/// type-level unchecked helpers for direct hot-path use and also implements
26/// [`Codec`] for generic codec pipelines. Callers must validate buffer lengths
27/// before entering the hot path.
28///
29/// # Type Parameters
30///
31/// - `T`: Scalar value type to decode from bytes and encode into bytes.
32/// - `O`: Type-level byte order marker. Multi-byte scalar implementations use
33///   [`BigEndian`], [`LittleEndian`], or [`NativeEndian`]. Prefer big- or
34///   little-endian formats for persistent and cross-platform data;
35///   native-endian encoding is only suitable for platform-local data. Single-
36///   byte scalar implementations accept any [`ByteOrderSpec`] marker because
37///   byte order does not affect one-byte values.
38///
39/// # Examples
40///
41/// ```
42/// use qubit_codec::BigEndian;
43/// use qubit_codec_binary::BinaryCodec;
44///
45/// let mut output = [0_u8; BinaryCodec::<u32, BigEndian>::MAX_ENCODE_UNITS_PER_VALUE];
46/// let written = unsafe {
47///     BinaryCodec::<u32, BigEndian>::encode(0x0102_0304, &mut output, 0)
48/// };
49/// assert_eq!(4, written);
50/// assert_eq!([1, 2, 3, 4], output);
51///
52/// let (decoded, consumed) = unsafe {
53///     BinaryCodec::<u32, BigEndian>::decode(&output, 0)
54/// };
55/// assert_eq!(0x0102_0304, decoded);
56/// assert_eq!(4, consumed.get());
57/// ```
58///
59/// Byte-order markers are also required for single-byte codecs, even though
60/// the marker has no effect on the encoded bytes:
61///
62/// ```compile_fail
63/// use qubit_codec_binary::{BinaryCodec, Strict};
64///
65/// let mut output = [0_u8; 1];
66/// let _ = unsafe { BinaryCodec::<u8, Strict>::encode(1, &mut output, 0) };
67/// ```
68#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
69pub struct BinaryCodec<T, O> {
70    /// Associates the value and byte-order types without storing either one.
71    marker: PhantomData<fn() -> (T, O)>,
72}
73
74impl<O> BinaryCodec<u8, O>
75where
76    O: ByteOrderSpec,
77{
78    /// Minimum number of bytes required to encode or decode this type.
79    pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
80
81    /// Maximum number of bytes emitted when encoding this type.
82    pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
83
84    /// Maximum number of bytes consumed when decoding this type.
85    pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
86
87    /// Decodes a value from `input` starting at `input_index` without bounds
88    /// checks.
89    ///
90    /// # Parameters
91    ///
92    /// - `input`: Source byte buffer.
93    /// - `input_index`: Start index in `input`.
94    ///
95    /// # Returns
96    ///
97    /// Returns the decoded value and the non-zero number of consumed bytes.
98    ///
99    /// # Safety
100    ///
101    /// The caller must guarantee that `input.as_ptr().add(input_index)` is
102    /// valid to read [`Self::MIN_UNITS_PER_VALUE`] bytes.
103    #[must_use]
104    #[inline]
105    pub unsafe fn decode(input: &[u8], input_index: usize) -> (u8, NonZeroUsize) {
106        debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
107
108        // SAFETY: The caller guarantees that the indexed byte is readable.
109        (
110            unsafe { UncheckedSlice::read(input, input_index) },
111            nonzero(Self::MIN_UNITS_PER_VALUE),
112        )
113    }
114
115    /// Encodes `value` into `output` starting at `output_index` without bounds
116    /// checks.
117    ///
118    /// # Parameters
119    ///
120    /// - `value`: Value to encode.
121    /// - `output`: Destination byte buffer.
122    /// - `output_index`: Start index in `output`.
123    ///
124    /// # Returns
125    ///
126    /// Returns the number of bytes written, equal to
127    /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
128    /// The next output position is `output_index + written`, where `written`
129    /// is the returned byte count.
130    ///
131    /// # Safety
132    ///
133    /// The caller must guarantee that `output.as_mut_ptr().add(output_index)`
134    /// is valid to write [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes.
135    #[inline]
136    pub unsafe fn encode(value: u8, output: &mut [u8], output_index: usize) -> usize {
137        debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());
138
139        // SAFETY: The caller guarantees that the indexed byte is writable.
140        unsafe {
141            UncheckedSlice::write(output, output_index, value);
142        }
143        Self::MAX_ENCODE_UNITS_PER_VALUE
144    }
145}
146
147impl<O> Codec for BinaryCodec<u8, O>
148where
149    O: ByteOrderSpec,
150{
151    /// Logical unsigned byte decoded from or encoded into the unit buffer.
152    type Value = u8;
153    /// One byte used as both the input and output unit.
154    type Unit = u8;
155    /// Decoding a byte cannot fail after the caller satisfies the safety
156    /// contract.
157    type DecodeError = Infallible;
158    /// Encoding a byte cannot fail after the caller satisfies the safety
159    /// contract.
160    type EncodeError = Infallible;
161
162    const MIN_UNITS_PER_VALUE: usize = 1;
163    const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
164
165    const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
166
167    /// Decodes one byte from the supplied input slice.
168    ///
169    /// # Parameters
170    ///
171    /// - `input`: Source byte buffer.
172    /// - `input_index`: Index of the byte to decode.
173    ///
174    /// # Returns
175    ///
176    /// Returns the decoded byte and the number of bytes consumed.
177    ///
178    /// # Safety
179    ///
180    /// The caller must satisfy the `Codec::decode` buffer and index
181    /// preconditions; the input must contain the byte at `input_index`.
182    #[inline]
183    unsafe fn decode(
184        &mut self,
185        input: &[u8],
186        input_index: usize,
187    ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
188        // SAFETY: The caller upholds the `Codec::decode` contract.
189        Ok(unsafe { Self::decode(input, input_index) })
190    }
191
192    /// Encodes one byte into the supplied output slice.
193    ///
194    /// # Parameters
195    ///
196    /// - `value`: Byte value to encode.
197    /// - `output`: Destination byte buffer.
198    /// - `output_index`: Index where the encoded byte is written.
199    ///
200    /// # Returns
201    ///
202    /// Returns the number of bytes written.
203    ///
204    /// # Safety
205    ///
206    /// The caller must satisfy the `Codec::encode` buffer and index
207    /// preconditions; the output must have space at `output_index`.
208    #[inline]
209    unsafe fn encode(
210        &mut self,
211        value: &u8,
212        output: &mut [u8],
213        output_index: usize,
214    ) -> Result<usize, Self::EncodeError> {
215        // SAFETY: The caller upholds the `Codec::encode` contract.
216        unsafe {
217            Self::encode(*value, output, output_index);
218        }
219        Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
220    }
221}
222
223impl<O> BinaryCodec<i8, O>
224where
225    O: ByteOrderSpec,
226{
227    /// Minimum number of bytes required to encode or decode this type.
228    pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
229
230    /// Maximum number of bytes emitted when encoding this type.
231    pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
232
233    /// Maximum number of bytes consumed when decoding this type.
234    pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
235
236    /// Decodes a value from `input` starting at `input_index` without bounds
237    /// checks.
238    ///
239    /// # Parameters
240    ///
241    /// - `input`: Source byte buffer.
242    /// - `input_index`: Start index in `input`.
243    ///
244    /// # Returns
245    ///
246    /// Returns the decoded value and the non-zero number of consumed bytes.
247    ///
248    /// # Safety
249    ///
250    /// The caller must guarantee that `input.as_ptr().add(input_index)` is
251    /// valid to read [`Self::MIN_UNITS_PER_VALUE`] bytes.
252    #[must_use]
253    #[inline]
254    pub unsafe fn decode(input: &[u8], input_index: usize) -> (i8, NonZeroUsize) {
255        debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
256
257        // SAFETY: The caller guarantees that the indexed byte is readable.
258        (
259            unsafe { UncheckedSlice::read(input, input_index) } as i8,
260            nonzero(Self::MIN_UNITS_PER_VALUE),
261        )
262    }
263
264    /// Encodes `value` into `output` starting at `output_index` without bounds
265    /// checks.
266    ///
267    /// # Parameters
268    ///
269    /// - `value`: Value to encode.
270    /// - `output`: Destination byte buffer.
271    /// - `output_index`: Start index in `output`.
272    ///
273    /// # Returns
274    ///
275    /// Returns the number of bytes written, equal to
276    /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
277    /// The next output position is `output_index + written`, where `written`
278    /// is the returned byte count.
279    ///
280    /// # Safety
281    ///
282    /// The caller must guarantee that `output.as_mut_ptr().add(output_index)`
283    /// is valid to write [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes.
284    #[inline]
285    pub unsafe fn encode(value: i8, output: &mut [u8], output_index: usize) -> usize {
286        debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());
287
288        // SAFETY: The caller guarantees that the indexed byte is writable.
289        unsafe {
290            UncheckedSlice::write(output, output_index, value as u8);
291        }
292        Self::MAX_ENCODE_UNITS_PER_VALUE
293    }
294}
295
296impl<O> Codec for BinaryCodec<i8, O>
297where
298    O: ByteOrderSpec,
299{
300    /// Logical signed byte decoded from or encoded into the unit buffer.
301    type Value = i8;
302    /// One byte used as both the input and output unit.
303    type Unit = u8;
304    /// Decoding a byte cannot fail after the caller satisfies the safety
305    /// contract.
306    type DecodeError = Infallible;
307    /// Encoding a byte cannot fail after the caller satisfies the safety
308    /// contract.
309    type EncodeError = Infallible;
310
311    const MIN_UNITS_PER_VALUE: usize = 1;
312    const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
313
314    const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
315
316    /// Decodes one signed byte from the supplied input slice.
317    ///
318    /// # Parameters
319    ///
320    /// - `input`: Source byte buffer.
321    /// - `input_index`: Index of the byte to decode.
322    ///
323    /// # Returns
324    ///
325    /// Returns the decoded signed byte and the number of bytes consumed.
326    ///
327    /// # Safety
328    ///
329    /// The caller must satisfy the `Codec::decode` buffer and index
330    /// preconditions; the input must contain the byte at `input_index`.
331    #[inline]
332    unsafe fn decode(
333        &mut self,
334        input: &[u8],
335        input_index: usize,
336    ) -> Result<(i8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
337        // SAFETY: The caller upholds the `Codec::decode` contract.
338        Ok(unsafe { Self::decode(input, input_index) })
339    }
340
341    /// Encodes one signed byte into the supplied output slice.
342    ///
343    /// # Parameters
344    ///
345    /// - `value`: Signed byte value to encode.
346    /// - `output`: Destination byte buffer.
347    /// - `output_index`: Index where the encoded byte is written.
348    ///
349    /// # Returns
350    ///
351    /// Returns the number of bytes written.
352    ///
353    /// # Safety
354    ///
355    /// The caller must satisfy the `Codec::encode` buffer and index
356    /// preconditions; the output must have space at `output_index`.
357    #[inline]
358    unsafe fn encode(
359        &mut self,
360        value: &i8,
361        output: &mut [u8],
362        output_index: usize,
363    ) -> Result<usize, Self::EncodeError> {
364        // SAFETY: The caller upholds the `Codec::encode` contract.
365        unsafe {
366            Self::encode(*value, output, output_index);
367        }
368        Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
369    }
370}
371
372macro_rules! impl_integer_binary_codec {
373    ($ty:ty, $len:expr) => {
374        impl BinaryCodec<$ty, BigEndian> {
375            /// Minimum number of bytes required to encode or decode this type.
376            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
377
378            /// Maximum number of bytes emitted when encoding this type.
379            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
380
381            /// Maximum number of bytes consumed when decoding this type.
382            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
383
384            /// Decodes a value from `input` starting at `input_index` without
385            /// bounds checks.
386            ///
387            /// This function is intended for hot binary codec paths where the
388            /// caller has already validated the buffer length externally.
389            ///
390            /// # Parameters
391            ///
392            /// - `input`: Source byte buffer.
393            /// - `input_index`: Start byte index in `input`.
394            ///
395            /// # Returns
396            ///
397            /// Returns the decoded value and the non-zero number of consumed
398            /// bytes.
399            ///
400            /// # Safety
401            ///
402            /// The caller must guarantee that:
403            ///
404            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
405            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
406            ///   for reading.
407            #[must_use]
408            #[inline]
409            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
410                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
411
412                // SAFETY:
413                // The caller guarantees that the readable range is fully
414                // in-bounds. This unaligned helper handles byte-aligned load.
415                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
416
417                (<$ty>::from_be(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
418            }
419
420            /// Encodes `value` into `output` starting at `output_index`
421            /// without bounds checks.
422            ///
423            /// This function is intended for hot binary codec paths where the
424            /// caller has already validated the buffer length externally.
425            ///
426            /// # Parameters
427            ///
428            /// - `value`: Value to encode.
429            /// - `output`: Destination byte buffer.
430            /// - `output_index`: Start byte index in `output`.
431            ///
432            /// # Returns
433            ///
434            /// Returns the number of bytes written, equal to
435            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
436            /// The next output position is `output_index + written`, where `written`
437            /// is the returned byte count.
438            ///
439            /// # Safety
440            ///
441            /// The caller must guarantee that:
442            ///
443            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
444            /// - `output[output_index..output_index +
445            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
446            #[inline]
447            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
448                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
449
450                let raw = value.to_be();
451
452                // SAFETY:
453                // The caller guarantees that the writable range is fully
454                // in-bounds. This unaligned helper handles byte-aligned store.
455                unsafe {
456                    UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
457                }
458                Self::MAX_ENCODE_UNITS_PER_VALUE
459            }
460        }
461
462        impl Codec for BinaryCodec<$ty, BigEndian> {
463            /// The scalar value represented by this codec implementation.
464            type Value = $ty;
465            /// One byte of the encoded representation.
466            type Unit = u8;
467            /// Decoding cannot fail after the buffer preconditions are met.
468            type DecodeError = Infallible;
469            /// Encoding cannot fail after the buffer preconditions are met.
470            type EncodeError = Infallible;
471
472            const MIN_UNITS_PER_VALUE: usize = $len;
473            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
474
475            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
476
477            /// Decodes one scalar value from the supplied input slice.
478            ///
479            /// # Parameters
480            ///
481            /// - `input`: Source byte buffer.
482            /// - `input_index`: Index where decoding starts.
483            ///
484            /// # Returns
485            ///
486            /// Returns the decoded scalar and the number of bytes consumed.
487            ///
488            /// # Safety
489            ///
490            /// The caller must satisfy the `Codec::decode` buffer and index
491            /// preconditions for this scalar's fixed encoded width.
492            #[inline]
493            unsafe fn decode(
494                &mut self,
495                input: &[u8],
496                input_index: usize,
497            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
498                // SAFETY: The caller upholds the `Codec::decode`
499                // contract.
500                Ok(unsafe { Self::decode(input, input_index) })
501            }
502
503            /// Encodes one scalar value into the supplied output slice.
504            ///
505            /// # Parameters
506            ///
507            /// - `value`: Scalar value to encode.
508            /// - `output`: Destination byte buffer.
509            /// - `output_index`: Index where encoding starts.
510            ///
511            /// # Returns
512            ///
513            /// Returns the number of bytes written.
514            ///
515            /// # Safety
516            ///
517            /// The caller must satisfy the `Codec::encode` buffer and index
518            /// preconditions for this scalar's fixed encoded width.
519            #[inline]
520            unsafe fn encode(
521                &mut self,
522                value: &$ty,
523                output: &mut [u8],
524                output_index: usize,
525            ) -> Result<usize, Self::EncodeError> {
526                // SAFETY: The caller upholds the `Codec::encode`
527                // contract.
528                unsafe {
529                    Self::encode(*value, output, output_index);
530                }
531                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
532            }
533        }
534
535        impl BinaryCodec<$ty, LittleEndian> {
536            /// Minimum number of bytes required to encode or decode this type.
537            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
538
539            /// Maximum number of bytes emitted when encoding this type.
540            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
541
542            /// Maximum number of bytes consumed when decoding this type.
543            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
544
545            /// Decodes a value from `input` starting at `input_index` without
546            /// bounds checks.
547            ///
548            /// This function is intended for hot binary codec paths where the
549            /// caller has already validated the buffer length externally.
550            ///
551            /// # Parameters
552            ///
553            /// - `input`: Source byte buffer.
554            /// - `input_index`: Start byte index in `input`.
555            ///
556            /// # Returns
557            ///
558            /// Returns the decoded value and the non-zero number of consumed
559            /// bytes.
560            ///
561            /// # Safety
562            ///
563            /// The caller must guarantee that:
564            ///
565            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
566            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
567            ///   for reading.
568            #[must_use]
569            #[inline]
570            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
571                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
572
573                // SAFETY:
574                // The caller guarantees that the readable range is fully
575                // in-bounds. This unaligned helper handles byte-aligned load.
576                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
577
578                (<$ty>::from_le(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
579            }
580
581            /// Encodes `value` into `output` starting at `output_index`
582            /// without bounds checks.
583            ///
584            /// This function is intended for hot binary codec paths where the
585            /// caller has already validated the buffer length externally.
586            ///
587            /// # Parameters
588            ///
589            /// - `value`: Value to encode.
590            /// - `output`: Destination byte buffer.
591            /// - `output_index`: Start byte index in `output`.
592            ///
593            /// # Returns
594            ///
595            /// Returns the number of bytes written, equal to
596            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
597            /// The next output position is `output_index + written`, where `written`
598            /// is the returned byte count.
599            ///
600            /// # Safety
601            ///
602            /// The caller must guarantee that:
603            ///
604            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
605            /// - `output[output_index..output_index +
606            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
607            #[inline]
608            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
609                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
610
611                let raw = value.to_le();
612
613                // SAFETY:
614                // The caller guarantees that the writable range is fully
615                // in-bounds. This unaligned helper handles byte-aligned store.
616                unsafe {
617                    UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
618                }
619                Self::MAX_ENCODE_UNITS_PER_VALUE
620            }
621        }
622
623        impl Codec for BinaryCodec<$ty, LittleEndian> {
624            /// The scalar value represented by this codec implementation.
625            type Value = $ty;
626            /// One byte of the encoded representation.
627            type Unit = u8;
628            /// Decoding cannot fail after the buffer preconditions are met.
629            type DecodeError = Infallible;
630            /// Encoding cannot fail after the buffer preconditions are met.
631            type EncodeError = Infallible;
632
633            const MIN_UNITS_PER_VALUE: usize = $len;
634            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
635
636            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
637
638            /// Decodes one scalar value from the supplied input slice.
639            ///
640            /// # Parameters
641            ///
642            /// - `input`: Source byte buffer.
643            /// - `input_index`: Index where decoding starts.
644            ///
645            /// # Returns
646            ///
647            /// Returns the decoded scalar and the number of bytes consumed.
648            ///
649            /// # Safety
650            ///
651            /// The caller must satisfy the `Codec::decode` buffer and index
652            /// preconditions for this scalar's fixed encoded width.
653            #[inline]
654            unsafe fn decode(
655                &mut self,
656                input: &[u8],
657                input_index: usize,
658            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
659                // SAFETY: The caller upholds the `Codec::decode`
660                // contract.
661                Ok(unsafe { Self::decode(input, input_index) })
662            }
663
664            /// Encodes one scalar value into the supplied output slice.
665            ///
666            /// # Parameters
667            ///
668            /// - `value`: Scalar value to encode.
669            /// - `output`: Destination byte buffer.
670            /// - `output_index`: Index where encoding starts.
671            ///
672            /// # Returns
673            ///
674            /// Returns the number of bytes written.
675            ///
676            /// # Safety
677            ///
678            /// The caller must satisfy the `Codec::encode` buffer and index
679            /// preconditions for this scalar's fixed encoded width.
680            #[inline]
681            unsafe fn encode(
682                &mut self,
683                value: &$ty,
684                output: &mut [u8],
685                output_index: usize,
686            ) -> Result<usize, Self::EncodeError> {
687                // SAFETY: The caller upholds the `Codec::encode`
688                // contract.
689                unsafe {
690                    Self::encode(*value, output, output_index);
691                }
692                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
693            }
694        }
695    };
696}
697
698macro_rules! impl_float_binary_codec {
699    ($ty:ty, $bits:ty, $len:expr) => {
700        impl BinaryCodec<$ty, BigEndian> {
701            /// Minimum number of bytes required to encode or decode this type.
702            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
703
704            /// Maximum number of bytes emitted when encoding this type.
705            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
706
707            /// Maximum number of bytes consumed when decoding this type.
708            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
709
710            /// Decodes a value from `input` starting at `input_index` without
711            /// bounds checks.
712            ///
713            /// This function is intended for hot binary codec paths where the
714            /// caller has already validated the buffer length externally.
715            ///
716            /// # Parameters
717            ///
718            /// - `input`: Source byte buffer.
719            /// - `input_index`: Start byte index in `input`.
720            ///
721            /// # Returns
722            ///
723            /// Returns the decoded floating-point value and the non-zero number
724            /// of consumed bytes.
725            ///
726            /// # Safety
727            ///
728            /// The caller must guarantee that:
729            ///
730            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
731            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
732            ///   for reading.
733            #[must_use]
734            #[inline]
735            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
736                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
737
738                // SAFETY:
739                // The caller guarantees that the readable range is fully
740                // in-bounds. This unaligned helper handles byte-aligned load.
741                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
742
743                (
744                    <$ty>::from_bits(<$bits>::from_be(raw)),
745                    nonzero(Self::MIN_UNITS_PER_VALUE),
746                )
747            }
748
749            /// Encodes `value` into `output` starting at `output_index`
750            /// without bounds checks.
751            ///
752            /// This function is intended for hot binary codec paths where the
753            /// caller has already validated the buffer length externally.
754            ///
755            /// # Parameters
756            ///
757            /// - `value`: Floating-point value to encode.
758            /// - `output`: Destination byte buffer.
759            /// - `output_index`: Start byte index in `output`.
760            ///
761            /// # Returns
762            ///
763            /// Returns the number of bytes written, equal to
764            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
765            /// The next output position is `output_index + written`, where `written`
766            /// is the returned byte count.
767            ///
768            /// # Safety
769            ///
770            /// The caller must guarantee that:
771            ///
772            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
773            /// - `output[output_index..output_index +
774            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
775            #[inline]
776            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
777                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
778
779                let raw = value.to_bits().to_be();
780
781                // SAFETY:
782                // The caller guarantees that the writable range is fully
783                // in-bounds. This unaligned helper handles byte-aligned store.
784                unsafe {
785                    UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
786                }
787                Self::MAX_ENCODE_UNITS_PER_VALUE
788            }
789        }
790
791        impl Codec for BinaryCodec<$ty, BigEndian> {
792            /// The scalar value represented by this codec implementation.
793            type Value = $ty;
794            /// One byte of the encoded representation.
795            type Unit = u8;
796            /// Decoding cannot fail after the buffer preconditions are met.
797            type DecodeError = Infallible;
798            /// Encoding cannot fail after the buffer preconditions are met.
799            type EncodeError = Infallible;
800
801            const MIN_UNITS_PER_VALUE: usize = $len;
802            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
803
804            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
805
806            /// Decodes one scalar value from the supplied input slice.
807            ///
808            /// # Parameters
809            ///
810            /// - `input`: Source byte buffer.
811            /// - `input_index`: Index where decoding starts.
812            ///
813            /// # Returns
814            ///
815            /// Returns the decoded scalar and the number of bytes consumed.
816            ///
817            /// # Safety
818            ///
819            /// The caller must satisfy the `Codec::decode` buffer and index
820            /// preconditions for this scalar's fixed encoded width.
821            #[inline]
822            unsafe fn decode(
823                &mut self,
824                input: &[u8],
825                input_index: usize,
826            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
827                // SAFETY: The caller upholds the `Codec::decode`
828                // contract.
829                Ok(unsafe { Self::decode(input, input_index) })
830            }
831
832            /// Encodes one scalar value into the supplied output slice.
833            ///
834            /// # Parameters
835            ///
836            /// - `value`: Scalar value to encode.
837            /// - `output`: Destination byte buffer.
838            /// - `output_index`: Index where encoding starts.
839            ///
840            /// # Returns
841            ///
842            /// Returns the number of bytes written.
843            ///
844            /// # Safety
845            ///
846            /// The caller must satisfy the `Codec::encode` buffer and index
847            /// preconditions for this scalar's fixed encoded width.
848            #[inline]
849            unsafe fn encode(
850                &mut self,
851                value: &$ty,
852                output: &mut [u8],
853                output_index: usize,
854            ) -> Result<usize, Self::EncodeError> {
855                // SAFETY: The caller upholds the `Codec::encode`
856                // contract.
857                unsafe {
858                    Self::encode(*value, output, output_index);
859                }
860                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
861            }
862        }
863
864        impl BinaryCodec<$ty, LittleEndian> {
865            /// Minimum number of bytes required to encode or decode this type.
866            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
867
868            /// Maximum number of bytes emitted when encoding this type.
869            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
870
871            /// Maximum number of bytes consumed when decoding this type.
872            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
873
874            /// Decodes a value from `input` starting at `input_index` without
875            /// bounds checks.
876            ///
877            /// This function is intended for hot binary codec paths where the
878            /// caller has already validated the buffer length externally.
879            ///
880            /// # Parameters
881            ///
882            /// - `input`: Source byte buffer.
883            /// - `input_index`: Start byte index in `input`.
884            ///
885            /// # Returns
886            ///
887            /// Returns the decoded floating-point value and the non-zero number
888            /// of consumed bytes.
889            ///
890            /// # Safety
891            ///
892            /// The caller must guarantee that:
893            ///
894            /// - `input_index + Self::MIN_UNITS_PER_VALUE <= input.len()`
895            /// - `input[input_index..input_index + Self::MIN_UNITS_PER_VALUE]` is valid
896            ///   for reading.
897            #[must_use]
898            #[inline]
899            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
900                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
901
902                // SAFETY:
903                // The caller guarantees that the readable range is fully
904                // in-bounds. This unaligned helper handles byte-aligned load.
905                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
906
907                (
908                    <$ty>::from_bits(<$bits>::from_le(raw)),
909                    nonzero(Self::MIN_UNITS_PER_VALUE),
910                )
911            }
912
913            /// Encodes `value` into `output` starting at `output_index`
914            /// without bounds checks.
915            ///
916            /// This function is intended for hot binary codec paths where the
917            /// caller has already validated the buffer length externally.
918            ///
919            /// # Parameters
920            ///
921            /// - `value`: Floating-point value to encode.
922            /// - `output`: Destination byte buffer.
923            /// - `output_index`: Start byte index in `output`.
924            ///
925            /// # Returns
926            ///
927            /// Returns the number of bytes written, equal to
928            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`], independently of `output_index`.
929            /// The next output position is `output_index + written`, where `written`
930            /// is the returned byte count.
931            ///
932            /// # Safety
933            ///
934            /// The caller must guarantee that:
935            ///
936            /// - `output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len()`
937            /// - `output[output_index..output_index +
938            ///   Self::MAX_ENCODE_UNITS_PER_VALUE]` is valid for writing.
939            #[inline]
940            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
941                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
942
943                let raw = value.to_bits().to_le();
944
945                // SAFETY:
946                // The caller guarantees that the writable range is fully
947                // in-bounds. This unaligned helper handles byte-aligned store.
948                unsafe {
949                    UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
950                }
951                Self::MAX_ENCODE_UNITS_PER_VALUE
952            }
953        }
954
955        impl Codec for BinaryCodec<$ty, LittleEndian> {
956            /// The scalar value represented by this codec implementation.
957            type Value = $ty;
958            /// One byte of the encoded representation.
959            type Unit = u8;
960            /// Decoding cannot fail after the buffer preconditions are met.
961            type DecodeError = Infallible;
962            /// Encoding cannot fail after the buffer preconditions are met.
963            type EncodeError = Infallible;
964
965            const MIN_UNITS_PER_VALUE: usize = $len;
966            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
967
968            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
969
970            /// Decodes one scalar value from the supplied input slice.
971            ///
972            /// # Parameters
973            ///
974            /// - `input`: Source byte buffer.
975            /// - `input_index`: Index where decoding starts.
976            ///
977            /// # Returns
978            ///
979            /// Returns the decoded scalar and the number of bytes consumed.
980            ///
981            /// # Safety
982            ///
983            /// The caller must satisfy the `Codec::decode` buffer and index
984            /// preconditions for this scalar's fixed encoded width.
985            #[inline]
986            unsafe fn decode(
987                &mut self,
988                input: &[u8],
989                input_index: usize,
990            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
991                // SAFETY: The caller upholds the `Codec::decode`
992                // contract.
993                Ok(unsafe { Self::decode(input, input_index) })
994            }
995
996            /// Encodes one scalar value into the supplied output slice.
997            ///
998            /// # Parameters
999            ///
1000            /// - `value`: Scalar value to encode.
1001            /// - `output`: Destination byte buffer.
1002            /// - `output_index`: Index where encoding starts.
1003            ///
1004            /// # Returns
1005            ///
1006            /// Returns the number of bytes written.
1007            ///
1008            /// # Safety
1009            ///
1010            /// The caller must satisfy the `Codec::encode` buffer and index
1011            /// preconditions for this scalar's fixed encoded width.
1012            #[inline]
1013            unsafe fn encode(
1014                &mut self,
1015                value: &$ty,
1016                output: &mut [u8],
1017                output_index: usize,
1018            ) -> Result<usize, Self::EncodeError> {
1019                // SAFETY: The caller upholds the `Codec::encode`
1020                // contract.
1021                unsafe {
1022                    Self::encode(*value, output, output_index);
1023                }
1024                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
1025            }
1026        }
1027    };
1028}
1029
1030macro_rules! impl_native_integer_binary_codec {
1031    ($ty:ty, $len:expr) => {
1032        impl BinaryCodec<$ty, NativeEndian> {
1033            /// Minimum number of bytes required to encode or decode this type.
1034            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
1035            /// Maximum number of bytes emitted when encoding this type.
1036            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
1037            /// Maximum number of bytes consumed when decoding this type.
1038            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
1039
1040            /// Decodes a native-endian value without bounds checks.
1041            ///
1042            /// # Parameters
1043            ///
1044            /// - `input`: Byte slice containing the encoded value.
1045            /// - `input_index`: Absolute byte index where decoding starts.
1046            ///
1047            /// # Returns
1048            ///
1049            /// Returns the decoded value and the fixed number of bytes read.
1050            ///
1051            /// # Safety
1052            ///
1053            /// The caller must guarantee that at least
1054            /// [`Self::MIN_UNITS_PER_VALUE`] bytes are readable from `input_index`
1055            /// within `input`.
1056            #[must_use]
1057            #[inline]
1058            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
1059                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
1060
1061                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
1062                (raw, nonzero(Self::MIN_UNITS_PER_VALUE))
1063            }
1064
1065            /// Encodes a native-endian value without bounds checks.
1066            ///
1067            /// # Parameters
1068            ///
1069            /// - `value`: Value to encode.
1070            /// - `output`: Destination byte slice.
1071            /// - `output_index`: Absolute byte index where encoding starts.
1072            ///
1073            /// # Returns
1074            ///
1075            /// Returns the fixed number of bytes written.
1076            ///
1077            /// # Safety
1078            ///
1079            /// The caller must guarantee that at least
1080            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes are writable from
1081            /// `output_index` within `output`.
1082            #[inline]
1083            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
1084                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
1085
1086                unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value) };
1087                Self::MAX_ENCODE_UNITS_PER_VALUE
1088            }
1089        }
1090
1091        impl Codec for BinaryCodec<$ty, NativeEndian> {
1092            /// The scalar value represented by this codec implementation.
1093            type Value = $ty;
1094            /// One byte of the encoded representation.
1095            type Unit = u8;
1096            /// Decoding cannot fail after the buffer preconditions are met.
1097            type DecodeError = Infallible;
1098            /// Encoding cannot fail after the buffer preconditions are met.
1099            type EncodeError = Infallible;
1100            const MIN_UNITS_PER_VALUE: usize = $len;
1101            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
1102            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
1103
1104            /// Decodes one scalar value from the supplied input slice.
1105            ///
1106            /// # Parameters
1107            ///
1108            /// - `input`: Source byte buffer.
1109            /// - `input_index`: Index where decoding starts.
1110            ///
1111            /// # Returns
1112            ///
1113            /// Returns the decoded scalar and the number of bytes consumed.
1114            ///
1115            /// # Safety
1116            ///
1117            /// The caller must satisfy the `Codec::decode` buffer and index
1118            /// preconditions for this scalar's fixed encoded width.
1119            #[inline]
1120            unsafe fn decode(
1121                &mut self,
1122                input: &[u8],
1123                input_index: usize,
1124            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
1125                Ok(unsafe { Self::decode(input, input_index) })
1126            }
1127
1128            /// Encodes one scalar value into the supplied output slice.
1129            ///
1130            /// # Parameters
1131            ///
1132            /// - `value`: Scalar value to encode.
1133            /// - `output`: Destination byte buffer.
1134            /// - `output_index`: Index where encoding starts.
1135            ///
1136            /// # Returns
1137            ///
1138            /// Returns the number of bytes written.
1139            ///
1140            /// # Safety
1141            ///
1142            /// The caller must satisfy the `Codec::encode` buffer and index
1143            /// preconditions for this scalar's fixed encoded width.
1144            #[inline]
1145            unsafe fn encode(
1146                &mut self,
1147                value: &$ty,
1148                output: &mut [u8],
1149                output_index: usize,
1150            ) -> Result<usize, Self::EncodeError> {
1151                unsafe { Self::encode(*value, output, output_index) };
1152                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
1153            }
1154        }
1155    };
1156}
1157
1158macro_rules! impl_native_float_binary_codec {
1159    ($ty:ty, $bits:ty, $len:expr) => {
1160        impl BinaryCodec<$ty, NativeEndian> {
1161            /// Minimum number of bytes required to encode or decode this type.
1162            pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
1163            /// Maximum number of bytes emitted when encoding this type.
1164            pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
1165            /// Maximum number of bytes consumed when decoding this type.
1166            pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
1167
1168            /// Decodes a native-endian floating-point value without bounds
1169            /// checks.
1170            ///
1171            /// # Parameters
1172            ///
1173            /// - `input`: Byte slice containing the encoded value.
1174            /// - `input_index`: Absolute byte index where decoding starts.
1175            ///
1176            /// # Returns
1177            ///
1178            /// Returns the decoded floating-point value and fixed byte count.
1179            ///
1180            /// # Safety
1181            ///
1182            /// The caller must guarantee that at least
1183            /// [`Self::MIN_UNITS_PER_VALUE`] bytes are readable from `input_index`
1184            /// within `input`.
1185            #[must_use]
1186            #[inline]
1187            pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
1188                debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
1189
1190                let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
1191                (<$ty>::from_bits(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
1192            }
1193
1194            /// Encodes a native-endian floating-point value without bounds
1195            /// checks.
1196            ///
1197            /// # Parameters
1198            ///
1199            /// - `value`: Floating-point value to encode.
1200            /// - `output`: Destination byte slice.
1201            /// - `output_index`: Absolute byte index where encoding starts.
1202            ///
1203            /// # Returns
1204            ///
1205            /// Returns the fixed number of bytes written.
1206            ///
1207            /// # Safety
1208            ///
1209            /// The caller must guarantee that at least
1210            /// [`Self::MAX_ENCODE_UNITS_PER_VALUE`] bytes are writable from
1211            /// `output_index` within `output`.
1212            #[inline]
1213            pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
1214                debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
1215
1216                unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value.to_bits()) };
1217                Self::MAX_ENCODE_UNITS_PER_VALUE
1218            }
1219        }
1220
1221        impl Codec for BinaryCodec<$ty, NativeEndian> {
1222            /// The scalar value represented by this codec implementation.
1223            type Value = $ty;
1224            /// One byte of the encoded representation.
1225            type Unit = u8;
1226            /// Decoding cannot fail after the buffer preconditions are met.
1227            type DecodeError = Infallible;
1228            /// Encoding cannot fail after the buffer preconditions are met.
1229            type EncodeError = Infallible;
1230            const MIN_UNITS_PER_VALUE: usize = $len;
1231            const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
1232            const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
1233
1234            /// Decodes one scalar value from the supplied input slice.
1235            ///
1236            /// # Parameters
1237            ///
1238            /// - `input`: Source byte buffer.
1239            /// - `input_index`: Index where decoding starts.
1240            ///
1241            /// # Returns
1242            ///
1243            /// Returns the decoded scalar and the number of bytes consumed.
1244            ///
1245            /// # Safety
1246            ///
1247            /// The caller must satisfy the `Codec::decode` buffer and index
1248            /// preconditions for this scalar's fixed encoded width.
1249            #[inline]
1250            unsafe fn decode(
1251                &mut self,
1252                input: &[u8],
1253                input_index: usize,
1254            ) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
1255                Ok(unsafe { Self::decode(input, input_index) })
1256            }
1257
1258            /// Encodes one scalar value into the supplied output slice.
1259            ///
1260            /// # Parameters
1261            ///
1262            /// - `value`: Scalar value to encode.
1263            /// - `output`: Destination byte buffer.
1264            /// - `output_index`: Index where encoding starts.
1265            ///
1266            /// # Returns
1267            ///
1268            /// Returns the number of bytes written.
1269            ///
1270            /// # Safety
1271            ///
1272            /// The caller must satisfy the `Codec::encode` buffer and index
1273            /// preconditions for this scalar's fixed encoded width.
1274            #[inline]
1275            unsafe fn encode(
1276                &mut self,
1277                value: &$ty,
1278                output: &mut [u8],
1279                output_index: usize,
1280            ) -> Result<usize, Self::EncodeError> {
1281                unsafe { Self::encode(*value, output, output_index) };
1282                Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
1283            }
1284        }
1285    };
1286}
1287
1288impl_integer_binary_codec!(u16, 2);
1289impl_integer_binary_codec!(u32, 4);
1290impl_integer_binary_codec!(u64, 8);
1291impl_integer_binary_codec!(u128, 16);
1292impl_integer_binary_codec!(i16, 2);
1293impl_integer_binary_codec!(i32, 4);
1294impl_integer_binary_codec!(i64, 8);
1295impl_integer_binary_codec!(i128, 16);
1296impl_float_binary_codec!(f32, u32, 4);
1297impl_float_binary_codec!(f64, u64, 8);
1298impl_native_integer_binary_codec!(u16, 2);
1299impl_native_integer_binary_codec!(u32, 4);
1300impl_native_integer_binary_codec!(u64, 8);
1301impl_native_integer_binary_codec!(u128, 16);
1302impl_native_integer_binary_codec!(i16, 2);
1303impl_native_integer_binary_codec!(i32, 4);
1304impl_native_integer_binary_codec!(i64, 8);
1305impl_native_integer_binary_codec!(i128, 16);
1306impl_native_float_binary_codec!(f32, u32, 4);
1307impl_native_float_binary_codec!(f64, u64, 8);