Skip to main content

qubit_codec/buffered/
decode_context.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Decode context passed to buffered decoder policy hooks.
11
12/// Context for one codec decode attempt inside a buffered decoder engine.
13#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
14pub struct DecodeContext {
15    /// Absolute source index where this `transcode` call starts.
16    pub input_start: usize,
17    /// Absolute source index where the attempted value starts.
18    pub input_index: usize,
19    /// Absolute output index where this `transcode` call starts.
20    pub output_start: usize,
21    /// Absolute output index where the next decoded value would be written.
22    pub output_index: usize,
23    /// Units visible to the codec from `input_index`.
24    pub available: usize,
25}
26
27impl DecodeContext {
28    /// Creates a decode context.
29    ///
30    /// # Parameters
31    ///
32    /// - `input_start`: Absolute source index where this `transcode` call starts.
33    /// - `input_index`: Absolute source index where the attempted value starts.
34    /// - `output_start`: Absolute output index where this `transcode` call starts.
35    /// - `output_index`: Absolute output index where the next value would be written.
36    /// - `available`: Units visible to the codec from `input_index`.
37    ///
38    /// # Returns
39    ///
40    /// Returns a decode context.
41    #[must_use]
42    #[inline(always)]
43    pub const fn new(
44        input_start: usize,
45        input_index: usize,
46        output_start: usize,
47        output_index: usize,
48        available: usize,
49    ) -> Self {
50        Self {
51            input_start,
52            input_index,
53            output_start,
54            output_index,
55            available,
56        }
57    }
58
59    /// Returns input units consumed since this `transcode` call started.
60    ///
61    /// # Returns
62    ///
63    /// Returns `input_index - input_start`.
64    #[must_use]
65    #[inline(always)]
66    pub const fn input_used(self) -> usize {
67        self.input_index - self.input_start
68    }
69
70    /// Returns output values written since this `transcode` call started.
71    ///
72    /// # Returns
73    ///
74    /// Returns `output_index - output_start`.
75    #[must_use]
76    #[inline(always)]
77    pub const fn output_written(self) -> usize {
78        self.output_index - self.output_start
79    }
80}