Skip to main content

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