Skip to main content

qubit_codec_binary/codec/
non_strict.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 super::Leb128DecodePolicy;
10
11/// Selects LEB128 decoding that accepts non-canonical integer encodings.
12///
13/// Use this zero-sized policy as the second type parameter of
14/// [`crate::Leb128Codec`] or [`crate::ZigZagCodec`] when decoding
15/// compatibility-sensitive input. Encoding remains canonical regardless of the
16/// selected policy.
17///
18/// # Examples
19///
20/// ```
21/// use qubit_codec_binary::{Leb128Codec, NonStrict};
22///
23/// let input = [0x80, 0x00];
24/// let (value, consumed) = unsafe {
25///     Leb128Codec::<u16, NonStrict>::decode(&input, 0)
26/// }
27/// .expect("non-canonical zero should decode with the non-strict policy");
28/// assert_eq!(0, value);
29/// assert_eq!(2, consumed.get());
30/// ```
31#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
32pub struct NonStrict;
33
34impl super::internal::sealed::Sealed for NonStrict {}
35
36impl Leb128DecodePolicy for NonStrict {
37    /// Whether this policy rejects non-canonical encodings.
38    const STRICT: bool = false;
39}