qubit_codec/transcode/engine/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//! Read-only policy context for one buffered encode attempt.
9
10/// Read-only context supplied to an encode policy hook.
11///
12/// The engine retains ownership of the mutable output slice. Hooks can inspect
13/// the current value and cursor/capacity metadata, then return an
14/// [`crate::engine::EncodeUnencodableAction`] without being able to mutate
15/// output behind the engine's progress accounting.
16///
17/// # Type Parameters
18///
19/// - `Value`: Logical input value type.
20#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
21pub struct EncodeContext<'a, Value> {
22 input_value: &'a Value,
23 input_index: usize,
24 output_index: usize,
25 available_output: usize,
26}
27
28impl<'a, Value> EncodeContext<'a, Value> {
29 /// Creates an encode policy context.
30 ///
31 /// # Parameters
32 ///
33 /// - `input_value`: Borrowed input value being encoded.
34 /// - `input_index`: Absolute input index of `input_value`.
35 /// - `output_index`: Absolute output index where writing begins.
36 /// - `available_output`: Output units writable from `output_index`.
37 ///
38 /// # Returns
39 ///
40 /// Returns a read-only encode context.
41 #[inline(always)]
42 #[must_use]
43 pub const fn new(input_value: &'a Value, input_index: usize, output_index: usize, available_output: usize) -> Self {
44 Self {
45 input_value,
46 input_index,
47 output_index,
48 available_output,
49 }
50 }
51
52 /// Returns the input value being encoded.
53 #[inline(always)]
54 #[must_use]
55 pub const fn input_value(&self) -> &Value {
56 self.input_value
57 }
58
59 /// Returns the absolute input index of the current value.
60 #[inline(always)]
61 #[must_use]
62 pub const fn input_index(&self) -> usize {
63 self.input_index
64 }
65
66 /// Returns the absolute output index where writing begins.
67 #[inline(always)]
68 #[must_use]
69 pub const fn output_index(&self) -> usize {
70 self.output_index
71 }
72
73 /// Returns output capacity visible to this encode attempt.
74 #[inline(always)]
75 #[must_use]
76 pub const fn available_output(&self) -> usize {
77 self.available_output
78 }
79}