Skip to main content

qubit_codec/transcode/
encode_context.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//! Encode context for one buffered encode step.
9
10/// Context for one encode attempt inside a buffered encoder engine.
11///
12/// The context carries the current input value and output cursor. It does not
13/// contain the prepared [`crate::EncodePlan`]; the engine keeps planning
14/// separate so callers and hooks can distinguish cursor state from policy
15/// state.
16///
17/// # Type Parameters
18///
19/// - `Value`: Logical input value type.
20/// - `Unit`: Encoded output unit type.
21#[derive(Debug)]
22pub struct EncodeContext<'a, Value, Unit> {
23    /// Input value being encoded.
24    pub input_value: &'a Value,
25
26    /// Absolute input index of [`input_value`](Self::input_value).
27    pub input_index: usize,
28
29    /// Complete output unit slice visible to the encoder.
30    pub output: &'a mut [Unit],
31
32    /// Start position in [`output`](Self::output) where writing begins.
33    pub output_index: usize,
34}
35
36impl<Value, Unit> EncodeContext<'_, Value, Unit> {
37    /// Returns writable output units from the current output index.
38    ///
39    /// # Returns
40    ///
41    /// Returns output capacity visible to this encode step.
42    #[must_use]
43    #[inline(always)]
44    pub fn available_output(&self) -> usize {
45        self.output.len().saturating_sub(self.output_index)
46    }
47}